Professional Documents
Culture Documents
A timer is a counting mechanism that is tied to some type of regular interval provided
by a clock. The Timer_A peripheral is a 16-bit timer. This means that the timer counts from 0 in binary
to 0b1111111111111111, or0xFFFF in hex, 65,535 in decimal. Simple enough, and very useful when we
know how to manipulate the behavior of the timer.
Timer Modes:
There are three modes to operate Timer_A, which one we use depends entirely on the application:
The first mode is what we call the continuous mode: Timer_A acts just like a 16-digit, binary odometer; it
counts from 0 to 0xFFFF and then "rolls over" to 0 again.
The second mode is called up mode; here, just like in continuous mode, it counts up and rolls over to 0. This
mode, however, lets you choose how high up the timer counts before rolling over.
The third mode, up/down mode, is similar to up mode in that you can program the upper limit. It differs in
thatratherthan rolling over to 0, it turns around and counts down to 0.
Clocks:
A clock signal controls timing on a computer, a typical digital clock signal is below.
MSP430 CPU and other system devices use three internal clocks:
Typically SMCLK runs at the same frequency as MCLK, both in the megahertz range.
ACLK is often derived from a watch crystal and therefore runs at a much lower frequency. Most peripherals
can select their clock from either SMCLK or ACLK.
For the MSP430 processor, both the MCLK and SMCLK clocks are supplied by an internal digitally
controlled oscillator (DCO), which runs at about 1.1 MHz.
The frequency of the DCO is controlled through sets of bits in the modules registers at three levels. There
calibrated frequencies of 1, 8, 12, and 16 MHz. To change frequency simply copy values into the clock
module registers.
The following sets the DCO used by the CPU to 1MHz, which seems to be the default value:
BCSCTL1 = CALBC1_1MHZ; // Set range
Registers
With so many ways to use Timer_A, you can probably imagine it corresponds to a lot of registers in the MSP430 to use
and control the timer:
TACTL -- The Timer_A Control Register is used to set up the link between the timer and a clock and select
the mode used.
o The TASSELx bits (8 and 9) tell the timer which clock to use as its source.
TASSEL_0 = TACLK
TASSEL_1 = ACLK @ 12KHz.
TASSEL_2 = SMCLK @ 1MHz
TASSEL_3 = INCLK
o The clock frequency can be divided by a further factor of 2, 4, or 8 using the IDx bits (6 and 7). (Note
that this is further division to any divisions done from the clock source for the clock itself; you could
potentially have a total division of up to 64 from your clock source for this peripheral.)
o The MCx bits (4 and 5) select the particular mode for the timer to use. Note particularly that setting
these bits to 0 (the default setting on POR) halts the timer completely.
o TACLR is bit 2. If you write a 1 to this bit, it resets the timer. The MSP430 will automatically reset
this bit to zero after resetting the timer.
o TAIE and TAIFG (bits 0 and 1) control the ability of the timer to trigger interrupts.
TAR -- The Timer_A Register is the actual counter; reading this register reports the current value of the
counter.
TACCRx -- The Timer_A Capture/Compare Registers, of which there are two in the value line devices
(TACCR0 and TACCR1) are where particular values we want to use are stored. In compare mode, we write
values here where we want the timer to signal an event. Particularly, TACCR0 is used to store the value to
which we want Timer_A to count in up and up/down mode. In capture mode, the processor will record the
value of TAR when the MSP430 is signaled to do so.
TACCTLx -- The Timer_A Capture/Compare Control Registers correspond to the TACCRx registers. These
set the behavior of how the CCR's are used.
o CMx (bits 14 and 15) change what type(s) of signals tell the timer to perform a capture.
o CCISx (bits 12 and 13) select where the input signals are taken.
o SCS and SCCI (bits 11 and 10 respectively) change the synchronicity; the timer normally operates
asynchronously to the input signals. (Yeah, I don't entirely understand this yet either)
o CAP (bit 8) changes whether capture mode (1) or compare mode (0) is used.
o OUTMODx (bits 5-7) select various output modes for the CCR's signal when the timer flags a capture
or compare event.
o CCIE and CCIFG (bits 4 and 0) are more interrupts associated with the CCR's.
o CCI and OUT (bits 3 and 2) are the input and output for the CCR.
o COV (bit 1) is the capture overflow; this bit is set to 1 if two captures are signaled before the first
capture value is able to be read.
TAIV -- The Timer_A Interrupt Vector Register; since there are multiple types of interrupts that can be
flagged by Timer_A, this register holds details on what interrupts have been flagged.
o The only bits used here are bits 1-3 (TAIVx), which show the type of interrupt that has happened,
allowing us to take different actions to resolve the different types of interrupts.
TOGGLE RED LED EVERY SECOND:
Counters in the examples are off by 1. Since the count starts at 0, a count of 12000 would have a limit of 11999. For
clarity and arithmetic ease, we'll live with the small error.
Counter
TA0CCTL0 : Control register for Timer A0 counter 0. Interrupts are enabled by writing a 1
into bit 4 of this register.
12000 is used because the ACLK clock operates at approximately 12KHz and we want to
toggle the LEDs at approximately at one second intervals. With interrupts enabled, an
interrupt is generated when the count reaches 12000.The counter, in UP mode, is
automatically reset to 0 when count limit reached.
Interrupts
One important point is that timers can run independently to the CPU clock, allowing the CPU to be turned off and
then automatically turned on when an interrupt occurs.
_BIS_SR(LPM0_bits + GIE); places CPU in low power mode with interrupts enabled.
SR/R2::Status register.
The WDT counts down from a specified value and either resets or interrupts when count overflow is reached. A way
to use this timer is to periodically service it by resetting its counter so that the system knows that everything is all
right and there is no reset required. It can also be configured configured as an interval timer to generate interrupts
at selected time intervals.
A computer watchdog is a hardware timer used to trigger a system reset if software neglects to regularly service the
watchdog . In a watchdog mode, the watchdog timer can be used to protect the system against software failure,
such as when a program becomes trapped in an unintended, infinite loop.
Left unattended in watchdog mode, the watchdog counts up and resets the MSP430 when its counter reaches its
limit. Your code must therefore keep clearing the watchdog counter before it reaches its limit to prevent a non-
maskable, reset interrupt. Be aware that watchdog mode is immediately activated after the MSP430 has been reset
and therefore the watchdog must be cleared, stopped, or reconfigured before the default time expires. (Otherwise,
your software enters an infinite reset loop.)
Applications needing a periodic "tick" may find the watchdog interval mode ideal for generating a regular interrupt.
The disadvantage is the limited selection of interval times - only 4 intervals are available and they are dependent
upon the clock assigned to the watchdog.
Assuming the MSP430 system clock has a frequency of 1Mhz and the watchdog timer is clocked by the sub-master
clock (SMCLK), the following intervals are available:
Example
WDTCTL = WDT_MDLY_0_5
Programming WDT
The watchdog timer is set to interval mode by setting its register to WDTCTL = WDT_MDLY_32.
Defined in the msp430g2553.h include file, this sets the watchdog to interrupt every 32ms when clocking from a
1Mhz SMCLK.
An interrupt counter is set up so that the device goes to low power after 8 seconds ( 32ms * 250 = 8 s). The
watchdog interrupt counter is cleared every time P1 interrupts, as well as wakes up if the device is currently in low
power mode.
#include <msp430g2553.h>
unsigned int counter = 0;
void main(void){
WDTCTL = WDT_MDLY_32; // Watchdog Timer interval to 32ms
IE1 |= WDTIE; // Enable WDT interrupt
P1DIR |= BIT0; // Set red LED P1.0 to output direction
_BIS_SR(GIE); // Enable interrupts
while(1);
}
#pragma vector=WDT_VECTOR // Watchdog Timer interrupt service routine
__interrupt void watchdog_timer(void) {
if(counter == 10){ // 10 * 32 ms = 320 ms, .3 s
P1OUT ^= BIT0; // P1.0 toggle, red LED
counter = 0;
}
else
counter++;
}
PULSE WIDTH MODULATION
Pulse-width modulation (PWM) is a commonly used technique for controlling power to inertial[ambiguous]
electrical devices, made practical by modern electronic power switches.
The average value of voltage (and current) fed to the load is controlled by turning the switch between supply
and load on and off at a fast pace. The longer the switch is on compared to the off periods, the higher the
power supplied to the load is.
The PWM switching frequency has to be much faster than what would affect the load, which is to say the
device that uses the power. Typically switching's have to be done several times a minute in an electric stove,
120 Hz in a lamp dimmer, from few kilohertz (kHz) to tens of kHz for a motor drive and well into the tens or
hundreds of kHz in audio amplifiers and computer power supplies.
Period is the time of each pulse, both the On and Off times.
Duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time; a low duty cycle
corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent,
100% being fully on.
The main advantage of PWM is that power loss in the switching devices is very low. When a switch is off
there is practically no current, and when it is on, there is almost no voltage drop across the switch. Power loss,
being the product of voltage and current, is thus in both cases close to zero. PWM also works well with digital
controls, which, because of their on/off nature, can easily set the needed duty cycle.
Period
Time of each pulse
A period of 0.2 second and 10% duty cycle = 0.10 * 0.2 second = 0.02 seconds.
If the signal has a low voltage of 0 and a high voltage of 10 volts, a 50% duty cycle produces an average of 5 volts, a
10% duty cycle produces an average of 1 volt.
PWM on the LaunchPad
PWM can be used to control the brightness of the green LED by varying the duty cycle, a longer duty cycle results in
a brighter LED.
TA0CCR0/TA0CCR1
Timer registers determine when events occur and the clock count limit. Combined, the register values determine the
PWM period (TA0CCR0) and duty cycle (TA0CCR1).
Achieves a 10% duty cycle, where PWM output is 1 for 10% of the time and 0 for 90%
"TA0CCR1 output is reset (0) when the timer counts to the TA0CCR1 value and is set (1) when the timer counts to
the TA0CCR0 value" (From User Guide).
Means that when TA0CCR0 count is reached PWM output is 1 and counting starts over, and when TA0CCR1 is
reached, PWM output is 0.
TA0CCTL1
Controls PWM output, whether high or low when TA0CCR1 is below the count.
TA0CTL
Placing system in LPM0 keeps the SMCLK and timer A operating to control the PWM while shutting down
the CPU.
The default DCO (digital controlled oscillator) which is the source of the MCLK and in this case the ACLK
at approximately 12KHz, making the PWM period about 1 second and .10 second duty cycle or 10%.
#include <msp430g2553.h>
void main(void){
TA0CCR1 = 1200; // PWM duty cycle, time cycle on vs. off, on 10% initially
TA0CTL = TASSEL_1 + MC_1; // Timer A control set to submain clock TASSEL_1 ACLK
// and count up to TA0CCR0 mode MC_1
Analog to digital conversion (ADC) senses the analog, continuous, world and converts it to a digital value
for use on a computer.
Analog Digital
Volts Value
4.375 to 5 111
0 to .625 000
ADC is often performed by successive approximation. The essentials of the process are:
4. When the input and output voltage match, the counter holds the digital value.
Because of the time it takes time for the counter to "find" the proper value, an ADC is generally much
slower than the CPU.
To prevent receiving stale or erroneous results, after starting an ADC conversion the CPU can poll the ADC
to determine when the conversion is complete.
The LaunchPad MSP430G2553 chip contains a temperature sensor, a thermistor, connected to an ADC; the
LaunchPad can take its own temperature.
An ADC compares a variable input voltage to a reference voltage, returning the proportion between the two.
The 10-bit ADC on the G2553, returns a number between 0 and 1023, or 1024 = 210 values.
The ADC, with an analog input of 1 volt and the LaunchPad's reference voltage of 1.5 volts, returns 682
because its the same proportion with 1024:
Digital temperature sensing often places a thermistor in a voltage dividing circuit such as the one below.
The ADC, with an analog input of 0.75 volt and the LaunchPad's reference voltage of 1.5 volts, returns 512
because proportional.
#include <msp430g2553.h>
long sample;
long DegreeF;
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON; // Ref voltage/sample & hold time/
// reference generator ON/ADC10 ON
while(1) {
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
TUTORIAL BY:
RAMITHA SUNDAR
EEE
107112069