Grundlagen Microcontroller Counter/Timer Günther Gridling Bettina Weiss 1
Counter/Timer Lecture Overview Counter Timer Prescaler Input Capture Output Compare PWM 2
important feature of microcontroller many applications require timers controllers include one or more timers width typically 8 or 16 bit "resolution": width of the timer "granularity": duration of 1 timer tick 3
timers are actually counters: have a timer count register (TCNT) that contains current count value count register can be read & set anytime be careful when acessing 16 bit count register on 8 bit machine > ensure atomic access! beware side effects when writing to running timer 4
timer may count up or down (sometimes configurable) several sources for clock possible system clock (possibly prescaled) external clock (pulse accumulator) asynchronous clock: external quartz (e.g. watch quartz, 32.768 khz) 5
typical use of timer: wait for some time count to given number of cycles, then raise interrupt (on timer overflow) user loads timer with value, timer counts down to 0 user loads timer with 2^n value, timer counts up to 0 maximum wait period: 2^n * clock period Example: 8 bit timer, 1 Mhz (1 cycle = 1 us) > can wait for 256 us 6
Prescaler: divides system clock normally, timer counts with every clock pulse prescaler: only lets every 2 nd, 4 th,... 1024 th pulse through timer counts more slowly is used to extend the range of the timer Example: 8 bit timer, 1 Mhz (1 cycle = 1 us) > can wait for ~260 ms with prescaler 1024 7
Prescaler problem : granularity gets larger no prescaler: 1 timer tick = 1 clock cycle 1024 prescaler: 1 timer tick = 1024 cycles Example: 8 bit timer, 1 Mhz (1 cycle = 1 us) no prescaler: 1 us with prescaler: 1.024 ms 8
Sometimes available: modulus mode user loads start value when timer reaches 0 > automatically loads start value again get periodic interrupts of given period 9
other typical features of timers: Input Capture Output Compare PWM (Pulse Width Modulation) 10
Input Capture: read timer value on external event used to timestamp external events if event happens, current value of count register is saved in input capture register rising edge, falling edge, toggle interrupt may be raised semi internal events (e.g., analog comparator) may trigger input capture 11
nice to measure intervals between events (with accuracy of +/ 1 clock cycle) problems when events too close together like digital I/O, input capture suffers from glitches noise cancellation through several samples delays timestamping of one event interval stays the same 12
Output Compare: set output on timer event event: timer count register has reached given value compare value is set by user in compare register, timer is started with 0 when counter value reaches compare value > something happens interrupt may be raised output pin may be changed 13
Different Output Compare Modes: set output pin to 0 set output pin to 1 toggle output Counter/Timer nice for doing something after a delay nice for periodic signals with equal high/low time 14
PWM (Pulse Width Modulation): periodic signal with variable high time period is adjustable (within range of timer) PWM has many uses: control speed of motor control position of servo motor dim LEDs cheap d/a conversion 15
PWM (Pulse Width Modulation): registers for period and high time counter starts from 0, counts to period toggles output pin according to high time value interrupts at reaching high time & period problem of glitches when updating registers 16
Lecture Summary controller has 8 bit, 16 bit timers (or mixed) Overflow, Modulus, Input Capture, Output Compare, PWM careful when accessing a running timer practically everything is an interrupt source 17