Interfacing ps2 keyboard to atmega128/atmega64
Interfacing ps2 keyboard to Uniboard(Atmega64/Atmega128)
Hardware needed :
- Uniboard
- Ps2 female connector
- 4 burg wires
AVR Peripherals used:
- External interrupts
- Io ports
- 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):
- Data
- NC
- Ground
- Vcc(+5v)
- Clock
- 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 start bit (Active Low)
- 8 data bits ( LSB First)
- 1 parity (Odd Parity)
- 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.
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. void Ps2_Init(void);
This function does the initialization job for the external interrupt and the io pin
- 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)
Video :
Posted on December 10, 2011, in Microcontrollers-AVR and tagged atmel, avr, ps2keyboard interface. Bookmark the permalink. 5 Comments.
that must be 1 stop bit and not start bit in the end
@Rahul my fault thanks 🙂
Can you re-upload a code ?
https://drive.google.com/file/d/0B9_i3IHV82KTNnRtUDg2ekowakE/edit?usp=sharing
The download link is now up !