Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/23 07:30] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 10:51] (current) – ktokarz |
---|
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 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). | 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 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). |
<figure timers1> | <figure timers1> |
{{ :en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing-page-1.drawio.png?100 | Blocking call: use of the delay}} | {{ :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> | <caption>Blocking call: use of the delay()</caption> |
</figure> | </figure> |
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}}. | 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> | <figure timers2> |
{{ :en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing-page-2.drawio.png?220 | Non-blocking call: use of the millis}} | {{ :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> | <caption>Non-blocking call: use of the millis()</caption> |
</figure> | </figure> |