avr-classes
|
The AVR's timer units are available using the Timer interface. While all use-cases are supported, the main use is currently the CTC mode for generating periodic timer interrupts. This is also supported by the Timer::autoFill method for configuration:
There are three ways to make use of CTC timer callbacks.
1. Using Timer callbacks directly
The most accurate timing is achieved by handling the timer interrupts directly. First create a class implementing the Timer::InterruptHandler interface:
Then register the class as interrupt handler for an existing Timer instance:
2. Using the TimerAdapter class
The TimerAdapter implements the Timer::InterruptHandler interface and takes a reference to an EventReceiver plus event IDs for the two individual use-cases (output compare match and overflow). The receiver can be an user defined class implementing the EventReceiver interface, or an instance of EventDispatcher or QueuedEventDispatcher to support multiple receivers. The latter can also used to decouple ISR context from main context.
The following example shows how to convert the output compare match interrupts into events which are then passed to several receivers:
3. Using Timer Services
Timer Services are classes derived from TimerService which can be used in connection with a TimerServiceHost. The TimerServiceHost implements the Timer::InterruptHandler interface and distributes the callbacks to all registered services. At the moment there are three timer services:
These classes are templates which allow the user to specify the data type to be used as couting variables (to control the range). The resolution depends on the rate at which the TimerServiceHost receives output compare match interrupt callbacks (common base tick).