开发者

PIC - is there a value for RB7?

开发者 https://www.devze.com 2023-02-18 22:24 出处:网络
I read the port RB7, and in the if a check the value. if there is output on RB7 I want that the led on my chip light up (led D1), but it burns all the time even when there is nothing connected to RB7.

I read the port RB7, and in the if a check the value. if there is output on RB7 I want that the led on my chip light up (led D1), but it burns all the time even when there is nothing connected to RB7. What i'm doing wrong? That's the PIC 18F4550 It's written in mplab v8.63 and the C18 compiler.

void main (void) 
{  
 TRISD = 0x00;            // PORTD  als uitgang 
 TRISB = 0b00110000;      // RB4 en RB5 als ingang 

 RCONbits.IPEN = 0;    // prioriteit uit 
 INTCONbits.GIE = 1;   // enable interrupt 
 INTCONbits.RB开发者_开发问答IE = 1;  // interrupt portB aan 

 TRISBbits.TRISB7 = 0; 
 TRISBbits.TRISB6 = 0; 
 TRISBbits.TRISB3 = 0; 

 while(1) 
 {  
  _asm sleep _endasm  
 } 
} 
#pragma interrupt ISR 
void ISR (void) 
{ 
 if (INTCONbits.RBIF==1) 
 {   
  if(LATBbits.LATB7 == 1)    // value on RB7 ? 
  { 
   LATDbits.LATD1 ^= 1;    // D2 togglen 
  } 
 }  
 INTCONbits.RBIF = 0; 
}


Three things:

  1. The inputs are always high or low regardless of whether you have anything wired up to them. Do you have appropriate pull-up or pull-down circuitry?
  2. You probably need to debounce that input to keep a single input from triggering multiple state changes.
  3. It doesn't look like you ever set output D1 on startup. I wouldn't assume that it defaults to any particular state.


You are reading back the output value on B7, which doesn't make a lot of sense. B7 is will be clear (low) unless you set it in code so there are no changes going on with B7. In any case, changes to the output latch for B7 would not trigger the RBIF.

The RBIF is set when any of RB7 to RB4 changes state. You have declared RB5 and RB4 as inputs, so why don't you read one of those when the RBIF fires?

if (PORTBbits.RB5 == 1)
   LATDbits.LATD1 ^= 1;

If you are having trouble with the interrupt-on-port-change functionality, just try the following line in main loop code to prove your IO.

  LATDbits.LATD1 = PORTBbits.RB5;

If your hardware does not have pull ups or pull downs, you should enable the weak pull ups on port B using the RBPU flag in INTCON2. Note that these are automatically turned off when you declare the pin to be an output.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号