How to Interface PS/2 with AVR Slicker

Interfacing PS2 with AVR Slicker

Tags: Interfacing PS/2 Keyboard with AVR, Circuit Diagram to Interface PS/2 keyboard with AVR, C Program to interface PS/2 using Atmege8535,c source code to interface ps2 with AVR microcontrollers,
Overall rating
AVR Slicker Board

The AVR Slicker board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports In-system programming (ISP) which is done through Parallel/Printer port.

ATMEL’s AVR (Atmega8535), AVR Slicker Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.

PS/2 (Play Station 2)

The PS/2 connector is a round shape of 6-pin Mini-DIN connector used for connecting some keyboards and mice to a PC compatible compute r system.


Interfacing PS/2

Fig. 1 shows how to interface PS/2 port to microcontroller. The PS/2 bus includes both clock and data. Both a mouse and keyboard drive the bus with identical signal timings and both use 11-bit words that include a start, stop and odd parity bit. However, the data packets are organized differently for a mouse and keyboard. Furthermore, the keyboard interface allows bidirectional data transfers so the host device can illuminate state LEDs on the Keyboard.


Fig. 1 Interfacing PS/2 to Microcontroller

Interfacing PS/2 with AVR Slicker

The AVR Slicker Kit includes a PS/2 port and the standard 6-pin mini-DIN connector, labeled U11 on the board. User can connect PS/2 Devices like keyboard, mouse to the AVR kit. PS/2’s DATA (PORTD.7) and CLK (PORTD.6) lines connected to AVR I/O Lines.

Pin Assignment with AVR






Turn ON TXD, RXD Pins of CONFIG-I switch SW24.

Put a Jumper between the pins CLK and PD6, DATA and PD7 of J9

Connect a PS2 keyboard with U4-PS2 connector

Connect Serial cable between USART Section on the Board and PC.

Press RESET once.

Press a Key in PS2 key-board, the key will be displayed in HyperTermi- nal


Only Alphanumeric keys and some special keys will be displayed.





Both a PC mouse and keyboard use the two-wire PS/2 serial bus to communicate with a host device, the AVR Slicker in this case.

Circuit Diagram to Interface PS/2 with AVR Slicker


Source Code

The Interfacing PS/2 with AVR program is very simple and straight forward, which controls keyboard and display the keys position into PC through serial port. The C programs are written in CodeVisionAVR software. Some time delay occurring for a single data transmitted into PC. The baud rate used in microcontroller is 9600.

C Program to interface PS/2 using AVR Slicker


Title : Program to control PC through PS/2







#define PS2_CLK   PIND.6       

#define PS2_DATA  PIND.7


#define TXIF        UCSRA.5

#define FOSC        16000000L                                           //Oscillator Frequency   16Mhz 

#define USART_Baud  9600                                                

#define BaudH       ((unsigned int)(((FOSC/16/USART_Baud)-1) >> 8))

#define BaudL       ((unsigned int)(((FOSC/16/USART_Baud)-1) & 0xFF))



flash unsigned char Key[]={"0F FFFFF FFFFt`  as cQ1   ZSAW2  CXDE43   VFTR5  NBHGY6   MJU78  ,KIO09  ./L;P-   ' [=\\"};

unsigned char KeyVal,i=0;

void Serial_init();

unsigned char Scan_Data();


void main()


        DDRB = 0x00;







                if(KeyVal>85 || KeyVal<20 || KeyVal==0x0e)




                        if(KeyVal==0x5d || KeyVal==0x5b)












unsigned char Scan_Data()


    unsigned char Data=0,temp;















void Serial_init(void)


     UCSRA = 0x00;                //Status Register

     UCSRB = 0x18;                //Transmit Enable,Receive enable,interrupts disabled   

     UCSRC = 0x86;                //8 databits,no parity,single stop   

     UBRRH = BaudH;

     UBRRL = BaudL;    


     printf("PS2 Keyboard Demo:\n\r");       





void putchar(unsigned char Data)


    UDR = Data;     



To compile the above C code you need the CodeVision AVR software. The software has it’s own IDE and built-in AVR gcc- Compiler. They must be properly installed and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In CodeVision AVR software, you can develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without microcontroller Board.

The AVR Code Blaster software is used to download the hex file into your microcontroller through Parallel port.

Testing the PS/2 with AVR Slicker

Give +12V power supply to AVR Slicker Board; a keyboard is connected with AVR Slicker Board.

If a key is pressed and held, the keyboard repeatedly sends the scan code every 100 ms or so. When a key is released, the keyboard sends an “f0” key-up code, followed by the scan code of the released key. the keyboard sends the same scan code, regardless if a key has different shift and non-shift characters and regardless whether the shift key is pressed or not.

The host determines which character is intended. Some keys, called extended keys, send an “e0” ahead of the scan code and furthermore, they might send more than one scan code.

When an extended key is released, an “e0 f0” key-up code is sent, followed by the scan code.


The host can also send commands and data to the keyboard. Below figure provides a short list of some often-used,




Turn on/off Num Lock, Caps Lock, and Scroll Lock LEDs


Echo. Upon receiving an echo command, the keyboard replies with the same scan code “EE”.


Set scan code repeat rate. The keyboard acknowledges receipt of an “F3” by returning an “FA”, after which the host sends a second byte to set the repeat rate.


Resend. Upon receiving a resend command, the keyboard resends the last scan code sent


Reset. Resets the keyboard


The PS/2 output is not working; you first check the keyboard is working. Otherwise you just check the port output in debugging mode in CodeVisionAVR.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,
       -  User Manual of AVR Slicker Board.
       -  How to create & Debug a Project in CodeVision AVR

Join the World's Largest Technical Community

we respect your privacy.