Page 453 - Programming Microcontrollers in C
P. 453
438 Chapter 8 MCORE, A RISC Machine
‘+’, ‘7’, ’8’, and ‘9’. The next row contains ‘-‘, ’4’, ’5’, and ‘6’. The
third row from the top contains ‘*’, ‘1’, ‘2’, and ‘3’. The bottom row
‘/’, ‘0’, ‘.’, and ‘=’. Remember that the right-most column is column
1 in the position and the bottom-most row is row 1. Therefore, one
can build a matrix as shown below that converts the position of the
key to its ASCII value. Now the positions must be indices into a two-
dimensional array rather than the position values. The decode routine
must convert the position values to indices.
The decode routine works on an array that contains several key
positions. This data array is terminated with a character 0xff. The string
is passed as a pointer to the beginning of the string. In the function, the
value of the pointer is copied to another pointer to a type BYTE that is
used in the program. A while loop is entered which will walk the
length of the array that contains all of the key position data. This loop
is terminated when the data 0xff is found in the input array. Each piece
of input data is now split into two parts, the rows and the columns, and
a switch/case statement is used to convert the 1, 2, 4, 8 values that the
input data contains into the 0, 1, 2, 3 values used for indices into the
two-dimensional array. In the event of a multiple key hit, a default
value of 4 is passed to either the row or column. These values are
tested for, prior to access to the keys[][] array. If a 4 is found in
either case, the decode loop is skipped to the next character. This
approach does not prevent errors in decoding, but it does keep control
and allows the procedure to be restarted.
static const BYTE keys[][4]= {{‘=’,’.’,’0',’/’},
{‘3’,’2',’1',’*’},
{‘6’,’5',’4',’-’},
{‘9’,’8',’7',’+’}};
/* the key position decode routine */
static void decode(BYTE *s)
{
int r,c;
BYTE *p=s;
while(*p !=0xff)
{
switch(*p & 0xf)
{
case 8: r=0;

