Polling Method in Embedded Programming

Polling operation in embedded programming refers to actively sampling the status of an external device. Polling is most often used in terms of input/output (I/O), and it is the process where the embedded system waits for an external device to check for its readiness or status. For example, we have a simple microcontroller device with a one-push button and LED. When we press the button, the LED will be lit. If we press the button again, then the LED is turned off. The pseudocode code would look something like this:

loop:
if button pressed, toggle LED
repeat

In this program, you can see that the microcontroller always checks the status of the button and then decides the condition for the LED.

A real-life example of the polling method can be when you are working on your homework in your room, and you are waiting for the mailman to deliver an important package. The polling method will be when you go to the front door at set intervals (maybe every 5 minutes) to check whether your package has arrived.

Polling Cycle

Polling basically means that the embedded system will check all status for each device at set intervals, which is called a polling cycle. For example, the embedded system may check the state of a switch every 5 ms, detect any obstacles every 20 ms, and read the room temperature every 5 sec. The optimal polling cycle will vary according to several factors, including the desired speed of response and the overhead (e.g. processor time and bandwidth) of the polling.

Implementation of Polling Method in a Single Task

A simple embedded system, it is usually designed to execute multiple jobs in a single task, such as in the main() function. There are three types of polling methods that can be used in the code, as shown below. Assume that the latency time on each job can be ignored.

Same Polling Cycle

Same Polling Cycle

MultiJobs SamePollingCyc
Figure 1
: Multi-Jobs with Same Polling Cycle

It is easy to implement If all the jobs have the same polling cycle. The following code uses a simple delay function for the polling interval.

loop:
execute Job_A
execute Job_B
execute Job_C
delay for 20 ms
repeat

The following code may use SysTick to get a more accurate time interval:

loop:
get the SysTick value to time1
execute Job_A

execute Job_B
execute Job_C
loop until ( time1 - SysTick) greater than or equal to 20 ms
repeat

© 2024 Air Supply Information Center (Air Supply BBS)