You are on page 1of 4

PS/2 Keyboard Interface for PIC uC (PIC16F887)

We need to get various kinds of inputs for our microcontroller circuit. If we need to use key board to input data,we can either use simple buttons or 4x4 keypadetc. In here Im going to show you how to use a standard PS/2 computer keyboard for this purpose. It is so cheap and readily available.It is easy to replace a keyboard even for a laymen in an error.

Here Im using a Mercury branded PS/2 keyboard I got 10 years back.I just cut the cable and got wires di rectly connected to the circuit.You may find a female PS/2 keyboard socket from ebay or from an old motherboard and use in your projects.

Here is the pinout of a PS/2 keyboard and how i connected to the uC. uC pins are showed in red and the colours of wires on my keyboard are shown in brackets.

Here is the circuit I used to test this.It consists of a 16F887 uC and a simple 16*2 LCD display.You can find additional information on my previous post.

Here is the connection between keyboard and the circuit

I used MikroC built in PS/2 keyboard library for this purpose. You can find more information from here.

I wrote a simple program to display the ASCII value of the pressed key and to show pressed keys in the LCD. You can change the code as you like to get key presses as you wish. The PS/2 library help page is much useful in this.Please note the bolded lines are for keyboard stuff.

sbit LCD_RS at RB5_bit; sbit LCD_EN at RB4_bit; sbit LCD_D4 at RB3_bit; sbit LCD_D5 at RB2_bit; sbit LCD_D6 at RB1_bit; sbit LCD_D7 at RB0_bit; sbit LCD_RS_Direction at TRISB5_bit; sbit LCD_EN_Direction at TRISB4_bit; sbit LCD_D4_Direction at TRISB3_bit; sbit LCD_D5_Direction at TRISB2_bit; sbit LCD_D6_Direction at TRISB1_bit; sbit LCD_D7_Direction at TRISB0_bit;

sbit PS2_Data at RA7_bit; sbit PS2_Clock at RA6_bit; sbit PS2_Data_Direction at TRISA7_bit;


2

sbit PS2_Clock_Direction at TRISA6_bit; unsigned short keydata = 0, special = 0, down = 0; void main() { char txt[7]; short place=1; osccon = 0x70; ansel = 0; anselh = 0; trisa = 0; trisb = 0; trisc = 0; trisd = 0; Lcd_Init(); Ps2_Config(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1,"ASCII:"); for(;;){ portd.f4=1; delay_ms(10); portd.f4=0; delay_ms(10); if (Ps2_Key_Read(&keydata, &special, &down)) { IntToStrWithZeros (keydata,txt); Lcd_Out(1,7,txt); Lcd_Out(2,place,&keydata); Lcd_Out(2,place+1," "); if (place==16){ place=1; } else{ place=place+1; } } delay_ms(10) ; } }
I used following configuration options for this. Please note Im using 8Mhz internal oscillator in my projects.

You might also like