Timing

 General audience classification icon  General audience classification icon
Writing code that handles interrupts from internal peripherals, for example, timers, is possible but depends strongly on the hardware.

Because this chapter presents just an introduction to programming, some essential timing functions will be shown.

Delay
The simplest solution to make functions work for a particular time is to use the delay() [1] function. The delay() function halts program execution for the time specified as the argument (in milliseconds).

The blinking LED code is a simple demonstration of delay functionality:

  digitalWrite(LED_BUILTIN, HIGH);   //Turn the LED on 
  delay(1000);                       //Stop program for a second
  digitalWrite(LED_BUILTIN, LOW);    //Turn the LED off 
  delay(1000);                       //Stop program for a second

Using delay() is convenient but has a severe drawback: the algorithm is halted, and only interrupts (or tasks in the background) are executed. The main algorithm is present in the figure 1. Some tasks, e.g. receiving serial transmissions, networking, and outputting set PWM values, continue to work as background tasks, using interrupts or task management (such as FreeRTOS).

 Blocking call: use of the delay
Figure 1: Blocking call: use of the delay()

The alternative to using delay is to switch to the non-blocking method, based on timing with the use of millis() as presented below.

Millis
The millis() [2] returns the number in milliseconds since MCU began running the current program. Note it has nothing to do with a real-time clock, as most microcontrollers and development boards do not have one. The readings are 32-bit and will roll over in approximately 49 days. millis() can be used to replace delay() but needs some additional coding. Instead of blocking the algorithm, one can check if the desired time has passed. Meanwhile, it is possible to handle other tasks instead of blocking execution, as presented in the algorithm in figure 2.

 Non-blocking call: use of the millis
Figure 2: Non-blocking call: use of the millis()

Here is an example code of blinking LED using millis(). Millis is used as a timer. Every new cycle time is calculated since the last LED state change. If the time passed is equal to or greater than the threshold value, the LED is switched:

//Unsigned long should be used to store time values 
//as the millis() returns a 32-bit unsigned number
//Store value of current millis reading
 
unsigned long currentTime = 0; 
//Store value of time when last time the LED state was switched
 
unsigned long previousTime = 0; 
 
bool ledState = LOW;              //Variable for setting LED state
 
const int stateChangeTime = 1000; //Time at which switch LED states
 
void setup() {
  pinMode (LED_BUILTIN, OUTPUT);  //LED setup
}
 
void loop() {
  currentTime = millis();         //Read and store current time
 
  //Calculate passed time since the last state change
  //If more time has passed than stateChangeTime, change the state of the LED
  if (currentTime - previousTime >= stateChangeTime) { 
 
    previousTime = currentTime;   //Store new LED state change time
    ledState = !ledState;         //Change LED state to oposite
    digitalWrite(LED_BUILTIN, ledState); //Write current state to LED
  }
}

Sleep Modes

Some IoT-dedicated microcontrollers have special features such as sleep modes that hold program execution for a predefined time or unless an external trigger occurs. This can be used for periodic, time-based activities. Its side effect is energy efficiency. The model of this behaviour and its features are very vendor-specific and vary much: e.g. Espressif MCUs have the only option to restart the code. At the same time, STM32 can hold execution and then continue. Because of the variety of models, modes and features, we do not present here any specific solution but rather a general idea.

en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/timing.txt · Last modified: 2024/05/27 10:51 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0