This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430FR2311: POLLING VS INTERRUPT - Defining the correct flow of program using an interrupt.

Part Number: MSP430FR2311

Tool/software: Code Composer Studio

Hi,

I was wondering about the correct flow of program while using interrupts and its difference from Polling technique.

Lets say I want to perform a task A when a switch on P1.0 is pressed.

1. Polling method - Keep looking at P1.0. When the switch press is detected, perform Task A

2. Interrupt method - Tie an interrupt to P1.0. When switch is pressed, an interrupt is triggered. In the ISR, a global flag is flipped.

My concern with the interrupt technique is that I am still always polling for the flag status to change.

In that case does is matter that we do the polling of the switch itself (P1.0) or do polling of a flag variable?

Please verify if the below flow of program is correct way of implementing the code. Lets say I want to turn on an LED for 1 sec and then turn it off.

P1OUT |= BIT1 ;  //Turn LED on P1.1 ON

start1sectimer(); //Code to setup and start 1 sec timer with interrupt

while (timerflag==1); //wait here until 1 sec is elapsed and timerflag is flipped

P1OUT &= ~BIT1; // Turn LED off when 1sec elapsed

--------------ISR-----------

timerflag=0 //flip timerflag when ISR is invoked



  • Your sequence will work (be sure "timerflag" is declared "volatile"). No, it's not very much different from polling the timer (CCIFG) directly with no interrupt. What this indirect method allows is in that diagram box (middle left) which refers to "(LPMn)". You could say:

    > while (timerflag==1) LPM0; // sleep here until 1 sec is elapsed and timerflag is flipped. ISR wakes us up.

    (The "while" substitutes for all those "flag_1 set?" boxes lower-left. When you have more than one thing to check for, you should do it their way.)

    The counterpart in the ISR would be:

    > LPM0_EXIT;    // Wake up main

    This serves the box (upper right) which mentions "Modify SR..". This is also polling, but you're checking only when you have a high probability of success. In between you're asleep, consuming less energy. If you want to see how LPM0 and LPM0_EXIT work, just right-click on the name and "Go to definition".

**Attention** This is a public forum