Timers and Event Counters13-1Embedded SystemsLecture 13In These Notes . . . We learn the basics of the Timer/Counter peripheral– Called timers by RenesasWe examine how to set up the timers for different operation:– Timer mode– Event counting mode– Pulse Width Modulation (PWM) mode–One-shot timer modeEmbedded Systems 13-2–One-shot timer modeWe then examine how to use a microcontroller in these modesTimer/Counter IntroductionCommon peripheral for microcontrollersBased on pre-settable binary counter, enhanced with configurability–Count value can be read and written by MCUEventsClockCurrent CountReload ValuePresettable Binary Counter÷2 or RSPWMInterruptReloadorEmbedded Systems 13-3–Count value can be read and written by MCU– Count direction can often be set to up or down– Counter’s clock source can be selected• Counter mode: count pulses which indicate events (e.g. odometer pulses)• Timer mode: clock source is periodic, so counter value is proportional to elapsed time (e.g. stopwatch)– Counter’s overflow/underflow action can be selected• Generate interrupt• Reload counter with special value and continue counting• Toggle hardware output signal• Stop!Timer A Block DiagramEach timer has one input, which is selectable from several different sources.Embedded Systems 13-4High-Level Timer A Block DiagramTimer A devices will be the most frequently usedFlexible – can be cascaded Embedded Systems 13-5be cascaded to create larger timers (i.e. 32 bits long)Timer A Mode RegisterTo use the timer, you must set up how you wish to use it (i.e. via TA0MR). After that, the mode register has different settings depending on bits 1 and 0.Embedded Systems 13-6Timer A “Data” RegisterEmbedded Systems 13-7Count Start RegisterOnce the timer has been loaded with a value, start it counting.Embedded Systems 13-8Counter ModeCount pulses representing eventsOdometer example– Measure total distance your car has traveled– Events are wheel rotations, measured with magnetic sensors (dirt-proof!)– Distance traveled = counter value * 100.53”Embedded Systems 13-9* 100.53”• Assume 16” tire radius. Tire circumference = 2πr = 100.53”– Will limited range of 16 bit counter be a problem?• 100.53” * 216-1 = 1247.78 miles– Yes. So need to extend range in software. • Enable overflow interrupt for the timer• Create an ISR to count overflowsTAiMR in Event Counting ModeEmbedded Systems 13-10Up/Down FlagThe default is that the timer will count down.Embedded Systems 13-11Trigger Select RegisterYou can set the trigger pulse of Timers A1 to A4Embedded Systems 13-12Example – Setting-up Event Mode#define TIME_CONFIG 0x01 /* 00000001 val to load into timer mode reg#define TIME_CONFIG 0x01 /* 00000001 val to load into timer mode reg#define TIME_CONFIG 0x01 /* 00000001 val to load into timer mode reg#define TIME_CONFIG 0x01 /* 00000001 val to load into timer mode reg||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||____ MR0: NO PULSE OUTPUT ||||||____ MR0: NO PULSE OUTPUT ||||||____ MR0: NO PULSE OUTPUT ||||||____ MR0: NO PULSE OUTPUT |||||_____ MR1: COUNT FALLING EDGES|||||_____ MR1: COUNT FALLING EDGES|||||_____ MR1: COUNT FALLING EDGES|||||_____ MR1: COUNT FALLING EDGES||||_______MR2: USE UP/DOWN FLAG||||_______MR2: USE UP/DOWN FLAG||||_______MR2: USE UP/DOWN FLAG||||_______MR2: USE UP/DOWN FLAG|||_______ MR3: = 0 IN EVENT COUNTER MODE |||_______ MR3: = 0 IN EVENT COUNTER MODE |||_______ MR3: = 0 IN EVENT COUNTER MODE |||_______ MR3: = 0 IN EVENT COUNTER MODE ||________ TCK0: RELOAD TYPE||________ TCK0: RELOAD TYPE||________ TCK0: RELOAD TYPE||________ TCK0: RELOAD TYPE|__________TCK1: BIT NOT USED */|__________TCK1: BIT NOT USED */|__________TCK1: BIT NOT USED */|__________TCK1: BIT NOT USED */#define CNTR_IPL 0x03 // TA2 priority interrupt level#define CNTR_IPL 0x03 // TA2 priority interrupt level#define CNTR_IPL 0x03 // TA2 priority interrupt level#define CNTR_IPL 0x03 // TA2 priority interrupt level#define LED p7_2 // LED port on MSV1632 board#define LED p7_2 // LED port on MSV1632 board#define LED p7_2 // LED port on MSV1632 board#define LED p7_2 // LED port on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 boardEmbedded Systems 13-13#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board#define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 boardvoid init() {void init() {void init() {void init() {ta2 = 100; //e.g for an automated packaging line, 100 items per caseta2 = 100; //e.g for an automated packaging line, 100 items per caseta2 = 100; //e.g for an automated packaging line, 100 items per caseta2 = 100; //e.g for an automated packaging line, 100 items per case// the following procedure for writing an Interrupt Priority Level follows // the following procedure for writing an Interrupt Priority Level follows // the following procedure for writing an Interrupt Priority Level follows // the following procedure for writing an Interrupt Priority Level follows // that as described in the M16C data sheets under 'Interrupts'// that as described in the M16C data sheets under 'Interrupts'// that as described in the M16C data sheets under 'Interrupts'// that as described in the M16C data sheets under 'Interrupts'_asm (" fclr i") ;_asm (" fclr i") ;_asm (" fclr i") ;_asm (" fclr i") ; // turn off interrupts before modifying IPL// turn off interrupts before modifying IPL// turn off interrupts before modifying IPL// turn off interrupts before modifying IPLta2ic |= CNTR_IPL; // use readta2ic |= CNTR_IPL; // use readta2ic |= CNTR_IPL; // use readta2ic |= CNTR_IPL; // use read----modifymodifymodifymodify----write instruction to write IPLwrite instruction to write IPLwrite instruction to write IPLwrite instruction to write IPLta2mr = TIME_CONFIG;ta2mr =
View Full Document