2

I am trying to test the interrupt function at home without a board to push buttons etc. I gave up on my code and copied an example but still cant make it work. Below is the sample code.

In the while loop it sets the bits on but the interrupt doesn't get called. I have a break set in the interrupt. Maybe this isn't possible to test?

My end result is I need to test for button1 and button2 being pressed. I know how to do that without the interrupt but need to follow the lesson.

The professor wants us to use the interrupt. Thanks for help!

unsigned short sync_flag;
char bad_synch;            // variable for detecting bad synchronization
int cnt = 0;               // Global variable cnt

void Interrupt() {
  // This is external INT0 interrupt (for sync start)
  //   - once we get falling edge on RB0 we are disabling INT0 interrupt

  if (INT0IF_bit && INT0IE_bit) {
     cnt = 0;
     sync_flag = 1;
     INT0IF_bit = 0;
     INT0IE_bit = 0;
     PORTD = 0xFF;
   }
}

void main() {

  ADCON1 = 0x0F;           // AD converter off
  CMCON = 7;
  sync_flag = 0;           // sync_flag is set when falling edge on RB0 is detected

  while(1){

  TRISB = 0xFF;            // Set PB0 as input
  TRISD = 0x00;            // Set PortD as output
  PORTD = 0x00;            // Starting value for PortD

  INTEDG0_bit = 0;         // Interrupt on falling edge on RB0
  INT0IF_bit = 0;          // Clear INT0IF
  INT0IE_bit = 0;          // turn OFF interrupt on INT0
  GIE_bit = 1;             // enable GIE

  while(1){
      bad_synch = 0;       // set bad synchronization variable to zero
      sync_flag = 0;       // reseting sync flag
      INT0IF_bit = 1;
      INT0IE_bit = 1;      // enable external interrupt on RB0 (start sync procedure)
    }
  }
}
Mark Worsnop
  • 121
  • 1

0 Answers0