...
You can copy and paste this code into the main.c file of your project to get started, and then you can run it by hitting the debug button () and then the run button () (a dialog box might appear telling you to update firmware, just click okay and let it update). The code should begin to run on the microcontroller and you will see the LED begin to blink, congratulations! Now lets go over the rest of the code that has not been explained yet, and figure out how and why it works.
This first line of code in the program is:
Code Block | ||||
---|---|---|---|---|
| ||||
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer |
This line has the important function of disabling the watchdog timer. The watchdog timer is a special timer on the microcontroller that will restart the microcontroller if it counts to a certain defined value without the timer being reset. This is useful to reset the running program in the even that it gets stuck in some kind of infinite loop. For know the watchdog timer will just be confusing to us so we will disable it. To figure out what the register WDTCTL does when you set the bits WDTPW & WDTHOLD, we can look at the datasheet for the MSP430x5xx family. Searching for the section on the watchdog timer we can now find the WDTCTL register section on page 461. From here we can see that WDTHOLD stops the watchdog timer from running and WDTPW is the password we need to use to modify the watchdog timer.
I've already explained the P2OUT and P2DIR registers so will skip over them and go to the for loop. In the for loop we are toggling BIT0 using the xor operator (The software 101 guide about bits in prerequisite section explains how this works) and then we call a special function "__delay_cycles"
Code Block | ||||
---|---|---|---|---|
| ||||
__delay_cycles(10000); |
What this function does is that it delays the processor for x cycles (10,000 in our case). So whats happening in our loop is we toggle the state of the LED (if its on it turns off, and if its off it turns on) and then we wait for a little bit. This creates the effect of the LED blinking. Now this is the inefficient way to blink an LED, there is a much better way to blink an LED.
Blink an LED with timers
The issue with the above method of blinking an LED is the "__delay_cycles" function. When this function is running, we aren't doing anything else. This is wasted time that we could be using to compute other things on the microcontroller. Luckily the MSP430 provides a way to run a timer in the background while other code is running. To get started, create a new project like before but this time name it "TestTimerBlink". Now the code we are going to use is very similar but will work slightly different.
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, and the
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;
P2OUT &= ~BIT0;
P2DIR |= BIT0;
return 0;
}
#pragma vector = PORT2_VECTOR
__interrupt void PORT2_ISR(void) {
P2OUT ^= BIT0;
} |