Guide Timer MSP430
Guide Timer MSP430
The timer modules on MSP430 ultra-low power microcontrollers often base several different outputs off
of a single time base a single timer period. This is especially true for the typical implementation of pulse
width modulation (PWM) signals on the MSP430 devices, where one capture compare register (TxCCR0)
sets the period, and the rest (TxCCRx) simply set different duty cycles. However, in some applications,
multiple time bases are needed to generate multiple output frequencies. Normally this is done using
multiple timer modules, but this might require upgrading to a part with many more extra features that may
not be needed for the application. However, in exchange for a small amount of software overhead,
multiple time bases can be implemented on a single MSP430 timer module, allowing for more features to
be implemented on a simpler MSP430 device. This potentially can reduce cost and board space by
allowing a smaller MSP430 device to be used and, on large MSP430 devices, allows for new applications
requiring a large number of different output frequencies to be implemented.
The source code and other files described in this application report can be downloaded from
http://www.ti.com/lit/zip/slaa513.
1
2
3
4
5
6
Contents
Typical Single Time Base Method ........................................................................................ 2
Multiple Time Base Method ................................................................................................ 3
Implementing the Multiple Time Base Method in a Custom Application ............................................. 4
Example Code ............................................................................................................... 5
Limitations of the Multiple Time Base Method .......................................................................... 8
References ................................................................................................................. 12
List of Figures
.........................................................................................
..........................................................
11
List of Tables
1
www.ti.com
0FFFFh
TxCCR0
0h
Timer
Intervals
(TxCCR0)
t0
t0
t0
t0
t0
t0
t0
www.ti.com
TxCCR1b
TxCCR1d
TxCCR0e
TxCCR1c
TxCCR1e
0FFFFh
TxCCR0f
TxCCR0c
TxCCR1a
TxCCR0a
0h
Timer
Intervals
(TxCCR0)
t0
Timer
Intervals
(TxCCR1)
t0
t1
t0
t1
t0
t1
t0
t1
www.ti.com
3.1
3.2
1
100 %
nperiod
(1)
ftimer
fdesired
(2)
Example Code
www.ti.com
3.3
(Duty cycle %) n
period
100 %
(3)
Figure 3 shows how the TxCCRx values and nhigh and nlow are used when implementing two independent
PWM frequencies and duty cycles on a single MSP430 timer module. Compare this to Figure 2, where
constant values were added to TxCCR0 and TxCCR1 to produce a 50% duty cycle.
TxCCR1b
TxCCR0b
TxCCR0e
TxCCR1f
TxCCR1d
0FFFFh
TxCCR0c
TxCCR0a
TxCCR0f
TxCCR1e
TxCCR0d
TxCCR1c
TxCCR1g
TxCCR1a
0h
Timer
Intervals
(TxCCR0)
n0high
Timer
Intervals
(TxCCR1)
n1high
n0high
n0low
n1low
n1high
n1low
n0high
n0low
n1high
n1low
n1high
Example Code
The zip file at http://www.ti.com/lit/zip/slaa513 contains example code for implementing multiple time
bases on a Timer_A module on an MSP430G2452 device and a Timer_B module on an MSP430F5529
device. Variations of the same code were used in testing to produce the data found in Section 5.
4.1
Method
In addition to setting up clocks, pins, and other standard MSP430 initialization, there are two main pieces
of code that need to be implemented for the multiple time base method the timer initialization and the
timer ISRs. The timer initialization is essentially the same no matter what frequencies you are trying to
implement. The following code shows the timer initialization from the file multi_freq_g2452_example.c:
TACCTL0
TACCTL1
TACCTL2
TACTL =
= OUTMOD_4 + CCIE;
= OUTMOD_4 + CCIE;
= OUTMOD_4 + CCIE;
TASSEL_2 + MC_2 + TAIE;
//
//
//
//
Example Code
www.ti.com
The only things that change in this initialization process, depending on the particular application, are the
number of TxCCTLx registers used (which should be set according to the desired number of output
frequencies) and the TxSSELx bits in the TxCTL register (which should be set according to the desired
timer clock source).
4.1.1
// reload period
This example shows the ISRs for generating 5-kHz, 2.5-kHz, and 1-kHz signals from a 1-MHz timer clock
source (see Equation 4).
f0 =
f1 =
f2 =
4.1.2
1MHz
(2 100 )
1MHz
(2 200 )
1MHz
(2 500 )
= 5 kHz
= 2.5 kHz
= 1kHz
(4)
Example Code
www.ti.com
// 75% low
In this case, the code generates the same frequencies of 5-kHz, 2.5-kHz, and 1-kHz signals from a 1-MHz
timer clock source. However, this time the signals have 25%, 12.5%, and 60% duty cycles respectively
(see Equation 5).
f0 =
f1 =
f2 =
1 MHz
(50 + 150 )
1 MHz
(50 + 350 )
= 5 kHz
DutyCycle0 =
= 2.5 kHz
DutyCycle1 =
= 1 kHz
DutyCycle2 =
1 MHz
(600 + 400 )
50
100% = 25%
(50 + 150 )
50
(50 + 350 )
100% = 12.5%
600
(600 + 400 )
100% = 60%
(5)
4.2
www.ti.com
5.1
ISR Overhead
In the single time base method of producing PWMs, the TxCCRx registers do not need to be reloaded and
the output is produced automatically without ever having to enter an ISR. This means that after the initial
timer configuration there is no software overhead, because everything is handled in hardware. For the
multiple time base method, an ISR is entered at the end of each high and low period, adding some
software overhead.
While the ISRs in the code examples are kept to the smallest length possible, the required operations
which add to the software overhead include:
Wake-up time if coming from a low-power mode
ISR entry time
Decision logic for which TxCCRx triggered the interrupt
Decision logic for what part of the cycle (high/low) just finished (when implementing PWMs only)
Reloading the TxCCRx register by adding the appropriate period value
This overhead increases for each signal generated, as this adds to the percentage of time spent in the
ISR. Problems occur if the number of cycles spent in the ISR is too great compared to the cycles between
ISRs (which is set by your period count value), preventing interrupts from being serviced in a timely
manner. Therefore, there is a cutoff point that varies depending on the number of TACCRx registers being
used due to the latency added by the ISR code. For a particular number of signals being generated this
cutoff point, in terms of number of cycles when the timer clock is being sourced from MCLK, remains
essentially constant across MCLK frequencies. This is because the value is an indicator of the ratio
between ISR cycles and cycles between interrupts. Figure 4 shows the constant relationship of the
minimum period count yielding reliable signals versus MCLK. This results in turn in a linear relationship
between the maximum output frequency and MCLK, as seen in Figure 5.
www.ti.com
160
140
120
100
80
60
40
20
0
0
10
15
20
MCLK (MHz)
The data in Figure 4 was generated from testing on an MSP430G2452 producing three signals of the same period
(worst-case).
The data points in Figure 4 and Figure 5 can also be found in Table 1.
This data is provided only as a general guideline for the required timer source frequency to produce the desired
output frequencies, and should not be regarded as a data sheet specification. Other factors like the other interrupts in
the system, the construction of the ISR code, or different frequency and duty cycle combinations may affect the
results in a particular application.
120
100
80
60
40
20
0
0
10
15
20
MCLK (MHz)
A
The data in Figure 5 was generated from testing on an MSP430G2452 producing three signals of the same period
(worst-case).
The data points in Figure 4 and Figure 5 can also be found in Table 1.
This data is provided only as a general guideline for the required timer source frequency to produce the desired
output frequencies, and should not be regarded as a data sheet specification. Other factors like the other interrupts in
the system, the construction of the ISR code, or different frequency and duty cycle combinations may affect the
results in a particular application.
www.ti.com
(1)
(2)
5.2
MCLK
(MHz)
Maximum Frequency
(kHz)
150
6.67
150
53.33
12
150
80
16
150
106.67
Period Counts
500
400
Minimum Period Count F5529
300
Minimum Period Count G2452
200
100
0
0
The data in Figure 6 was generated from testing on an MSP430G2452 and an MSP430F5529 producing signals of
the same period.
The data points in Figure 6 and Figure 7 can also be found in Table 2.
This data is provided only as a general guideline for the required timer source frequency to produce the desired
output frequencies, and should not be regarded as a data sheet specification. Other factors like the other interrupts in
the system, the construction of the ISR code, or different frequency and duty cycle combinations may affect the
results in a particular application.
10
www.ti.com
25
20
Maximum Frequency (kHz) F5529
15
10
5
0
0
The data in Figure 7was generated from testing on an MSP430G2452 and an MSP430F5529 producing signals of the
same period.
The data points in Figure 6 and Figure 7 can also be found in Table 2.
This data is provided only as a general guideline for the required timer source frequency to produce the desired
output frequencies, and should not be regarded as a data sheet specification. Other factors like the other interrupts in
the system, the construction of the ISR code, or different frequency and duty cycle combinations may affect the
results in a particular application.
Output frequencies are based on a 1-MHz MCLK. The maximum output frequencies scale directly with MCLK.
(1)
(2)
(3)
5.3
MSP430F5529 Timer_B
MSP430G2452 Timer_A
Minimum Period
Count
Maximum
Frequency (3)
(kHz)
Minimum Period
Count
Maximum
Frequency (3)
(kHz)
50
100
20
50
20
10
100
10
3
4
170
5.882
150
6.667
240
4.167
320
3.125
410
2.439
500
The data in Table 2 was generated from testing on an MSP430G2452 and an MSP430F5529
producing signals of the same period.
This data is provided only as a general guideline for the required timer source frequency to produce the
desired output frequencies, and should not be regarded as a data sheet specification. Other factors like
the other interrupts in the system, the construction of the ISR code, or different frequency and duty
cycle combinations may affect the results in a particular application.
Maximum frequency with a 1-MHz MCLK. The maximum frequencies scale directly with MCLK. For
example, maximum frequency at 16-MHz MCLK = 16 x maximum frequency at 1-MHz MCLK
Power Consumption
Implementing multiple frequencies on a single timer module can result in higher current consumption than
using multiple timer modules. This is because the multiple time base method requires the use of timer
interrupts that periodically wake the CPU for servicing, whereas the single-time base method can run
completely in hardware. This means that when using the multiple time base method, the CPU is awake for
a larger percentage of the time instead of staying in low-power mode. This is related to the tables in
Section 5.2, which show that the percentage of time in active mode (ISR time) increases with each
additional timer signal and as timer signal frequencies approach the timer clock source frequency. This in
turn increases the average power consumption.
11
References
References
12
www.ti.com
IMPORTANT NOTICE
Texas Instruments Incorporated and its subsidiaries (TI) reserve the right to make corrections, modifications, enhancements, improvements,
and other changes to its products and services at any time and to discontinue any product or service without notice. Customers should
obtain the latest relevant information before placing orders and should verify that such information is current and complete. All products are
sold subject to TIs terms and conditions of sale supplied at the time of order acknowledgment.
TI warrants performance of its hardware products to the specifications applicable at the time of sale in accordance with TIs standard
warranty. Testing and other quality control techniques are used to the extent TI deems necessary to support this warranty. Except where
mandated by government requirements, testing of all parameters of each product is not necessarily performed.
TI assumes no liability for applications assistance or customer product design. Customers are responsible for their products and
applications using TI components. To minimize the risks associated with customer products and applications, customers should provide
adequate design and operating safeguards.
TI does not warrant or represent that any license, either express or implied, is granted under any TI patent right, copyright, mask work right,
or other TI intellectual property right relating to any combination, machine, or process in which TI products or services are used. Information
published by TI regarding third-party products or services does not constitute a license from TI to use such products or services or a
warranty or endorsement thereof. Use of such information may require a license from a third party under the patents or other intellectual
property of the third party, or a license from TI under the patents or other intellectual property of TI.
Reproduction of TI information in TI data books or data sheets is permissible only if reproduction is without alteration and is accompanied
by all associated warranties, conditions, limitations, and notices. Reproduction of this information with alteration is an unfair and deceptive
business practice. TI is not responsible or liable for such altered documentation. Information of third parties may be subject to additional
restrictions.
Resale of TI products or services with statements different from or beyond the parameters stated by TI for that product or service voids all
express and any implied warranties for the associated TI product or service and is an unfair and deceptive business practice. TI is not
responsible or liable for any such statements.
TI products are not authorized for use in safety-critical applications (such as life support) where a failure of the TI product would reasonably
be expected to cause severe personal injury or death, unless officers of the parties have executed an agreement specifically governing
such use. Buyers represent that they have all necessary expertise in the safety and regulatory ramifications of their applications, and
acknowledge and agree that they are solely responsible for all legal, regulatory and safety-related requirements concerning their products
and any use of TI products in such safety-critical applications, notwithstanding any applications-related information or support that may be
provided by TI. Further, Buyers must fully indemnify TI and its representatives against any damages arising out of the use of TI products in
such safety-critical applications.
TI products are neither designed nor intended for use in military/aerospace applications or environments unless the TI products are
specifically designated by TI as military-grade or "enhanced plastic." Only products designated by TI as military-grade meet military
specifications. Buyers acknowledge and agree that any such use of TI products which TI has not designated as military-grade is solely at
the Buyer's risk, and that they are solely responsible for compliance with all legal and regulatory requirements in connection with such use.
TI products are neither designed nor intended for use in automotive applications or environments unless the specific TI products are
designated by TI as compliant with ISO/TS 16949 requirements. Buyers acknowledge and agree that, if they use any non-designated
products in automotive applications, TI will not be responsible for any failure to meet such requirements.
Following are URLs where you can obtain information on other Texas Instruments products and application solutions:
Products
Applications
Audio
www.ti.com/audio
Amplifiers
amplifier.ti.com
www.ti.com/computers
Data Converters
dataconverter.ti.com
Consumer Electronics
www.ti.com/consumer-apps
DLP Products
www.dlp.com
www.ti.com/energy
DSP
dsp.ti.com
Industrial
www.ti.com/industrial
www.ti.com/clocks
Medical
www.ti.com/medical
Interface
interface.ti.com
Security
www.ti.com/security
Logic
logic.ti.com
www.ti.com/space-avionics-defense
Power Mgmt
power.ti.com
Microcontrollers
microcontroller.ti.com
RFID
www.ti-rfid.com
www.ti.com/omap
Wireless Connectivity
www.ti.com/wirelessconnectivity
TI E2E Community Home Page
www.ti.com/video
e2e.ti.com
Mailing Address: Texas Instruments, Post Office Box 655303, Dallas, Texas 75265
Copyright 2011, Texas Instruments Incorporated