I have a 12F683 micro and this is my first program using the new MPLAB with XC8 compiler.
I have configured Timer1 to trigger about every ms. For now I would need an LED to flash on and off but need to do this with an IF
statement because later I will add other actions in there. Here is the interrupt code:
#define PWM GPIObits.GP5
void interrupt High_Priority_Interrupt(void) {
if (CCP1IF) {
if (PWM) {
PWM = 0; //NEVER excuted
}
else {
PWM = 1; //ALWAYS executed
}
CCP1IF = 0;
}
}
For some reason the output is never set to 1. If I replace the whole code with PWM^=1;
it works like a charm. But I need to do it with the if
statement. What am I doing wrong?
PWM
declared? \$\endgroup\$CCP1IF
is never true. Also, since interrupts are non-deterministic, you need to (at least in my experience with AVR) declare any variable asvolatile
that is used in the ISR or the compiler will optimize it into read-only as it never changes in regular program flow and your interrupt routine is never called by software. \$\endgroup\$