This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2023/11/17 16:50] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 10:51] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Timing ====== | ====== Timing ====== | ||
+ | {{: | ||
+ | Writing code that handles interrupts from internal peripherals, | ||
- | Writing code that handles interrupts from internal peripherals, | + | ==== Time-related functions ==== |
+ | Because this chapter presents just an introduction to programming, | ||
** Delay **\\ | ** Delay **\\ | ||
The simplest solution to make functions work for a particular time is to use the '' | The simplest solution to make functions work for a particular time is to use the '' | ||
- | '' | + | The '' |
The blinking LED code is a simple demonstration of delay functionality: | The blinking LED code is a simple demonstration of delay functionality: | ||
Line 16: | Line 19: | ||
Using '' | Using '' | ||
<figure timers1> | <figure timers1> | ||
- | {{ : | + | {{ : |
< | < | ||
</ | </ | ||
Line 23: | Line 26: | ||
** Millis **\\ | ** Millis **\\ | ||
- | '' | + | The '' |
<figure timers2> | <figure timers2> | ||
- | {{ : | + | {{ : |
< | < | ||
</ | </ | ||
Line 32: | Line 35: | ||
<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, | + | pinMode (LED_BUILTIN, |
} | } | ||
void loop() { | void loop() { | ||
- | currentTime = millis(); //Read and store current time | + | currentTime = millis(); |
//Calculate passed time since the last state change | //Calculate passed time since the last state change | ||
Line 53: | Line 59: | ||
if (currentTime - previousTime >= stateChangeTime) { | if (currentTime - previousTime >= stateChangeTime) { | ||
| | ||
- | previousTime = currentTime; | + | previousTime = currentTime; |
- | ledState = !ledState; //Change LED state to oposite | + | ledState = !ledState; |
digitalWrite(LED_BUILTIN, | digitalWrite(LED_BUILTIN, | ||
} | } | ||
Line 60: | Line 66: | ||
</ | </ | ||
- | === 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: 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. | 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. | ||