Before going to generating PWM signal on MSP430 Launchpad, I
would like to introduce some difference between the capture and compare modes
of the Timer in MSP430G2553. We know that, there are two timers categories available
In MSP430 family named as Timer_A and Timer_B. Both of these have got their
importance based on its implementation.
- In Timer_B, it is possible to program the bit length like 8-bit, 10-bit, 12-bit, 16-bit, which is available in Timer_A category.
- Some Timers belongs to Timer_B have 7 CCR registers, but the Timer_A contains three capture/compare registers (CCR).
- Timer_B contains double buffered capture/compare registers (CCR).
- Remember MSP430G2553 contains two 16-Timers (Timer_A0 and Timer_A1) which belong to Timer_A category.
Basically these timers may operate
in two modes.
- Capture mode.
- Compare mode.
Whereas the counting modes for these two timers are
- Up mode.
- Down mode.
- Up/down mode.
- Continuous mode.
Ok! Now we will look into capture mode and compare mode.
Capture mode: It
is used to record/capture the time events for speed computations and time
measurement. This will be very useful Robotics applications like Frequency
measurement of a signal, motor speed calculations, degree of freedom in robotic
arms, etc.,
Compare mode: It
is used to generate Specific time periods which replaces the delay loops in
software. By doing so, the timer allows CPU to sleep by consuming less power.
It is also used to generate PWM signals which is more suitable in applications
like MOTOR driving, LED display intensity control, etc.,
The Timer_A in MSP430G2553 contains mainly two sections as
shown in the figure 1.1
- Timer Block
- Capture/compare block
Timer Block is mainly responsible for the basic configurations
of the Timer like count mode selection, clock source selection, etc., for basic
time intervals generation the user may simply concentrate on this block and can
easily start his programming on timers. Here I am not going to discuss about
the Timer block in details rather I will concentrate on Capture/compare block.
I encouraged you read my tutorial about “UNDERSTANDING TIMERS IN MSP430 LAUNCHPAD” .Capture/compare Block contains configuration settings for
capture mode and compare mode operations. There are three capture/compare
blocks (CCR0, CCR1, and CCR2) available in TIMER_A in MSP430G2553.
![]() |
| Figure 1.1 shows the basic Timer Block and three capture/compare blocks (CCR0, CCR1 and CCR2 respectively). |
Capture mode:
- If CAP = 1, capture mode is selected.
- CCIxA and CCIxB are the input pins for capturing an event.
- CCISx bits are used to select one of the capture input pins.
- There are three types of triggering of capture input pins (raising edge, falling edge, both) and these can selected by using CMx bits.
- If capture occurs, Timer value is copied into the TACCRx register and the interrupt flag CCIFG is set automatically.
NOTE: The input
level signal can be read at any time via the CCI bit. Therefore the capture
signal is asynchronous to the timer clock and cause race condition. Setting the
SCS bit will sync the capture with the next timer clock.
Compare mode:
- Each capture/compare block contains an output unit and it is used to generate output signals such as PWM signals.
- To select Compare mode, CAP should be ‘0’ (CAP = 0).
- When the count value in the ‘TAR’ register reaches the value in a TACCRx, then, CCIFG interrupt flag is set.
- Internal signal EQUx = 1, the EQUx affects the output according to the output mode.
- Each output unit has eight operating modes as shown in the figure 1.2 that generate signals based on the
EQU0 and EQUx signals and it can be selected by using
OUTMODx.
![]() |
| Figure 1.2 shows that eight operating modes of output unit and its corresponding OUTMODx bits. |
Figure 1.3 shows that the generation of different output
signals by configuring the output unit in different modes and the timer
operates in UP mode.
![]() |
| FIGURE 1.3 shows different output signals by configuring output modes in UP mode operation of the timer. |
Now we will switch into the program to generate the PWM
output on P1.6 pin of the MSP430 Launch pad, so that green LED will flash
according to duty cycle provided in the program. Here the Timer0 is source from
ACLK which is sourced from VLO (12 KHz) oscillator.
Step
1: Stop the watch dog timer, to avoid unnecessary resets of the
microcontroller. We will discuss more
on watch dog timer later.
WDTCTL = WDTPW + WDTHOLD; // stop
watch dog timer
Step
2: make cpu to run with MCLK from DCO source. You can use any clock to drive
cpu.
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
Step
3: BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO
oscillator is selected.
BCSCTL3 |= LFXT1S_2;
Step
4: Configure the MCLK and SMCLK by setting
proper bits BCSCTL2. SELM_0 -- selects MCLK
source from DCOCLK, DIVM_3 -->
MCLK = DCO/8,
We should remember that "Up
on reset, MCLK and SMCLK are source from 1.1MHz DCO
clock source" DIVS_3
--> SMCLK = DCO/8.
BCSCTL2 |= SELM_0 + DIVM_3 +
DIVS_3;
Step
5: Configure the GPIO port to blink the
LED according to the PWM output signal. In MSP430G2553
port pins has multiple
functionalities (like GPIO, UART, Timers, PWM, SPI, I2C, etc.,). To choose
a
special functionality out of these many functionalities of a pin, there is a
special function registers
P1SEL and P1SEL2 as shown in figure 1.4. Here we are
using P1.6 (green LED) which has
“P1.6/TA0.1/UCB0SOMI/UCB0SCL/A6/CA6/TDI/TCLK” and we have to use P1.6 as
TA0.1. To do this, make the 6th bit of Port1 in P1SEL as ‘1’ and in P1SEL2
as ‘0’ as shown in
figure 1.4.
![]() |
| Figure 1.4 shows port pin multiplexing capability and its selection. |
P1SEL2 |= ~BIT6;
P1SEL |= BIT6; // TA1 option is
select, P1SEL2 and P1SEL combination is ‘01’
Make the direction of the bit6 as output.
P1DIR |= BIT6; // MAKE P1.6
(TA0.1) as output port
Step
6: Configure the TimerA to operate with ACLK which is source from VLO (12KHz) -
(TASSEL = 1)
and in up mode (MC = 1). TACLR
bit clears the TAR register, clock divider and count direction.
TACTL = TASSEL_1 + MC_1 + TACLR;
Load the count 12000 in counter
compare register to generate 1 second delay (12000 * T) = 12000
* (1/12000) =
1sec; T = 1/F = 1/12 kHz. This count define the PWM period (Ton + Toff).
CCR0 = 12000; // PWM period.
CCTL1
= OUTMOD_7; // RESET/SET
CCR1
= 6000-1; // 50% duty cycle (12000/6000
= 0.5).
And
the PWM waveform will generate depend upon the duty cycle as shown in the
figure 1.5
![]() |
| Figure 1.5 shows the PWM waveform based on the duty cycle using CCR0 and CCR1 registers. |
Step 7: use infinite loop or Lower
power mode (LPM). We haven’t discussed about the low power mode,
so I use
infinite loop even though it wastages the CPU time.
While (1);
code:
#includevoid Config_TimerA(void); void Config_GPIO(void); void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog timer BCSCTL1 = CALBC1_1MHZ; // make cpu to run with MCLK from DCO source DCOCTL = CALDCO_1MHZ; BCSCTL3 |= LFXT1S_2; // BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is selected //In the basic clock system by default OFIFG (oscillator-fault flag) bit is set and force the MCLK to use DCO as its source. //IFG1 &= ~OFIFG; // Clear OSCFault flag not used here BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3; // SELM_0 -- selects MCLK source from DCOCLK, DIVM_3 --> MCLK = DCO/8, // We should remember that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO clock source" //DIVS_3 --> SMCLK = DCO/8 Config_GPIO(); Config_TimerA(); while(1); } void Config_GPIO(void) { P1DIR |= BIT6; // MAKE P1.0 and P1.2 (TA0.1) as output port P1SEL |= BIT6; // TA1 option } void Config_TimerA(void) { // Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - (TASSEL = 1) and in up mode (MC = 1) TACTL = TASSEL_1 + MC_1 + TACLR; CCR0 = 1000-1; // PWM Period CCTL1 = OUTMOD_7;// Reset/set CCR1 = 200-1; // 50% Duty cycle }
8:09 AM
Lovakiranvarma Myla











