Frequently Asked Questions

How can the RB interrupt be used to detect a button press?

The RB interrupt will happen when there is any change (input or output) on pins B4-B7. There is only one interrupt and the PIC does not tell you which pin changed. The programmer must determine the change based on the previously known value of the port. Furthermore, a single button press may cause several interrupts due to bounce in the switch. A debounce algorithm will need to be used. The following is a simple example:

#int_rb 
rb_isr ( ) { 
   byte changes; 
   changes = last_b ^ port_b; 
   last_b = port_b; 
   if (bit_test(changes,4 )&& !bit_test(last_b,4)) { 
      //b4 went low 
   } 
   if (bit_test(changes,5)&& !bit_test (last_b,5)) { 
      //b5 went low 
   } 
   . 
   . 
   . 
   delay-ms (100); //debounce 
}

The delay=ms(100) is a quick and dirty debounce. In general, you will not want to sit in an ISR for 100 MS to allow the switch to debounce. A more elegant solution is to set a timer on the first interrupt and wait until the timer overflows. Don't process further changes on the pin.


C-Aware IDE Demo
Embedded C Learners Kit
EZ App Lynx