ECE2049: Embedded Computing in Engineering Design C Term – Spring 2016 Lecture #12: Real World Timers Reading for Today: Reading for Next Class: HW #3 (on web): Lab #2 (on web): User's Guide Ch 17 Davies 9.2-9.3, 9.7, User's Guide 20 Due Tuesday 2/9/16 (in class) Early sign-off by 5 pm Fri 2/12. Report due 2/16 (in class) Last Class: Introduction to the MSP430F5529's timers. >> Setting the control registers for Timer A2 Default Frequencies: ACLK = 32768 Hz; MCLK = 1.048576 MHz; SMCLK = 1.048576 MHz; Stop Watch example continued... >> We want to implement Stop Watch with accuracy of 0.01 seconds. Selected ACLK = 32768 Hz as TimerA2 source clock and Up count mode --> Timer generates Interrupt after MAX COUNT reached 327 /*** This function configures and starts Timer A2 to count ~0.01 second intervals ***/ void runtimerA2(void) { // From Users Guide Ch 17 // Use ACLK (TASSEL_1), clock divider of 1 (ID_0) // and start timer counting in Up mode (MC_1) TA2CTL = TASSEL_1 + MC_1 + ID_0; // TA2CCR0 = 327; TA2CCTL0 = CCIE; // 327+1 ACLK tics = ~1/100 seconds // TA2CCR0 interrupt enabled } What will it mean when the CPU gets an interrupt from TimerA2? What should the ISR do? #pragma vector=TIMER2_A0_VECTOR // syntax for ISR __interrupt void Timer_A2_ISR(void) { } At the top of main() you must enable also interrupts ... // Using msp430.h definitions _BIS_SR(GIE); // Global Interrupt enable >> What does the CPU do when it receives the interrupt (assuming interrupts are enabled) and between interrupts unsigned int timer; // global timer count variable . . . _BIS_SR(GIE); . . // Global Interrupt enable . // Inside main() while(1) { // Forever loop of the stop watch . . . chkBtns(); // check for button press if(button1) { timer_on=0; stoptimerA2(1); // reset timer P1OUT =~BIT0; // turn LED off } if(button2) { timer_on=1; runtimerA2(); // starts timer P1OUT =~BIT0; // turn LED off GrClearDisplay(&g_sContext); } if (timer_on) if (timer%10==0) // update display every 1/10th sec displayTime(timer); } /* End while(1) loop */ How accurate will our stop watch be? Is that accuracy ok? >> Duration of 1 clock tic = 1/32768 Hz = 3.053E-5 sec >> Our stop watch will actually run a bit slow. How long until it is off by .01 sec >> Not unusual for clock crystals to run at odd-ball frequencies (32768 Hz is actually reasonably nice!) >> We can compensate in software – using leap counts! #pragma vector=TIMER2_A0_VECTOR __interrupt void Timer_A2_ISR(void) { // timer and leap_cnt are Global unsigned integers } >> Is this perfect? Now how long is will the stop watch be accurate to 0.01 sec? >> Now that TimerA2 and its associated ISR are properly configured and running perfectly how we use the resultant count?? -- What does the value of the variable timer represent? What if we wanted 1 ms resolution? What would change in our TimerA2 settings? Our ISR? How we use timer? What about ½ second resolution? Or 0.0001 second? Should we still use ACLK as the clock source here? What to do??