Professional Documents
Culture Documents
Same Time?
1. Our microcontroller actually has multiple general purpose timers that we can use in our programs.
The default timer that we used in the previous lessons is called Timer_A0. The second timer that
we can use is called Timer_A1.
The default Timer_A0 and alternative Timer_A1 are almost identical, so you already know
almost everything necessary to use both timers in your programs. The only differences between
the two are their register names. For example, here is a program that we used for Timer_A0.
For easy reference, we have indicated the Timer_A0 register names.
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Stop the watchdog timer
PM5CTL0 = ENABLE_PINS; // Enable inputs and outputs
while(1)
{
if(TA0CTL & TAIFG) // If timer has counted to 5000
{
P1OUT = P1OUT ^ RED_LED; // Then, toggle red P1.0 LED
TA0CTL = TA0CTL & (~TAIFG); // Count again
}
}
}
Notice, the #define terms (ACLK, UP, and TAIFG) are not changed. This is because the ACLK,
UP, and TAIFG bits are located in the same position in both the TA0CTL and TA1CTL registers.
Save, Build, Debug, and run your program to verify that it works. The red LED should be
blinking on and off approximately 4 times per second.
When you are ready, click Terminate to return to the CCS Editor.
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Stop the watchdog timer
PM5CTL0 = ENABLE_PINS; // Enable inputs and outputs
while(1)
{
if(TA1CTL & TAIFG) // If timer has counted to 5000
{
P1OUT = P1OUT ^ RED_LED; // Then, toggle red P1.0 LED
TA1CTL = TA1CTL & (~TAIFG); // Count again
}
}
}
#include <msp430.h>
main()
{
WDTCTL = DEVELOPMENT; // Stop the watchdog timer
PM5CTL0 = ENABLE_PINS; // Enable inputs and outputs
while(1)
{
if(TA0CTL & TAIFG) // If timer 0 has counted to 33000
{
P1OUT = P1OUT ^ RED_LED; // Then, toggle red P1.0 LED
TA0CTL = TA0CTL & (~TAIFG); // Count again
}
}//end while(1)
}//end main()
Then, the program enters an infinite while(1) loop. In the loop, the program is continuously
checking to see if Timer 0 has counted to 33,000. If the answer is yes, the program toggles the
red P1.0 LED and clears the Timer 0 TAIFG flag in preparation for the next 33,000 count.
Next, the program checks to see if Timer 1 has counted to 5,000. If the answer is yes, the
program toggles the green P9.7 LED and clears the Timer 1 TAIFG flag in preparation for the
next 5,000 count. The program then returns to the top of the while(1) loop and checks the
status of the timers repeatedly.
6. Click Terminate to return to the CCS Editor when you are ready.
7. Using multiple timers like in the above program is relatively straightforward. However, if you
have to count higher than 65,535, it becomes a little more complicated.
A program is shown on the next page, and its corresponding flowchart is shown on the page after
that. However, for a short summary:
2) After ten periods of 10ms elapses, Timer 0 causes the red LED to toggle
What you will see is that this program has become quite complex rather quickly. There are a few
more bonus sections on timers that follow, but after that, we are going to introduce the concept of
functions in the C programming language. Functions will greatly simplify the development and
also the readability of our C programs.
main()
{
unsigned char t0_count=0;
unsigned char t1_count=0;
while(1)
{
}//end timer0 if
}//end timer1 if
}//end while(1)
}//end main()
Ti mer 0
Count up
for 10ms
Ti mer 1
Count up
for 1s
Ma ke outputs
P1.0 a nd P9.7
No
No 100ms
el apsed?
Yes
Cl ea r 10ms
counter
No 3s
el apsed?
Yes
Cl ea r 3s
counter
Toggl e green
LED
In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.