Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2023/07/11 21:19] pczekalskien:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 10:51] (current) ktokarz
Line 1: Line 1:
-==== Timing ====+====== Timing ====== 
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ 
 +Writing code that handles interrupts from internal peripherals, for example, timers, is possible but depends strongly on the hardware. 
  
-Writing code that handles interrupts that come 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.+==== Time-related functions ==== 
 +Because this chapter presents just an introduction to programming, some essential timing functions will be shown.\\
  
-=== Delay ===+** Delay **\\
 The simplest solution to make functions work for a particular time is to use the ''delay()'' ((https://www.arduino.cc/reference/en/language/functions/time/delay/)) function. The simplest solution to make functions work for a particular time is to use the ''delay()'' ((https://www.arduino.cc/reference/en/language/functions/time/delay/)) function.
-''delay()'' function halts program execution for the time specified as the argument (in milliseconds).+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: The blinking LED code is a simple demonstration of delay functionality:
Line 14: Line 17:
   delay(1000);                       //Stop program for a second   delay(1000);                       //Stop program for a second
 </code> </code>
-Using ''delay()'' is convenient but has a severe drawback: the algorithm is halted, and only interrupts (or tasks in the background) are executed. Some jobslike receiving serial transmissions and outputting set PWM values, continue to work. The alternative to using delay is to switch to the non-blocking method, based on timing with the use of ''millis()'' as presented below.+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 {{ref>timers1}}. Some taskse.g. receiving serial transmissions, networking, and outputting set PWM values, continue to work as background tasks, using interrupts or task management (such as FreeRTOS). 
 +<figure timers1> 
 +{{ :en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing-page-1.drawio.png?200 | Blocking call: use of the delay}} 
 +<caption>Blocking call: use of the delay()</caption> 
 +</figure> 
 + 
 +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()'' (( https://www.arduino.cc/reference/en/language/functions/time/millis/)) 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 {{ref>timers2}}. 
 +<figure timers2> 
 +{{ :en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing-page-2.drawio.png?350 | Non-blocking call: use of the millis}} 
 +<caption>Non-blocking call: use of the millis()</caption> 
 +</figure>
  
-=== Millis === 
-''millis()'' (( https://www.arduino.cc/reference/en/language/functions/time/millis/)) 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 simply 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, here we check if the desired time has passed. Meanwhile, we can handle other tasks instead of blocking execution. 
 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: 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:
  
 <code c> <code c>
-//Unsigned long should be used to store time values as the millis() returns a 32-bit unsigned number+//Unsigned long should be used to store time values  
 +//as the millis() returns a 32-bit unsigned number
 //Store value of current millis reading //Store value of current millis reading
 +
 unsigned long currentTime = 0;  unsigned long currentTime = 0; 
 //Store value of time when last time the LED state was switched //Store value of time when last time the LED state was switched
 +
 unsigned long previousTime = 0;  unsigned long previousTime = 0; 
  
-bool ledState = LOW; //Variable for setting LED state+bool ledState = LOW;              //Variable for setting LED state
  
 const int stateChangeTime = 1000; //Time at which switch LED states const int stateChangeTime = 1000; //Time at which switch LED states
  
 void setup() { void setup() {
-  pinMode (LED_BUILTIN, OUTPUT); //LED setup+  pinMode (LED_BUILTIN, OUTPUT);  //LED setup
 } }
  
 void loop() { void loop() {
-  currentTime = millis(); //Read and store current time+  currentTime = millis();         //Read and store current time
  
   //Calculate passed time since the last state change   //Calculate passed time since the last state change
Line 42: Line 59:
   if (currentTime - previousTime >= stateChangeTime) {    if (currentTime - previousTime >= stateChangeTime) { 
      
-    previousTime = currentTime; //Store new LED state change time +    previousTime = currentTime;   //Store new LED state change time 
-    ledState = !ledState; //Change LED state to oposite+    ledState = !ledState;         //Change LED state to oposite
     digitalWrite(LED_BUILTIN, ledState); //Write current state to LED     digitalWrite(LED_BUILTIN, ledState); //Write current state to LED
   }   }
Line 49: Line 66:
 </code> </code>
  
-=== Sleep Modes === +==== 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: i.e. 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.+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.1689110394.txt.gz · Last modified: 2023/07/11 18:19 (external edit)
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