...
- All code running on our electrical system is written in C. It would be a good idea to become familiar with C before continuing with this tutorial. You can find many resources online about programming in C such as this one.
- Install code composer studio (CCS)
- Simply make an account on TI website, and after you have completed that find the download link for windows, linux, or mac (depending on what you use) from this site. Just run through the installer (default options should be fine) and you will have code composer studio installed.
- A MSP430 EXP430F5529LP launchpad. (There is some available in the bay to use or you can purchase your own here).
- Read Software 101: Clearing, Toggling, Setting, and Checking a Bit
...
First to understand how timers work it would be a good idea to look at the timer section in the datasheet (Documentation for timer A starts on page 462). Here we can see all the documentation about the registers that interact with timer A on the microcontroller. From this we can read that in order to start the time we need to set some information in the Timer A control register (TACTL), we need to set the source select (which clock to use), the mode control. We also need to enable interrupts globally and enable interrupts for the timer. We have to set the value the timer counts to, and we also need to create an interrupt function.
First to get started, it would probably be a good idea to understand what interrupts are. Interrupts are essentially a special function that will be get called by that hardware in the case of an event. So in our case we're going to create an interrupt that gets called when the hardware timer is done counting to it's maximum value.
Next we need to figure out what values to set in the timer control registers. We can check the datasheet for that. We want to use the source clock "smclock" (this is usually the source clock you want to use for anything) and we want to count in up-down mode. From the datasheet we can see that we want TASSEL_2 for the source select and MC_2 for the mode control. We are also going to divide the clock frequency (To make it count slower) by 8 using ID_3 (input divider 3, which divides the input clock frequency by 8). We can see this all in action in the code below that you can copy and paste into your editor.
Code
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <msp430.h>
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
TACTL |= TASSEL_2 | MC_2 | ID_3;
TACCTL0 |= CCIE;
TACCR0 = 65535;
__enable_interrupt();
P2OUT &= ~BIT0;
P2DIR |= BIT0;
return 0;
}
#pragma vector = PORT2_VECTOR
__interrupt void PORT2_ISR(void) {
P2OUT ^= BIT0;
} |
Here at the bottom in the "#pargma" section we can see the interrupt function we created, this interrupt function is called an "Interrupt Service Routine" orĀ "ISR" for short. All this service routine does is toggle the state of the blinking LED. At the beginning of the main function we can see the timer setup code. First we setup the timer A control register (TACTL) with smclock (TASSEL_2), up-down mode (MC_2), and frequency divider of 8 (ID_3). Next we set the Timer a capture/compare control register (TACCTL0) bit to enable interrupts locally for the timer. After we set the value we will be counting to by setting "TACCR0" to 65535 (the maximum value it can store), this causes the time to start counting. Lastly we globally enable interrupts by calling "__enable_interrupt". Then we configure the IO like before and that's all well need.
You can run the above code and see an LED blink. This method is slightly more complicated but is much better. It allows to run other code while you wait for the LED to blink, that way no time is wasted.
This was a very specific example, if you wanted to learn how to use different hardware on the MSP430, you could easily lookup that specific piece of hardware in the datasheet and see how to use configure and use it properly. The datasheet should be your go to place to find out what the msp430 is capable of, and how to actually do things.