In the second tutorial, we have introduced timers and have shown that by using a timer and its interrupt function. This tutorial will do ths same thing by using a timer configured in PWM.

 About PWM

PWM stands for Pulse Width Modulation. We are going to configure a timer so that we don't need an interrupt function anymore. The timer will run by itsef and will be set once for all. The processor will sleep forever.

Timer PWM feature



MSP430's timers have an interesting feature: they have multiple registers that can be configured in many ways.The timers have also output pins that can be used to output the timer's status.

Let's take an example. Suppose that we configure the timer so that it counts up to TA0CCR0. This is done with as explained in tutorial 2. Now we can configure another register TA0CCR1 with a value between 0 and TA0CCR0. For example TA0CCR1 = 16383.

Then, there are many possible output configurations as can be seen on the right side of the picture. In this example we will choose output mode 7. In this case, the output pin starts high, and when the counter reaches TA0CCR1, it switches to low.


What will happen is that the timer output will start high, become low at 16384, and become high again when the counter rolls back to 0. The result will be a square wave with a ratio of 50%.

The program

 We use the same utilities as previously (F5659Utils.h).

The program is as follows.


#include <msp430F5659.h>
#include "F5659Utils.h"

int main(void) {
    WDTCTL = WDTPW + WDTHOLD;            //    Stop WDT
    SetCoreVoltage(VCORE19);             //    Set the core to 1.9V
    SetFLL(24);                          //    Set SMCLK to 24 MHz
    P1DIR |= 0x04;                       //    P1.2 output
    P1SEL |= 0x04;                       //    P1.2 as timer output
    TA0CCR0 = 32768-1;                   //    PWM Period
    TA0CCTL1 = OUTMOD_7;                 //    CCR1 reset/set
    TA0CCR1 = 16384;                     //    CCR1 PWM duty cycle
    TA0CTL = TASSEL_1 + MC_1 + TACLR;    //    SMCLK, up mode, clear TAR
    __bis_SR_register(LPM0_bits);        //    Enter LPM0(sleep)


The result is the same as follows:











As we can see, there are some advantages with this method:

  • The program became extremely simple
  • We don't need an interrupt function
  • The processor does not work at all and therefore can be used for other tasks
  • It could be possible to change parameters on the fly

About this latter advantage, we are going to introduce AD (analog to digital) conversion in the next chapter, and we will try to blink the LED differently according to an input voltage.

Download the C code

A summary of the first tutorials has been added as a downloadable zip file here.