Page 450 - Programming Microcontrollers in C
P. 450
Keyboard 435
bits, as does the row. These bits are packed into a single 8-bit value
by the instruction
c=*data++=(~(KPDRB.COLUMNS<<4 | (KPDRB.ROWS&0xf))) & 0xff;
The column value is shifted left by four bits and the result is bit wise
ANDed with the row value. These values are the ones-complement
of the values we want, so the ones-complement is taken. The final
result is ANDed with a mask of 0xff to delete any bits outside of the
eight bits needed for the final result. This result is stored in the array
back in the calling program. Also at this time, the result is stored in a
local variable for later use. The count of the number of characters
entered into the array are incremented. If this value exceeds the length
of the array, there is an error and the input is completely terminated
when the program is restarted at its very beginning.
#define COL_0 0x0e
/* key board interrupt service routine */
void kb_isr(void)
{
UHWORD column,input;
static BYTE c;
if(KPSR.KPKD) /* if a key has been depressed */
{
for(column=COL_0;column!=0xf;column=(column<<1 |0x1) & 0xf)
{
KPDRB.COLUMNS=column;
/* kill some time */
if(KPDRB.ROWS!=0xff)
{
c=*data++=(~(KPDRB.COLUMNS<<4 |
(KPDRB.ROWD&oxf))) & 0xff;
if(++n>=leng) /* data exceed the array */
{
puts(“input data overflow\n”);
_start(); /* bad error, restart the
program */
}
}
}
*data=0xff; /* needed to terminate decode */
if(c==0x11) /* ‘=’ reached the end of the input */

