Tutorial_10 : TimerDelay Driver - sudeshmoreyos/Morey_os-demo-1.0 GitHub Wiki
1. Introduction to Timer Delay
At times, an application may require certain actions to be performed repeatedly at very short and precise intervals. The DELAY_SEC() macros do not guarantee the exact specified delay on every call, while blocking delays cannot be used because they prevent other tasks from running.
For example, consider a four-digit seven-segment display. If all four displays are driven individually, a total of 32 controller pins would be required. However, by using a technique called multiplexing, the same functionality can be implemented using only 8 + 4 pins. More about seven-segment multiplexing is discussed in Tutorial-11. The idea behind multiplexing is simple. Instead of glowing all four seven-segment displays simultaneously, we glow each display one by one. The switching is performed so quickly that it creates the illusion that all four displays are glowing at the same time.
Another example is a 4x4 keypad. If each key is connected and read individually, 16 controller pins would be required. By using multiplexing techniques, the same keypad can be implemented using only 8 pins. More about keypad implementation is discussed in Tutorial-12. Applications such as seven-segment multiplexing, keypad scanning, and other time-critical repetitive operations can be efficiently implemented using the TimerDelay Driver.
2. TimerDelay Driver
The TimerDelay Driver is used to periodically call a callback function at precise intervals specified using the TimerDelay.begin() function. The delay can be as low as 100 microseconds, with a resolution of 100 microseconds.
To use this driver, include the following header file in your code:
#include "TimerDelay.h"
The TimerDelay Driver supports the following functions:
2.1 TimerDelay.begin
Declaration:
TimerDelay.begin(mos_uint8_t timer_no,
float timer_delay_ms,
void (*timer_delay_callback)(void));
The TimerDelay.begin() function activates a callback function that is called periodically after the specified delay interval. This callback function can be used to implement repetitive actions that require precise timing. This function takes three input parameters:
a) Timer Number
Possible values include TIMER_DELAY0, TIMER_DELAY1, etc. The available timer numbers are platform-dependent. For example, the ATmega328P (Arduino Uno) provides three timers: TIMER_DELAY0, TIMER_DELAY1, and TIMER_DELAY2. However, TIMER1 is used internally by the OS scheduler. Therefore, TIMER_DELAY1 is not available unless the scheduler is configured to use a different timer. To view the available TimerDelay timers for your platform, enable the following macro in config.h:
#define TIMER_DELAY_HELP_ENABLE
b) Callback Interval in Milliseconds
This input is of type float and specifies the callback interval in milliseconds. The minimum supported delay is 0.1 millisecond (100 microseconds). If a value smaller than 0.1 millisecond is passed, it will automatically be treated as 0.1 millisecond.
c) Pointer to Callback Function
A pointer to the callback function must be provided here. The callback function must have void input and void output.
A typical example is shown below:
TimerDelay.begin(TIMER_DELAY0,1.5,&callback_function);
In the above example:
- TIMER_DELAY0 is used.
- The callback interval is 1.5 milliseconds.
- callback_function() is called every 1.5 milliseconds.
The callback function will continue to execute periodically until the timer is stopped using TimerDelay.end().
2.2 TimerDelay.end
Declaration:
TimerDelay.end(mos_uint8_t timer_no);
This function is used to deactivate a previously configured TimerDelay timer. It takes a single input parameter, which is the timer number to be stopped.