huma
Joined: 23 Jun 2006 Posts: 12
|
problem with Port _b_ inpeetrup |
Posted: Sat Oct 21, 2006 5:37 am |
|
|
hello
i have to take input from encoders for that i decided to use RB4 pin(interrup on chnage) on pic16f877. i used the example code EX_PBUTT.c with little modifications.
on change i on/off led attached to RD0 pin.To have interrupt on change i used to ground pin RB4 & RB5.
The code is working well in proteus simulation but when i use it on actual hardware it is not responding well.
Instead when i ground pin RB3 it toggles LED. I m not getting wht the problem is .
I have also read the forums regarding port B interrupt on chnage but i m getting no info.
Please help me out.
Also i am not understanding why #if LOWTOHIGH & #elif HIGHTOLOW
are used in program.
Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#define LOWTOHIGH TRUE
#define HIGHTOLOW FALSE
short int dbutton4, dbutton5, dbutton6, dbutton7;
#int_rb
void detect_rb_change() {
int current;
static int last=0;
set_tris_b(0xF0);
current=input_b();
#if LOWTOHIGH
if ((!bit_test(last,4))&&(bit_test(current,4))) {dbutton4=1;}
if ((!bit_test(last,5))&&(bit_test(current,5))) {dbutton5=1;}
if ((!bit_test(last,6))&&(bit_test(current,6))) {dbutton6=1;}
if ((!bit_test(last,7))&&(bit_test(current,7))) {dbutton7=1;}
#elif HIGHTOLOW
if ((!bit_test(current,4))&&(bit_test(last,4))) {dbutton4=1;}
if ((!bit_test(current,5))&&(bit_test(last,5))) {dbutton5=1;}
if ((!bit_test(current,6))&&(bit_test(last,6))) {dbutton6=1;}
if ((!bit_test(current,7))&&(bit_test(last,7))) {dbutton7=1;}
#endif
last=current;
}
void clear_delta() {
dbutton4=0;
dbutton5=0;
dbutton6=0;
dbutton7=0;
}
void main() {
clear_delta();
port_b_pullups(TRUE);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
output_low(PIN_D0);
while (TRUE) {
if(dbutton4) {
output_high(PIN_D0);
dbutton4=FALSE;
}
if(dbutton5) {
output_low(PIN_D0);
dbutton5=FALSE;
}
}
}
|
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sat Oct 21, 2006 6:30 am |
|
|
Quote: | Also i am not understanding why #if LOWTOHIGH & #elif HIGHTOLOW |
I believe these are used to set whether you want to detect a switch change from low-to-high or high-to-low... If you are grounding a pullup with a switch then you would use HIGHTOLOW. To do that you would comment out the appropriate #define line.
Code: | //#define LOWTOHIGH TRUE |
Good luck,
John |
|