Hi All,
Fairly new to programming micro-controllers in C and I have been having some difficulty optimizing low power mode while keeping my interrupts short, like in this blog post. Was wondering if anyone could give me some advice on how to proceed.
Basically what I am trying to accomplish is setting up my device in main(), then waiting for an interrupt in low power mode before executing the corresponding code. Currently, I have this set up as follows, with the bulk of the code inside the ISR:
...
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 and enabling interrupts while(1) //Loop forever {}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
WDTCTL = WDTPW + WDTCNTCL;
P1IFG &= ~BIT3; // P1.3 IFG (interrupt flag) cleared
trig += 1; // Cycling through 'trig' values to activate different LEDs sequentially
if(trig == 4){
trig = 0x000;
}
if(trig == 1){
LED = BIT1;
} else if (trig == 2) {
LED = BIT3;
} else if (trig == 3) {
LED = BIT5;
} else if (trig == 0){
LED = 0x000;
}
}
As this is just a short example and this method would eventually lead to very large ISRs, something I am trying my best to avoid, I would like to poll using interrupts in low power mode and then deal with the triggered actions within main(). The following diagram I found online seems to suggest this is possible but I cannot figure out how to check a condition with the CPU off.
I have tried running the following script but keep returning an error as a condition cannot be evaluated in LPM0. Does anyone know of another way of handling this? Perhaps something similar to the MSP432's wait for interrupt (WFI) function which I believes halts code execution until an interrupt is generated?
{...
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 and enabling interrupts
while(1) { //Loop forever
if(trig == 4){
trig = 0x000;
}
if(trig == 1){
LED = BIT1;
} else if (trig == 2) {
LED = BIT3;
} else if (trig == 3) {
LED = BIT5;
} else if (trig == 0){
LED = 0x000;
}
__bis_SR_register(CPUOFF + GIE); //
}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
WDTCTL = WDTPW + WDTCNTCL;
P1IFG &= ~BIT3; // P1.3 IFG (interrupt flag) cleared
trig += 1; // Cycling through 'trig' values to activate different LEDs sequentially
__bic_SR_register_on_exit(LPM0_bits + GIE); // Exiting LMP0 on exit from interrupt, disabling interrupts until task in main is compete
}