Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2023/11/21 22:24] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:timing [2024/05/27 10:51] (current) – ktokarz |
---|
====== 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 from internal peripherals, for example, timers, is possible but depends strongly on the hardware. |
| |
** 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: |
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?150 | 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> |
| |
** Millis **\\ | ** 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 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?300 | 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> |