Page 157 - Embedded Microprocessor Systems Real World Design
P. 157
Switch and Other Contact Closures
A switch closure such as relay contacts or a push-button switch will “bounce.” Always.
A switch “bounces” when opening or closing and takes the form of repeated open-
ings and closings of the contacts. If a switch is not debounced in hardware, it usually
will need to be debounced in software. Most mechanical switches finish bouncing
in 10 to 20 milliseconds (ms). The logic to debounce a switch is as follows:
Switch closure detected:
Wait 30 ms.
Check switch again. If open, it was bounce on opening. If still closed, it was a
valid switch closure.
Hard Deadlines Versus Soft Deadlines
An old saying in the embedded world is that the right answer, late, is the wrong
answer. Nearly all embedded systems have some type of deadline within which
all tasks must be performed. These deadlines can be broadly divided into two
categories: hard deadlines and soft deadlines.
A hard deadline is something that absolutely must occur by a certain time or things
just won’t work right. Examples include reading a byte from a UART before the
next byte is received and overwrites it. If the software doesn’t make it in time, the
first byte will be lost.
A soft deadline is one that can be missed occasionally, but the average rate must
keep up with the requirement. An example of this would be a UART that has a
large FIFO to store incoming bytes. The software doesn’t have to get every byte out
of the FIFO before the next byte is received. As long as the average read rate is faster
than the rate at which bytes are received, the system will work correctly. Note that
the soft deadline can become a hard deadline if things fall behind. Once the UART
FIFO gets full, at least one byte must be read before the next byte is received or
data will be lost.
Another example of a soft deadline is things that can be imprecise without
affecting system operation. A 30ms switch debounce can vary quite a bit and still
work. If your software is a few milliseconds late updating a display, the user isn’t
going to notice.
Dangerous Independence
An earlier example showed a protocol converter with parallel, independent
processes. Independent processes are common in embedded systems, but it is also
138 Embedded Microprocessor Systems