Reputation: 11
I'am trying to work with the external interrupt source and I wrote a small program to test the interrupt. When I start the program RB0 is set low, and RB1 is set high. If I set RB7 high, it must be generated an interrupt that reverses the logic state of RB0 and RB1. I don't know why the interrupt doesn't work. I have configured all registers, is something missing yet? The compiler is xc16.
Thanks.
Here is the code:
#include <xc.h>
#include "setup.h"
void main(void) {
AD1PCFG = 0xFFFF;
TRISBbits.TRISB7=1;
TRISBbits.TRISB0=0;
TRISBbits.TRISB1=0;
PORTBbits.RB0=0;
PORTBbits.RB1=0;
_INT0IE= 1; //enable interrupt on RB7
_INT0IF = 0; //clear status flag interrupt on RB7
_INT0IP = 0b010; /priority level 2 for INT0
while(1) {
_RB0=0;
_RB1=1;
}
}
void _ISR _INT0Interrupt(void) {
if(_INT0IF) {
_INT0IF = 0;
_RB0=1;
_RB1=0;
}
}
Upvotes: 0
Views: 322
Reputation: 335
it is generally a good idea to write to the latch registers and not the port. When you perform a write, you are actually doing a read of the port, modifying the bit(s), and then a write. Depending on the circuitry, your port read may not reflect the same value as you think. By writing to the latch register, you will read your desired output state and write that back to the port (via the latch)
Upvotes: 1
Reputation: 59
Write following code in your ISR as you want Simply reverse the state of RB0 & RB1 and you must Enable Global and peripheral Interrupt i.e. GIE & PIE
void_ISR_INT0Interrupt(void){
if(_INT0IF){
_INT0IF=0;
_RB0=~_RB0;
_RB1=~_RB1;
}
}
Upvotes: 1