Uploaded by Sarah Taylor

stopWatch example

advertisement
// ******************* STOP WATCH ***************************
//
// The skeleton of the Stop Watch example presented in lecture
//
// Function prototypes
void initButtons(void);
void initLeds(void);
unsigned char readButtons(void);
void setLeds(unsigned char state);
void swDelay(char numLoops);
void runtimerA2(void);
void stoptimerA2(int reset);
void displayTime(unsigned int inTme);
// Globals
long unsigned int timer_cnt=0;
char tdir = 1;
void main(void)
{
int timer_on=0;
WDTCTL = WDTPW | WDTHOLD;
initButtons();
initLeds();
// Stop watchdog timer
configDisplay();
configKeypad();
// . . .
//__enable_interrupt();
_BIS_SR(GIE);
while (1)
{
ret_val = readButtons();
// see demo project
if (~ret_val & 0x01)
runtimerA2();
if (~ret_val & 0x02)
stoptimerA2(1);
// . . . do other stuff . . .
if (timer_on)
{
if (timer_cnt%15==0)
displayTime(timer_cnt); // pass current time, why?
}
} /* End while loop */
}
// Configure and initialze LCD display
void configDisplay(void)
{
// enable clock crystal XT1 and XT2 inputs and outputs
P5SEL |= (BIT5|BIT4|BIT3|BIT2);
}
// . . .
void runtimerA2(void)
{
// This function configures and starts Timer A2
// Timer is counting ~0.01 seconds
//
// Input: none, Output: none
//
// smj, ECE2049, 17 Sep 2013
//
// Use ACLK, 16 Bit, up mode, 1 divider
TA2CTL = TASSEL_1 + MC_1 + ID_0;
TA2CCR0 = 327;
// 327+1 = 328 ACLK tics = ~1/100 seconds
TA2CCTL0 = CCIE;
// TA2CCR0 interrupt enabled
}
void stoptimerA2(int reset)
{
// This function stops Timer A2 andresets the global time variable
// if input reset = 1
//
// Input: reset, Output: none
//
// smj, ECE2049, 17 Sep 2013
//
TA2CTL = MC_0;
// stop timer
TA2CCTL0 &= ~CCIE;
// TA2CCR0 interrupt disabled
if(reset)
timer_cnt=0;
}
void displayTime(unsigned int inTme)
{
// This converts the global counter variable timer_cnt to a display
// of minutes and seconds -- MM:SS.S
//
// Input: none, Output: none
//
// smj, ECE2049, 17 Sep 2013
char asc_arr[6];
unsigned int min;
unsigned int sec;
unsigned int msec;
. . .
/* Calling the draw command with OPAQUE_TEXT instead of TRANSPARENT_TEXT
removes the need to clear whole screen with every update */
Graphics_drawStringCentered(&g_sContext, asc_arr,6,51,32,OPAQUE_TEXT);
. . .
}
// Timer A2 interrupt service routine
#pragma vector=TIMER2_A0_VECTOR
__interrupt void TimerA2_ISR (void)
{
if(tdir)
{
timer_cnt++;
if (timer_cnt == 60000)
timer_cnt = 0;
if (timer_cnt%100==0) // blink LEDs once a second
{
P1OUT = P1OUT ^ BIT0;
P4OUT ^= BIT7;
}
}
else
timer_cnt--;
}
Download