I have recently been turned on to the world of microcontrollers while doing some hobby development. Here is the first “real” program I have written with the Texas Instruments MSP430 LaunchPad development tool. Check it out…$4.30 to get a development board with 2 devices and a USB connection to your Windows PC for programming and debugging. It’s a lot of fun…especially if you enjoy bit-twiddling!
The MSP430 processors specialize in low power operations. They accomplish this by enabling peripherals to run that require minimal resources. These peripherals have the ability to “wake up” the CPU (in less than 1 micro-second) to service requests through interrupts. The software handles the interrupt and go back to sleep as quickly as possible.
My goal is to create an electrical signal with known period and cycle time (% of the period where the signal is “on”). This is essentially pulse-width modulation, the magic behind many kinds of variable speed electric motors as well as dimming light switches.
As I was reading through the processor data sheet to try to figure out how to generate this signal, I discovered the Timer_A peripheral on these processors can output a signal without code intervention. Very cool. This means no software interrupt service routines…the program configures the clocks and enters the low power mode where the CPU and all the clocks are off except a single timer running on the internal low frequency oscillator.
Here is the code:
#include <msp430g2553.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 &= ~XTS; // LFXTCLK 0:Low Freq.
BCSCTL3 |= LFXT1S_2; // Mode 2 for LFXT1 : VLO
TA0CTL = TASSEL_1 // Timer A clock source select: 1 - ACLK
+ MC_1; // Timer A mode control: 1 - Up to CCR0
// VLO is running at 12 kHz
TA0CCR0 = 12000; // the number of counts in the entire period
TA0CCR1 = 6000; // the number of counts the output signal is set
TA0CCTL1 |= OUTMOD_7; // PWM output mode: 7 - PWM reset/set
// select P1.6 function as TA0.1
P1SEL |= BIT6;
P1SEL2 &= BIT6;
P1DIR |= BIT6; // P1.6 to output
while(1)
{
LPM3; // Enter Low Power Mode 3 - CPU Off, MCLK Off, SMCLK Off, DCO Off, ACLK On
}
}
Code without fear!
--Don