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 a memory clock using the features provided by Timer1. The clock is updated once every second by the Basic Timer1 interrupt service routine (ISR). This procedure also performs switching of LED1. In order to evaluate the execution time of the routine, LED2 is kept active during the execution of the ISR. When the ISR has completed, the device goes into low power mode, until the new interrupt wakes it up.
Resources
This application ( Lab1_Timers.c) sets Basic Timer1 to generate an interrupt once every second. The interrupt service routine generated by this peripheral is required to update the clock stored in memory. Moreover, it must refresh the content of the clock displayed on the LCD.
Thus, the system resources used by this application are:
- Basic Timer1;
- I/O ports;
- LCD;
- Interrupts;
- Low power modes.
The default configuration of the FLL+ is used, so, all the clock signals required for the operation of the components of the device assume their default values.
Software application organization
The first task is to disable the Watchdog Timer. It should be stated that this feature, when used correctly, makes the application more robust.
The resources needed for the LCD are all configured. This code is given, since its operation will be analysed in a later laboratory. Once the LCD configured, it is cleared by the execution of the routine LCD_all_off().
The memory clock consists of setting three global variables: hour, min, and sec, all of the type unsigned char, used to store the hours, minutes and seconds values elapsed respectively since the beginning of the execution of the application. These variables are initialized with zero values.
The LCD is refreshed at startup to show the initial clock value.
LED1 is used as an indicator of Basic Timer1 ISR execution. The execution time can be determined through it. In addition, LED2 state switches whenever the Basic Timer1 ISR is executed.
The Basic Timer1 is set to generate an interrupt once every second.
The routine main() ends with the interrupts global activation and puts the device in low power mode, awaiting the next interrupt.
Basic Timer1 ISR begins by activating LED2, indicating the beginning of the routine execution and then switches the state of LED1. The counters are updated in cascade and their contents updated on the LCD, through routines LCD_sec(), LCD_min() and LCD_hour(). The routine ends with switching the state of the clock separation points. Finally, LED2 is turned off.
System configuration
Watchdog Timer
The Watchdog Timer is disabled with the objective of reducing energy consumption, but giving up the protection afforded by it. This peripheral is configured by the WDTCTL register. Its access is protected by a password. The value to disable it:
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
FLL+ configuration
A 32.768 kHz crystal is applied to the oscillator LFXT1. Since it is possible to select the internal capacitors using software, the value to write to the FLL_CTL0 configuration register to select the 8 pF capacitors is:
FLL_CTL0 |= XCAP18PF; // Set load cap for 32k xtal
Taking into consideration the change mentioned earlier to the FLL+ module, what are the frequencies of each of the clock signals?
ACLK = _________________;
MCLK = _________________;
SMCLK = ________________;
LED ports configuration
LED1 and LED2 are connected to ports P2.2 and P2.1 respectively. How should they be configured so that just the bits related to these ports have digital output functions?
P2DIR |= 0x06; // P2.2 and P2.1 as output
How should the P2OUT register be configured so that the application starts with LED1 on and LED2 off?
P2OUT |= 0x04; // LED1 on and LED2 off
Basic Timer1 configuration
Basic Timer1 should generate an interrupt once every second. It uses two counters in series, so that the input of the BTCNT2 counter is the output of the BTCNT1 counter divided by 256. The BTCNT1 counter input is the ACLK with a 32.768 kHz frequency. If the selected output of the BTCNT2 counter is divided by 128, what is the time period associated with the Basic Timer1 interrupt? _________
BTCTL = BTDIV | BT_fCLK2_DIV128; // (ACLK/256)/128
IE2 |= BTIE; // Enable Basic Timer1 interrupt
//*********************************************************
// BasicTimer1 Interrupt Service Routine
//*********************************************************
#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer_ISR(void)
{
P2OUT |=0x02; // LED1 turn on
P2OUT ^=0x04; // LED2 toogle
sec++; // increment seconds
LCD_sec(); // refresh seconds field in LCD
if (sec == 60) // one minute was pass
{
sec = 0; // reset seconds counter
min++; // increment minutes
LCD_min(); // refresh minutes field in LCD
if (min == 60) // one hour was pass
{
min = 0; // reset minutes counter
hour++; // increment hours
LCD_min(); // refresh hours field in LCD
if (hour == 24)// one day was pass
{
hour = 0; // reset hours counter
}
}
}
if (sec & 0x01) // toogle clock dots
{
P3_DOT_ON;
P5_DOT_ON;
}
else
{
P3_DOT_OFF;
P5_DOT_OFF;
}
P2OUT &=~0x02; // LED1 turn off
}
Low power modes
The task simply updates the counters periodically and refreshes the LCD contents. It is possible to configure the registers for an energy-efficient operation.
Which low power mode should be used? _____________
Which system clocks are activated in the low power mode selected? _________________
BIS_SR(LPM3_bits + GIE); // Enter LPM3 + interrupts enabled
Analysis of operation
System clocks inspection
The MCLK, SMCLK and ACLK system clocks are available at ports P1.1, P1.4 and P1.5 respectively. These ports are located on the SW2, RESET_CC and VREG_EN lines, which are available on the H2 Header pins 2, 5 and 6. All these resources are available because the Chipcon RF module is not installed and SW2 is not used.
Using the Registers view, set bits 1, 4 and 5 of P1SEL and P1DIR registers, to choose the secondary function of these ports configured as outputs. By connecting an oscilloscope to those lines, it is possible to monitor the clock signals.
What are the values measured for each of the system clocks?
ACLK: ________________
SMCLK: _______________
MCLK: ________________
ISR execution time
The Basic Timer1 ISR execution time is fundamental to energy conservation, in order to extend the life of the system battery. The routine execution time can be measured by connecting the oscilloscope to port P2.1, which controls LED2. This output is available on pin 2 of Header H4.
The execution time of this routine varies with the number of the counter updates and respective updates to the LCD. What are the times measured for each of these situations and what their frequencies?
Seconds update: ______ with a time period of ______
Seconds and minutes update: ______ with a time period of ______
LCD fields update: ______ with a time period of ______
If the developer chooses to update all the LCD fields at each interrupt, the time required is much greater than the solution presented. Efficient programming contributes to a reduction in the system power consumption.
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







Lab1_Timers.c

"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 […]"