Understanding MSP430 Timer_A
Timer_A is a 16-bit timer/counter. We can understand some features of the Timer_A using a simple code. The Timer_A has a register TAR which holds a the current count of the timer. There are 3 different modes of counting in Timer_A. One will count up to ffffh and again start from 0, other one count up to a defined value and again starts from 0 and the last one counts up to a defined value and counts back to 0.
- Timer Control Register
These are the 8th and 9th bits of the register. These two bits select the source of the clock for the timer. Subsystem master clock(SMCLK) is used in this situation. This is the clock provider for all the peripherals. To select the SMCLK the 9th and 8th bit must be 1 and 0 respectively.
These are the 6th and 7th bits of the register. These two bits select which clock frequency must be taken, whether to take the same SMCLK or divide it by 2, 4 or 8.
These are the 4th and 5th bits in the register. The selects the counting nature of the timer. In this case we select a continuous mode i.e. the count will reach up to ffffh and then overflows to 0 and the count restart. To select this mode 4th and 5th bits must be 0 and 1 respectively.
This is the second bit. Setting this bit resets the TAR, the clock divider and the count direction.
0th bit of the register. On overflow of the timer this bit is automatically set.
The following code try to blink between red and green led on overflow of the clock. Between two overflow there is a delay and it is used to light a led. Code has two functions init and delay. init initializes all timer and delay causes the delay.
The watchdog timer must be disabled otherwise there may be frequent resetting of the device.
The init function:
This function do all the initializations. The first step will disables the watchdog timer. The second step will make configure the two led’s as output pin. The third step will select the clock source, input divider, mode control and timer clear. All the uppercase words are macros. MC_2 will select the continuous up mode. If the macro was MC_1, the selected mode will be up mode. In this case what the macro do is, it will left shift the digit 2 by 4 position making the 5th and 4th bit 1 and 0 respectively. It’s similar in case of ID_0 and TASSEL_2.
This function produces the delay. It checks when the count overflows i.e. when the 0th bit is set. Until the a busy looping is done. Once the overflow occurs, the control comes out of the loop and manually reset the 0th bit to 0.
This while loop is an infinite loop. First a delay is called and then the red led blinks and again we call a delay. For that delay time the red led remains and after that delay the green led blinks and red sleeps. This process is continued in an infinite loop.