// define the clock and data bits
#define AT_CLK PORTCbits.RC14 // clock on RC14
#define AT_CLK_ENABLE TRISCbits.TRISC14
#define AT_DATA PORTCbits.RC13 // data on RC13
#define AT_DATA_ENABLE TRISCbits.TRISC13
// initialise the AT keyboard clock and data bits for input
void ATkbdInitialise(void)
{
AT_CLK_ENABLE=1; // enable keyboard CLK input
AT_DATA_ENABLE=1; // enable keyboard DATA input
}
// decode the CLK and DATA lines into an integer - polled version
static int ATkbdRead(int debug)
{
int i=0, scanCode=0;;
char data[20]={0};
while(AT_DATA); // wait for start bit - high to low transition
while(AT_CLK); // wait for clock high to low transition
//printf("START BIT");
// read 8 bits of data + parity + stop bit
for(i=0; i< 10; i++)
{
while(!AT_CLK); // wait for clock low to high
while(AT_CLK); // wait for clock high to low
data[i]=AT_DATA+'0'; // read the next data bit
}
if(debug)printf("%s ", data);
// convert the pattern into an 8 bit scan code
for(i=7; i>=0; i--)
{
scanCode=(scanCode<<1) | (data[i]-'0');
}
if(debug)printf("scan code %x ", scanCode);
return scanCode;
}