Tutorial_10 : TimerDelay Driver - sudeshmoreyos/Morey_os-demo-1.0 GitHub Wiki

Home <<Prev 10 Next>>

1. Introduction to Timer Delay

At times, application may require some repetitive actions to be implemented at very short precise intervals. DELAY_SEC Macros do not guarantee specified delay at every call, while blocking delays cannot be used because it will block other tasks.

For example, in Seven Segment multiplexing implementation with four seven segments, if you glow all four seven segments individually, it will require 32 pins of controller. But if we use multiplexing, we require only 8 + 4 pins. More about Seven Segment multiplexing is discussed in Tutorial-11. The trick here is that, instead of glowing four seven segments individually, we glow each Seven-Segment one by one. But Switching is done so fast that we get illusion that all four seven segments are glowing simultaneously.

Another example is 4x4 keypad implementation. If we read each button individually we will need 16 pins, but by multiplexing method it can be implemented in 8 pins only. More about keypad is discussed in Tutorial-12. Seven Segment multiplexing and keypad like functionality can be implemented using TimerDelay driver.

2. Timer Delay Driver

TimerDelay driver is used to activate a callback function which is being called after fixed precise intervals as specified in TimerDelay.begin function. This delay can be as low as 100 us with resolution of 100 us. In order to use this driver we need to include header file :

#include "TimerDelay.h"

TimerDelay driver supports following functions :

2.1 TimerDelay.begin

Declaration :

TimerDelay.begin(mos_uint8_t timer_no , float timer_delay_ms, void (*timer_delay_callback)(void));

TimerDelay.begin function activates a callback function, which is called after every specified delay. This callback function can be used to implement some periodic action. This function has three inputs :

a) Timer no : input values could be TIMER_DELAY0, TIMER_DELAY1, etc. These values are platform dependent. Like Atmega328p or arduino uno has three timers, namely TIMER_DELAY0, TIMER_DELAY1 and TIMER_DELAY2. However, TIMER1 is used by OS scheduler. Hence TIMER_DELAY1 is not available unless user changes TIMER of OS scheduler. To check available values you can enable TIMER_DELAY_HELP_ENABLE macro by adding following lines in config.h file :

#define TIMER_DELAY_HELP_ENABLE

b) Callback interval in milli-seconds : This input is float type. Minimum delay is 0.1 milli-second. If value less than this is passed, it will take it as 0.1 milli-second delay only.

c) Pointer to callback function : Pointer to callback function should be passed here. Function type should have void input and void output.

An example function call is :

TimerDelay.begin(TIMER_DELAY0,1.5,&callback_function);

Above function uses TIMER0, callback interval is 1.5 milli-second and callback_function is the function to be called after every 1.5 milli-second. Callback_function will be periodically called till Timer Delay ends using TimerDelay.end function.

2.2 TimerDelay.end

Declaration :

TimerDelay.end (mos_uint8_t timer_no);

This function is used to deactivate specified timer delay. It has only one input which is Timer no.

Home <<Prev 10 Next>>