2010-04-01

Timers in Microcontrollers

Timers are indispensable components of microcontroller. These timers are essentially counters that increment based on the clock cycle and the timer prescaler. When the limitation reaches (overflow), a interrupt will be generated. There are several different types timers:

  • watchdog timer, used to reset the controller if a hang of system or unexpected logical conditions occur.
  • 8 bit basic timer, upper limit is 255. Usually for general purpose
  • 16 bit timer, upper limit is 65535, used for PWM and capture/compare function, e.g RC acquisition.
Every timer is configured with a clock source (internal or external) and prescaler. See the example at http://www.societyofrobots.com/programming_timers.shtml:

Assume that Timer1 is set up with a prescaler of 8 on a MCU clocked at 20 MHz. Assume that a total of 6250 clicks were counted.

then . . .

    delay (in ms) = (# ticks) * 4 * 8 * 1000 / (20000000)

    delay (in ms) = (6250) / 625 = 10 ms

where
  • 4: in PIC16 and 18F families 1 instruction cycle = 4 crystal cycle
  • 8: prescaler number
  • 1000: unit in ms
  • 20000000: 20 MHz clock input

Reference:

1 comment: