Presentation is loading. Please wait.

Presentation is loading. Please wait.

Timers and Event Counters

Similar presentations


Presentation on theme: "Timers and Event Counters"— Presentation transcript:

1 Timers and Event Counters
Embedded Systems

2 In These Notes . . . We learn the basics of the Timer/Counter peripheral Called timers by Renesas We examine how to set up the timers for different operation: Timer mode Event counting mode Pulse Width Modulation (PWM) mode One-shot timer mode We then examine how to use a microcontroller in these modes Embedded Systems

3 Timer/Counter Introduction
Current Count Reload Value Presettable Binary Counter Events Reload or ÷2 or RS PWM Clock Interrupt Common peripheral for microcontrollers Based on pre-settable binary counter, enhanced with configurability 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! Embedded Systems

4 Timer A Block Diagram Each timer has one input, which is selectable from several different sources. Embedded Systems

5 High-Level Timer A Block Diagram
Timer A devices will be the most frequently used Flexible – can be cascaded to create larger timers (i.e. 32 bits long) Embedded Systems

6 Timer A Mode Register To 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

7 Timer A “Data” Register
Embedded Systems

8 Count Start Register Once the timer has been loaded with a value, start it counting. Embedded Systems

9 Counter Mode Count pulses representing events Odometer example
Measure total distance your car has traveled Events are wheel rotations, measured with magnetic sensors (dirt-proof!) Distance traveled = counter value * ” Assume 16” tire radius. Tire circumference = 2pr = ” Will limited range of 16 bit counter be a problem? 100.53” * = miles Yes. So need to extend range in software. Enable overflow interrupt for the timer Create an ISR to count overflows Embedded Systems

10 TAiMR in Event Counting Mode
Embedded Systems

11 Up/Down Flag The default is that the timer will count down.
Embedded Systems

12 Trigger Select Register
You can set the trigger pulse of Timers A1 to A4 Embedded Systems

13 Example – Setting-up Event Mode
#define TIME_CONFIG 0x01 /* val to load into timer mode reg ||||||||_ TMOD0,TMOD1: EVENT COUNTR MODE ||||||____ MR0: NO PULSE OUTPUT |||||_____ MR1: COUNT FALLING EDGES ||||_______MR2: USE UP/DOWN FLAG |||_______ MR3: = 0 IN EVENT COUNTER MODE ||________ TCK0: RELOAD TYPE |__________TCK1: BIT NOT USED */ #define CNTR_IPL 0x // TA2 priority interrupt level #define LED p7_ // LED port on MSV1632 board #define LED_PORT_DIRECTION pd7_2 // LED port dirn on MSV1632 board void init() { ta2 = 100; //e.g for an automated packaging line, 100 items per case // the following procedure for writing an Interrupt Priority Level follows // that as described in the M16C data sheets under 'Interrupts' _asm (" fclr i") ; // turn off interrupts before modifying IPL ta2ic |= CNTR_IPL; // use read-modify-write instruction to write IPL ta2mr = TIME_CONFIG; _asm (" fset i"); ta2s = 1; //start counting } Embedded Systems

14 TAiMR in Timer Mode Embedded Systems

15 Example – Timer & Event Mode
The maximum amount of time one can count using two timers is: Cascade Timer A0 to Timer A1 Use the 1/32 clock (bits 7 and 6 of TA0MR set to ’10’) Set both timer values to xFFFF Timer A1 output generates an interrupt This would create a timer event which would interrupt the CPU every ______ seconds (almost __ hours!) Embedded Systems

16 Pulse-Width Modulation
Often need to generate an analog value: motor speed, servo motor position, incandescent light dimmer, adj. switch-mode power supply Analog circuits have some disadvantage Generating the voltage accurately – circuits drift with temperature and time Analog power amplifiers are inefficient (P = E*I) Analog signals are much more sensitive to noise than digital signals PWM provides a digital encoding of an analog value Now circuits are digital: more efficient, less sensitive to noise, temperature, aging effects PWM signal characteristics Modulation frequency – how many pulses occur per second (fixed) Period – 1/(modulation frequency) On-time – amount of time that each pulse is on (asserted) Duty-cycle – on-time*period Adjust on-time (hence duty cycle) to create encoded analog value Embedded Systems

17 Pulse-Width Modulation
We rely on low-pass filtering to “decode” (convert) this high-frequency PWM signal to an analog DC value Variable speed motor control: Motor has inertia (will coast if power is turned off) Incandescent Lamp Dimmer: Human eye has persistence of vision (retina averages signals above 30 Hz), and filament takes time to cool down and stop emitting visible light Adjustable switch-mode power supply: Rely on inductor and capacitor to maintain current and voltage when not drawing current from battery Many Timer/Counter peripherals can also generate PWM signal For example Count up Set PWM output when counter value reaches 0 Reset PWM output when counter value reaches n When counter reaches , overflow and continue counting Read Michael Barr’s Embedded Systems Programming article “Pulse Width Modulation” for more PWM information Embedded Systems

18 Timer A PWM Description
Embedded Systems

19 Timer A Mode Register for PWM
Embedded Systems

20 One-Shot Flag Embedded Systems


Download ppt "Timers and Event Counters"

Similar presentations


Ads by Google