|
The Easy Way
by Charles Kosina
Start ý An
Example ý State Machine Control ý Keyboard
Scanning ý Receive Serial Data ý LCD
Module ý Transmit Data ý Debugging
Techniques ý Other Options ý Sources
and PDF
KEYBOARD SCANNING
Figure 2 shows the state machine for
the keyboard. Only three states are needed. State 0 waits for a keystroke
in the 4 ý 4 matrix. The decoded character is then stored in a memory
location and a flag is set. This flag is also used by other tasks.
State 1 then waits for the key to be released. Finally, in state 3
there is a de-bounce delay of about 10 ms before going back to state
0.
 |
| Figure 2ýThe Keyboard Scan
State Machine interrogates a 4 ý 4 keyboard matrix. It is activated
60 times per second. |
Each state machine has a control block.
Listing
4 shows how this is done for
the 8051. There are a few important things to note. Each subroutine
in the jump table must have only one entry and exit (standard programming
practice). And, keep the subroutine short because there are other
tasks waiting for their share of CPU time.
Obviously there is overhead involved.
About seven instructions are executed every time a state is entered
and exited. The number of instructions executed within a state
can vary enormously, from three to 700. So, depending on your
programming style, the particular chip that you use, and how much
processing is done within each state, the overhead could range
from 10% to 50%.
One thing to watch for is that the
ajmp instruction range is only 4 KB. If your program exceeds
this you may get an "out of page" error. To fix this,
replace all the jump table ajmp instructions with ljmp
instructions and the rl a with add a,#3 because
there are 3 bytes per instruction.
Note that the first two instructions
allow the task to be disabled by setting the kb_disable
bit. This prevents a potential race condition where a keystroke
could alter the data that is being accessed by another task.
PREVIOUS
NEXT
Circuit Cellar provides up-to-date information for engineers. Visit
www.circuitcellar.com for
more information and additional articles.
For subscription information, call (860) 875-2199, subscribe@circuitcellar.com
or subscribe online.
ýCircuit Cellar, the Magazine for Computer Applications. Posted with
permission. |