Interface PS/2 with PIC16F877A

PS2 Interfacing With PIC16F877A Slicker

Tags: Interfacing PS/2 with PIC16F877A, Circuit Diagram to Interface PS/2 with PIC16F877A, C Program to interface PS/2 using PIC16F877A,c source code to interface ps2 with pic microcontrollers,
Overall rating
PIC16F/18F Slicker Board

The PIC16F/18F 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 USB port.

Microchip’s PIC (PIC16F877A), PIC16F/18F 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-Interfacing PS/2 to Microcontroller

Interfacing PS/2 with PIC16F877A

The PIC16F/18F 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 PIC16F/18F kit. PS/2’s DATA (RC0) and CLK (RC1) lines connected to PIC16F877A I/O Lines.

Pin Assignment with PIC16F877A


6PIN MINI Connector



I/O Lines











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

Circuit Diagram to Interface PS/2 with PIC16F877A


Source Code

The Interfacing PS/2 with PIC16F877A 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 Mplab 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 PIC16F877A


Title : Program to control PC through PS/2







#define CLOCK RC1

#define DATA  RC0

#define FOSC       10000    //10Mhz==>10000Khz

#define BAUD_RATE   9.6        //9600 Baudrate

#define BAUD_VAL   (char)(FOSC/ (16 * BAUD_RATE )) - 1;      //Calculation For 9600 Baudrate @10Mhz

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

unsigned char KeyVal,KeyVal2,i=0;

void Pwm_Init(void);

void Capture_init(void);

void Serial_init(void);

unsigned char Scan_Data(void);

void DelayMs(unsigned int);

void main()


   PORTC=0xff;     //Pull up the inputs to High



   Capture_init(); //Input capture utility in Pic micro Enabled


   printf("\033[2J");    //Clear the Hypherterminal;

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



      KeyVal=Scan_Data();    //Scan for a incoming data

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

      //If any special character received





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







      printf("%c",Key[KeyVal]);//Else If the character rcv   

      is a Number or Letter then print the letter or number


      Capture_init();//Initialise once again for next capture




void Capture_init()


   TRISC |=0x03;    //Make PS2 CLK and DATA Pins as Input

   CCP2CON=0x04;    //Capture Every Falling Edge

   T1CON=0x04;      //Timer1 1:1 Prescale, Internal Clock



       CCP2IF=0;        //Clear the Interrupt Flag




void Serial_init()


   TRISC=0xc0;     //RC7,RC6 set to usart mode(INPUT)

       TXSTA=0x24;     //Transmit Enable

       SPBRG=BAUD_VAL; //9600 baud at 10Mhz

       RCSTA=0x90;     //Usart Enable, Continuous receive enable

       TXIF=1;         //Start Transmit



void putch(unsigned char Data)      //trasnmit data




         TXREG = Data;



unsigned char Scan_Data()


    unsigned char Data=0,temp;

    while(CCP2IF==0);   //Wait for First Falling Edge

    CCP2IF=0;     //Clear the Flag--Bcoz this is Start Pulse




        while(CCP2IF==0); //Capture the next 7 bits of data









        return(Data);        //return the data



void DelayMs(unsigned int Ms)     //Delay Routine


   int delay_cnst;






for(delay_cnst = 0;delay_cnst <220;delay_cnst++);     






To compile the above C code you need the Mplab software. They must be properly set up 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 Mplab, you want to 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 PIC16F/18F Slicker Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.

Testing the PS/2 with PIC16F877A

Give +12V power supply to PIC16F/18F Slicker Board; a keyboard is connected with PIC16F/18F 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 Mplab. If you want to see more details just see the videos in below link.

General Information

Join the World's Largest Technical Community

we respect your privacy.