This is an old revision of the document!


ADV1: Using timers to execute code asynchronously

It is advised to use timers that periodically execute a function to handle repeating tasks. Hardware timers work parallel to the CPU and consume few CPU resources. ESP32-S3 has 4 hardware timers, but each timer can execute multiple handlers. The idea of using the timer is to encapsulate a piece of compact code that can be run virtually asynchronously and executed is a precisely-defined time manner. In this scenario, we use a timer to update the LCD screen periodically. We choose a dummy example where Timer 1 is used to increment a value of the byte type, and Timer 2 reads this value and writes it on the LCD. Naturally, a collision may occur whenever two processes are to access a single memory block (variable). It is critical when both processes (tasks, asynchronous functions) are writing to it. However, in our example, the handler executed by Timer 1 only writes to the variable, while Timer 2 only reads from it. In this scenario, there is no need to use mutexes (semaphores). A scenario with detailed description of when to use mutexes is beyond the scope of this example.

Prerequisites

To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:

A standard LCD handling library is attached to the platformio.ini, and it is the only library needed. Timers are integrated into the Arduino framework for ESP32.

lib_deps = adafruit/Adafruit LiquidCrystal@^2.0.2 

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

Task to be implemented

Present on the LCD current value of the byte variable; update the LCD every 3 seconds using one timer. Use another timer to increase this variable every second.

Start

Write starting conditions, i.e. what to do in the beginning, what to pay attention to before beginning, how the mechanical part should look, etc. Include needed compiler configuration, etc.

Steps

Write some extra information if, i.e. some steps are optional; otherwise, cancel this paragraph (but do not remove the header).

Step 1

Describe activities done in Step 1.

Step n

Describe activities done in Step n.

Result validation

On the LCD screen, you should see values starting from number 3, then 6, 9, 12 and so on (=3 every 3 seconds). Note, as byte has a capacity of 256 (0…255), the sequence changes in the following increments once it overflows.

FAQ

This section is to be extended as new questions appear.
When using the printed version of this manual, please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. Provide some FAQs in the following form:
Question?: Answer.

Project information


This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.

en/iot-open/practical/hardware/sut/esp32/adv1_1.1711387885.txt.gz · Last modified: 2024/03/25 17:31 by pczekalski
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