Keyboard Side Software:

 

            The software on the keyboard side has two main tasks:  It listens to the data coming from the keyboard and it sends it out to the transmitter through the UART.  The keyboard generates a clock on the Clock line and sends its data on the Data line.  Whenever the clock is goes low, 1 bit of data is present on the Data line and the microcontroller reads it.  To read the data line at the appropriate times, we use edge triggered external interrupt 2 to trigger on each falling edge.  When the interrupt executes, one data bit is shifted into a register.  Since the ps/2 protocol sends a start bit, followed by 8 data bits, then a parity and a stop bit, we only shift in the data bits into a register.  Once we have the 8 data bits, we place the byte into a queue.  To make sure that the AGC on the receiver end can read the data bytes correctly, we place a stream of eleven bytes that are all 0xAA and then the byte 0xFF into the queue before each data byte.  The 0xFF byte indicates that the next byte is a data byte and should not be ignored.  Furthermore, we maintain timer2 to keep track of how long it has been since we transmitted some data.  If more than approximately 25mSec has elapsed, we transmit a stream of 0xAA, followed by one 0xFF and one 0xAA.  This keeps the AGC on the receiver end properly set to receive data.  The ps2 protocol also allows the computer to talk to the keyboard.  To do this, we also use the external edge triggered interrupt to place a data bit on the DATA line at the appropriate times so that the keyboard can get it.  Although the interrupt code appears quite long, there is only a piece of it that is executed every time the interrupt is taken.  The rest of the time, the program is inside the main while loop where we are constantly checking if the queue is empty and if the UART is free to send data.  If there is data in the queue and the UART is available, we transmit the data bytes found in the queue.