Projects
Friday, 19 July 2013
MSP430 Launchpad- The simplest possible TimerA example
This is probably the simplest possible example of a MSP430 timer program. Forget interrupts and ISRs, PWM and all that stuff you always see in timer related articles. I just want a program to wait until a specific time period has elapsed. This one will toggle the green led every time the time period elapses. Its period is set to 50000 'ticks' of SMCLK (running here about 1MHz) so you actually see it flashing (about every 20ms), too short a period and you won't see it at all.
I use Timer0_A channel 0 for this example to keep it very simple. TA0CCR0 is where we set the interval which must elapse before we get the notification, which comes in CCIFG (Interrupt flag). Note there was no need to enable interrupts or provide an ISR as we are just waiting for the interrupt flag to come on, and then continuing with the program.
#define GREEN_LED BIT6 //P1.6
int main() {
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // SMCLK @ 1MHz
DCOCTL = CALDCO_1MHZ;
P1DIR = GREEN_LED; // green led as output
TA0CCR0 = 50000; // ticks before int flag sets
TA0CTL = TASSEL_2 + MC_1; // MC_1 = up to TA0CCR0
while(1) { // keep doing this...
while(!(TA0CCTL0&CCIFG)); // whoa, wait for the int flag...
P1OUT ^= GREEN_LED; // flash the green led
TA0CCTL0 &= ~CCIFG; // turn flag off again for next time
}
}
The interrupt flag CCIFG simply 'comes on' when the timer overflows (i.e. reaches its max value and restarts at zero). The flag is actually a bit in the TA0CCTL0 register, as you see in the code above, but don't worry about that at all. So all we have to do is wait for it to come on, then do our thing, then wait for it again.
Another benefit of this very simple program is that it makes you feel the timer is somewhere outside the program (which it is). Like all MSP430 peripherals the program runs parallel to, or alongside, them. The flag CCIFG, does NOT interrupt this program, but instead the programs chooses to continually look to see when it gets set as part of its normal execution path. It is the timer that sets CCIFG on, and the program reacts to this. Even though we monitor the interrupt flag there is no interrupt processing in the program. It is important to understand this.
The program is simple, but its also doing only one thing- watching for that CCIFG flag to come on. Normally your program will have several other things to do as well. But I did it that way on purpose. Understanding that the interrupt flag comes on like this, independent of the program, is important first step in understanding interrupt processing later.
In a later blog we will look at how the program can be automatically interrupted whenever the CCIFG flag comes on even if its doing something else at the time. Before that though we will look at how a timer can do loads of other things on its own without even notifying the program at all.
I am going to be using the above code as part of a program to generate varying length pulse streams. I want the the timer CCIFG to come on when its time to change the output high or low. I will let you know how that goes.
Subscribe to:
Comments (Atom)