Introduction
Correct system timing is a fundamental requirement for the proper operation of a real-time application. The timing definition can dictate how the data information processed during the execution of the application program. The clock implementations vary between devices in the MSP430 family. Each device provides different clock sources, controls and uses. This chapter discusses the clock controls included in the platforms used.
The MSP430 4xx family has two general-purpose 16-bit or 8-bit counters and event timers, named Timer_A, Timer_B, and a Basic Timer. The Basic Timer module is only implemented in ‘4xx devices. The 2xx device family also has Timer_A and Timer_B, but the clock signals are provided by the basic clock module+.
The timers may receive an internal or external clock. Timer_A and Timer_B also include multiple independent capture and compare blocks, with interrupt capabilities.
Overview
This laboratory implements an application ( Lab5_Timers.c ) designed to measure a PWM signal frequency. If a signal generator is not available, the microcontroller generates a PWM signal based on the frequencies stored in a file. The frequencies generated are read and updated with a fixed time period using the features of CCE. The measured value is shown on the LCD in Hz.
Resources
The module FLL+ is configured to a frequency of 7.995392 MHz for the MCLK and SMCLK clock signals. This application performs the two tasks simultaneously.
On the one hand, it generates a PWM signal with a frequency of 200 Hz and a duty cycle of 50%. Alternatively, the PWM signal frequency can be read from a file using a breakpoint. This function is performed by Timer_B, using the compare unit to generate the PWM signal.
The time period between two consecutive PWM signals low-to-high transitions is measured by Timer_A. The capture unit of this timer is configured to collect the Timer_A counter register’s contents when a PWM signal low-to-high transition is detected at its input.
The Basic Timer1 generates an interrupt once every second. The ISR updates the PWM signal frequency generated by the Timer_B. If you choose to use this feature, a breakpoint associated with this ISR execution allows reading a file with the value of the frequency that will be generated.
The microcontroller’s ports are configured in order that the PWM signal generated by Timer_B through the TBCCR4 compare unit available at Port P3.5/TB4 can be connected to the Port P1.2/TA1 of the Timer_A TACCR1 capture unit. If you plan to use this feature, these pins must be connected together. Port P3.5 pin is available on Header 7 pin 6, while the Port P1.2 pin is available on Header H2 pin 3.
Ports P2.1 and P2.2 are used to monitor the state of the LED2 and LED1, respectively.
The resources used by the application are:
- Timer_A;
- Timer_B;
- Basic Timer1;
- I/O ports;
- FLL+;
- Interrupts.
Software application organization
The software structure allows various tasks to be performed simultaneously. The routine main() is responsible for configuring all the resources used by the application. Once started, the application enables all the interrupts and waits for an interrupt request.
There are two routines that separately service the two possible interrupts. The routine TimerA1_ISR() services interrupts required by the Timer_A overflow and by the TACCR1 capture unit. For every interrupt caused by a TACCR1 capture, the value collected in the TACCR1 register is stored in T1, if it is the first low-to-high transition, or stored in T2 if it is the second low-to-high transition. This sequence is controlled by the variable capture. The variable flag is used to flag the measurement process. This process occurs between the capture of the first low-to-high transition and the second transition. The counting of clock pulses is done by Timer_A, in the time interval between the T1 and T2 acquisition, assigned to the variable T. The process is synchronized when Timer_A overflows, restarting the measurement process. The LCD is refreshed once every 0.5 seconds with the latest measured frequency value, using the control variable control tick that corresponds to 0.5 seconds.
The routine basic_timer_ISR() services the interrupt produced by Basic Timer1 once every second. This routine begins by switching the state of LED1 and LED2. In addition, it updates the Timer_B counting period. The variable read_data allows the counting period to be changed.
System configuration
Basic Timer1
Basic Timer1 generates an interrupt once every second. Use the two counters in series, where the BTCNT2 counter input is selected as the BTCNT1 counter output divided by 256. The BTCNT1 counter input is the ACLK clock signal with a frequency of 32.768 kHz.
If BTCNT2 counter selected output is divided by 128, what is the time period associated with the Basic Timer1 interrupt? _________
The values written to the configuration registers are:
BTCTL = BTDIV | BT_fCLK2_DIV128; // (ACLK/256)/128
IE2 |= BTIE; // Enable BT interrupt with 1 sec period
Timer_B
The TBCCR4 compare unit is used to generate the PWM signal. The set/reset compare mode is used.
The values written to the configuration registers are:
TBCTL = TBSSEL_2 | CNTL_0 | TBCLGRP_0 |MC_1 | ID_0;
// SMCLK, continuous mode
TBCCTL4 = OUTMOD_3; // CCR4 output mode 3 (set/reset)
The TB4 PWM output signal has a frequency X, with a 50% duty-cycle. The SMCLK clock signal is used as input of Timer_B.
The values written to the configuration registers are:
TBCCR0 = 39977;// Output 200 Hz signal with 50% duty cycle
TBCCR4 = TBCCR0/2;
What the largest and lowest generated frequency?
Maximum frequency value: ____________
Minimum frequency value: _____________
Timer_A
Timer_A is sourced by the SMCLK clock signal. It counts to the value 0xFFFF, in continuous mode. An interrupt is generated when the TAR counter overflows. What is the value to write to its configuration register?
TACTL = TASSEL_2 |MC_2 | ID_0 | TAIE; // SMCLK
// up mode to 0xFFFF
The capture unit captures the TAR register value to the TACCR1 register when it detects a low-to-high transition at the TA1 input. What is the value to write to the configuration register?
TACCTL1 = CCIS_0 | CAP | CCIE;
// Capture on rising edge,
// TACCR1 input signal selected,
// Capture mode,
// Capture/compare interrupt enable.
Determine the maximum and minimum frequency values detected. Note that these values do not take into account the execution time of the application. The PWM signals should be applied at frequencies well below the maximum value determined.
Maximum frequency value: ____________
Minimum frequency value: _____________
The TACCR1 capture unit is configured to generate an interrupt when it detects a low-to-high transition. What is the value to write to the configuration register?
TACCTL1 |= CM1
Ports P3.5/TB4 and P1.2/TA1 configuration
These ports perform special functions. Thus, the Port P3.5 is configured as an output, selected by the special function TB4, with the values:
// TB4 configuration (Port3)
P3SEL = 0x20; // P3.5 as special function (TB4)
P3DIR = 0x20; // P3.5 as output
The Port P1.2 is configured as input, with the special function TA1, using the values:
// TA1 (TACCR1) configuration (Port1)
P1SEL = 0x04; // P1.2 as special function (TA1)
P1DIR = 0x00; // P1.2 as input
I/O Ports configuration:
// SW1 and SW2 configuration (Port1)
P1SEL &= 0x00; // P1.0 and P1.2 I/O
P1DIR &= 0x00; // P1.0 and P1.2 as inputs
P1IFG = 0x00;
P1IES &= 0xFF // high-to-low transition interrupt
P1IE |= 0xFF; // enable port interrupts
// LED1 and LED2 configuration (Port2):
P2DIR |= 0x06; // P2.2 and P2.1 as outputs
P2OUT = 0x04; // LED1 on and LED2 off
// Buzzer port configuration (Port3)
P3SEL |= 0x20; // P3.5 as special function
P3DIR |= 0x20; // P3.5 as digital output
Analysis of operation
Run the application using the frequency generator based on Timer_B
Without a frequency generator, the Timer_B generates a PWM signal at the TBCCR4 unit output that can be fed back to Timer_A TACCR1 capture unit input. These two pins must therefore be connected together. By default, the PWM signal frequency is 200 Hz. Add a breakpoint at the line of code belonging to the Basic Timer1 ISR to modify this value.
TBCCR0 = 7995392/read_data;
If the variable read_data has the value 200, it will generate a 200 Hz frequency. The value of this variable can be changed by associating a breakpoint to that line of code. Before the line of code is executed, the value of the data file is read and assigned to the variable read_data. The signal will oscillate at the desired frequency, loading the value in TBCCR0. The breakpoint configuration is as follows:
- Action: read data from file
- File: address of the data file (example in freq.txt)
- Wrap Around: activate this option to restart reading at the beginning
- Start address: &read_data
- Length: 1 in order to read a value from the file each time
Run the application using a frequency generator
The operation of the application can be verified using a frequency generator. The generator should generate a PWM signal with voltage and frequency values compatible with the device’s input (2.5 to 3.3 volts).
Observe the measured frequency
The PWM signal applied to the TA1 input can be viewed using an oscilloscope, connected to pin 3 of Header 2. Perform this task and confirm the values present at the LCD.
Measurement of electrical current drawn
The power consumption was discussed in the previous point. The electrical power required by the system during operation is measured by replacing the jumper on the Header PWR1 by an ammeter, which indicates the electric current taken by device during operation.
What is the value read? __________
This example and many others are available on the MSP430 Teaching ROM.
Request this ROM, and our other Teaching Materials here https://www-a.ti.com/apps/dspuniv/teaching_rom_request.asp









"This is an excerpt from the MSP430 Teaching CD produced under TI sponsorship and review at the University Beira Interior in Portugal. The material covers everything from "hello world" on an eZ430 […]"