Differences in Approach to Choosing a Timer Method

Submitted by Ed_B on Tue, 07/06/2010 - 23:04

Printer-friendly versionPrinter-friendly version

 The timer-scheduler library gives three methods for using timer generated events in your code. 

  • Polling a flag attached to a timer.
  • Using callback functions that are activated by the timers.
  • Driving an i/o pin directly from the timer service (pin-direct method).
  Poll a Flag Callback Pin-direct
loop() will be interrupted during normal operation (this is usually ok)    X  
user code runs at every timer event (too much can choke the AVR)    X  
must attach a pin to a timer to see anything happen      X

can attach a pin to timer, but it's better to control the pin in the program

 X  X  
user code must be tight and fast (no delay(), minimal use of Serial.print(), etc.)    X  
should avoid using delay()  X won't
can have empty loop()    X  X
can use Timer.setHighTime() and Timer.setLowTime()  X  X  X
can use Timer.repeat()  X  X  


A flag is a sort of boolean value that the chip hardware uses to communicate between different hardware systems, or flags can be read from or written to in the user's code. In  the MCU hardware, flags are specially designated bits contained in bytes called registers. The registers of an MCU are in a special block of memory called the register file. Hundreds of control and data lines from the guts of the MCU connect to bits in the register file which serves as a sort of  "patch bay" that operates under a mix of automatic and software control. Registers are beautiful things, but madness that way lies. (See the AVR data sheet). The timer-scheduler is written to look like an interface to a group of hardware timers. In fact, the timer-scheduler uses periodic interrupts from one timer (hardware timer 2) in the MCU and runs a number of software timers from that. (I didn't invent this. It's a simple scheme that's probably as old as computers.)