Interfacing ps2 keyboard to atmega128/atmega64

Interfacing  ps2 keyboard to Uniboard(Atmega64/Atmega128)

Hardware needed :

  1. Uniboard
  2. Ps2 female connector
  3. 4 burg wires

AVR  Peripherals used:

  1. External interrupts
  2. Io ports
  3. UART(for debugging)

Ps2 key board implements a bidirectional synchronous serial communication .Most of the ps2 keyboards use 6-pin Mini-DIN (PS/2) connector which is shown in the following image.

Pin configuration of 6-pin Mini-DIN (PS/2):

  1. Data
  2. NC
  3. Ground
  4. Vcc(+5v)
  5. Clock
  6. NC

                                        Ps2 female connector 

All the data originating from the keyboard is transmitted one byte at time using the synchronous serial communication .when a key is pressed on the keyboard a unique scan code associated with the key is shipped out using the synchronous serial communication i.e. using the clock and data lines.

The keyboard generates the clock in this case whose frequency is 2000hz i.e. baud rate=2000 bits per second.

On every falling edge of the clock generated by the keyboard on the clock pin a new bit is shipped out on to the data pin which can be sampled to read the data bit by bit.

Ps2 keyboard used a serial frame of 11 bits each

  1. 1 start bit (Active Low)
  2. 8 data bits ( LSB First)
  3. 1 parity (Odd Parity)
  4. 1 Stop bit (Active High)

The timing diagram of the synchronous serial communication used by the keyboard is shown bellow

Fine so as every key has unique scan code which will be shipped out serially through the data pin bit bit by bit on every falling edge of the clock that’s it is it that simple…?.

No. To enable the host device to know the press and release of the key the keyboard sends the scan code twice in the following fashion.

Key pressed                                                                                                   key Released
Scan Code=11 bits                                                            Break Code + Scan Code=22 bits

 Total( key press + key released)=33 bits.

Break code is same for all the keys and  it indicate the release of the key and now to identify which key is released scan code corresponding to the key released is also shipped out again.

Ok enough theory let us do some bang bang on the keyboard now (write code)

Its very simple we just need to implement the synchronous serial communication using an external interrupt and an io pin.

Ok now tell me what should I do with external interrupt and  i/0 pin

Step one

Configure the external interrupt for falling edge and connect the clock pin to it. Since on every key press 33 bits will be shipped out of the data pin serially on the falling edge of the clock which is connected to the external interrupt pin .

On each key event(press +released) the external interrupt   ISR  will be executed 33 times of this 33 only first 11 times for which the ISR gets Executed is of our interest  to decode the scan code.

The First bit can be neglected since it is the start bit and the last two can also be neglected If you want to quickly decode the scan code .

The code to implement  the above mentioned algorithm can be found in  Ps2_lib.h

Function list:

  1. 1.       void Ps2_Init(void);

This function does the initialization job for the external interrupt and the io pin

  1. 2.       INT8U Ps2_Get_Data(void);

This function can be used to read data from the ps2 keyboard.it will wait till a key  is pressed on the keyboard and then returns the character corresponding to the key.

NOTE:

To change the  hardware configuration of the lib please look into the ps2_lib.h

/*******************************HARDWARE CONFIGRATION****************************************/

///DATA PIN CONNECTED TO ANY GPIO PIN

#define datapinno 0x07                                        // Value of data port when the data pin is high

#define dataport PORTB                                        // Enter the data port here

#define dataddr DDRB

#define datapin PINB

///CLOCK PIN CONNECTED TO EXTERNAL INTERRUPT

#define external_interrupt_no 6

/******************************************************************************************/

—————Bug reports and hints for improving these pages are very welcome——————

project code link for AVR-GCC compiler(winavr)

 Download Code            

Video :

 

Posted on December 10, 2011, in Microcontrollers-AVR and tagged , , . Bookmark the permalink. 5 Comments.

Leave a comment