Category Archives: FreeRtos

FreeRTOS – Making a clock and timer

Timer is one of the most important functions in any Embedded Project. Making a timer in FreeRTOS is an easy task. Lets have a look at it on FreeRTOS .

That looks like a lot! ok lets simplify things! First, obvious thing is to create a timer which can be done with : xTimerCreate().

TimerHandle_t xTimerCreate

( const char * const pcTimerName,

  const TickType_t xTimerPeriod,

  const UBaseType_t uxAutoReload,

  void * const pvTimerID,

  TimerCallbackFunction_t pxCallbackFunction );

So what is the meaning of each variable. pcTimerName is name of the timer! like “Timer 1” or “Timer” or “One Second Timer”.

xTimerPeriod is the period of the timer which is specified with ticks and as it is stated , the macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds to a time specified in ticks. For example, if the timer must expire after 100 ticks, then simply set xTimerPeriod to 100. Alternatively, if the timer must expire after 500ms, then set xTimerPeriod to pdMS_TO_TICKS( 500 ). pdMS_TO_TICKS() can only be used if configTICK_RATE_HZ is less than or equal to 1000. The timer period must be greater than 0.

uxAutoReload, If uxAutoReload is set to pdTRUE, then the timer will expire repeatedly with a frequency set by the xTimerPeriod parameter. If uxAutoReload is set to pdFALSE, then the timer will be a one-shot and enter the dormant state after it expires.

pvTimerID, An identifier that is assigned to the timer being created. Typically this would be used in the timer callback function to identify which timer expired when the same callback function is assigned to more than one timer. In this example as there is only one timer and callback function, we set it to 0.

pxCallbackFunction, The function to call when the timer expires. Callback functions must have the prototype defined by tmrTIMER_CALLBACK, which is: void vCallbackFunction( TimerHandle_t xTimer );

So how define the callback function? lets say our callback function is “Yoda”. Then it will be define as:

void Yoda(TimerHandle_t timer) 
{

// Do something 

}//END

what i like is to have a “clock” which starts when the device is turned on and keep records of second, minutes and hour. Therefor, it is possible to take an action on different time span using one timer. It sounds to be an easy task to do. Make a timer that calls every second , then count! I am using Arduino core for ESP32 and PlatformIO in Visual Studio Code . Oh and other details:

#define LEVEL Intermediate

#define MCU ESP32-WROOM-32D

#define Board WEMOS-Lolin // This one comes with OLED display

Here is The code :

#include <Arduino.h>
int Second=0;
int Minute=0;
int Hour=0;


void timer1_callback(TimerHandle_t timer) {
  
  Second++;
           

         if (Second > 59){
           Minute++;
           Second=0;
         }
         
         if (Minute >59){
           Hour++;
           Minute=0;
         }

         if (Hour >23){
           Hour=0;
         }

    Serial.print("Time is: ");
    Serial.print(Hour);
    Serial.print(" : ");
    Serial.print(Minute);
    Serial.print(" : ");
    Serial.println(Second);


}// END Defining the callback function 

void setup() {

Serial.begin(115200);

TimerHandle_t timer1 = xTimerCreate("1 second timer", pdMS_TO_TICKS(1000), pdTRUE, 0, timer1_callback);

xTimerStart(timer1, 0);

}// END Setup 

void loop() {
  
}// END Loop 

And here is the output:

Well , lets make the output looks better and add “0” on the left when the hour, minute or second is less than 10.

if (Hour < 10)
    {
      Serial.print("0");
    }

Adding it to the code, it will be :

#include <Arduino.h>
int Second=0;
int Minute=0;
int Hour=0;


void timer1_callback(TimerHandle_t timer) {
  
  Second++;
           

         if (Second > 59){
           Minute++;
           Second=0;
         }
         
         if (Minute >59){
           Hour++;
           Minute=0;
         }

         if (Hour >23){
           Hour=0;
         }

    Serial.print("Time is: ");

    if (Hour < 10)
    {
      Serial.print("0");
    }

    Serial.print(Hour);
    Serial.print(" : ");

    if (Minute < 10)
    {
      Serial.print("0");
    }

    Serial.print(Minute);
    Serial.print(" : ");

    if (Second < 10)
    {
      Serial.print("0");
    }

    Serial.println(Second);


} // END Defining the callback function 

void setup() {

Serial.begin(115200);

TimerHandle_t timer1 = xTimerCreate("1 second timer", pdMS_TO_TICKS(1000), pdTRUE, 0, timer1_callback);

xTimerStart(timer1, 0);

}// END setup 

void loop() {
  
}// END loop 

Now it looks like :

Ok!! we got our clock and timer!

Lets make it more interesting. A simple challenge!!. change the code to do something in every 10 second, 5 minute and 1.5 hour!

Also, if you find it you can do this in different way, please do not hesitate to share your code.

Happy Coding!