PWM_test.c
file (Canvas, but different from PCB files)For Lab 2, you learned how to switch the timer interrupt from the DCO to the VLO by, in part, changing its source from the SMCLK to the ACLK. As discussed in class, the primary advantage of the VLO is much lower power consumption than the DCO. It also is lower frequency, which can be convenient if you want infrequent timer interrupts.
LPM4 consumes very little power. I noticed, playing around with my Lab 2 pendant, that if I removed the battery while in LPM4, the capacitory between power and ground held enough charge to keep the device functioning (in LPM4) for at least a few seconds. To see this yourself, remember that on first power up, the pendant will always start with LED1 illluminated. So start it going, and then go into LPM4 when the next LED will not be LED1. Remove the battery, wait a while, then put the battery back and push the button again. You’ll see that it will continue on where it left off. If you push the button while it’s asleep, you’ll rapidly discharge the capacitor and when you put the battery back in, it will start with LED1 again.
In Lab 2, you used the timer interrupt to wake up the CPU periodically to control which LED was lit on your PCB. For Labs 3 – 5, we want to generate PWM signals, which will end up potentially using both of the TimerA modules on the MSP430G2553. There is a third, easily forgogtten, timer module on the MSP430G2553, though. Before you look in the skeleton code for Lab 3, see if you can remember what it is.
Now take a look at the skeleton code. This program follows the same code pattern as in Lab 2. Succinclty, this pattern involves a main loop which contains the program logic. At the end of each main loop cycle, the device goes into LPM3, and then the timer interrupt wakes it back up.
__bic_SR_register_on_exit(LPM3_bits);
? What
would be the result to the program if we replaced this with __bic_SR_register(LPM3_bits);
?To change the brightness of an LED (or most other analog devices), we use PWM. This essentially changes the brightness by altering the duty cycle of the output signal. We could do this manually – i.e., write code using the timer interrupts that makes the appropriate output bit high or low for the apropriate amount of time. However, many microcontrollers include special hardware that allows for this to be done automatically. In the MSP430G2553, the two TimerA modules can drive up to 3 pins each with PWM signals, with the limitation that the fundamental frequency for the 3 pins must be the same – the module’s selected clock frequency. Because PWM is a useful function for controlling intensity, the Timer modules are designed so that this constraint will still let a variety of pulse patterns to be generated. Hence the various modes (continuous, up, up/down).
The TimerA module allows us to set various pins to be on for a fraction of the timer counter period, creating a PWM signal when it is pulsed fast enough. These pins are depicted in the device datasheet.
Write some code to toss all this together. Source Timer A1 off the VLO clock, leaving it at its default frequency. Configure your registers to use Up mode and so that the duty cycle of the LED input signal is 50%, and make sure to enable the LPM found in b). Since the PWM signals are automatically generated, you should not need an ISR.
For the lab demo, combine the PWM code you just wrote with the skeleton code and run it on your Pendant from Lab 2. Your goal is for LED3 and LED7 to change intensity (duty cycle) from 0 to 100% and back to 0% in 5% steps each time the WDT interrupts. They should move in opposite directions, one getting brighter and one getting dimmer. Hint: You should be able to accomplish this by changing the value of the TA0CCR1 and TA1CCR1 registers. Demo this functionality and turn in your code.