3.3.1.1 Busy Polling

This technique keeps monitoring the state of a button, and a change of state causes the code exit a loop. For example, the following code waits for a button to be pressed, and then released, before it calls button_event:

while (PINA & (1 << 5));
// button is now pressed
while (!(PINA & (1 << 5)));
// button is now released
button_event();

The two loops are busy loops that do not do anything inside. The first loop exits when the button is pressed, whereas the second loop exits when the button is released. You can insert another function call between the loops if you need to respond to the ``button is being pushed'' event.

Busy polling is relatively easy to code, but it suffers from one major problem: it is wasteful of processing resources. In addition, it is difficult to extend the code, due to its structure, to detect and respond to edge events of multiple buttons.



Copyright © 2006-02-15 by Tak Auyeung