You are currently viewing Interface PS/2 with PIC16F877A PIC Advanced Development Board

Interface PS/2 with PIC16F877A PIC Advanced Development Board

Spread the love

PIC16f/18F Advanced Development 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 Advanced Development 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.

image of PS/2 connector

Interfacing PS/2

Fig. 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.

Interfacing PS/2 to Microcontroller

Fig-Interfacing PS/2 to Microcontroller

Interfacing PS/2 with PIC16F877A

The PIC16F/18F Advanced Development 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

PS/2 Keyboard

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

#define CLOCK RC1
#define DATA RC0
#define FOSC 10000
#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()
//Pull up the inputs to High Serial_init();
//Input capture utility in Pic micro Enabled DelayMs(250);
//Clear the Hypherterminal;
printf("PS2 Keyboard Demo:\n\r");
//Scan for a incoming data if(KeyVal>85 || KeyVal0)
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 Advanced Development 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 toPIC16f/18F Advanced Development Board. Akeyboard is connected with PIC16f/18F Advanced Development 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,

EDTurn on/off Num Lock, Caps Lock, and Scroll Lock LEDs
EEEcho. Upon receiving an echo command, the keyboard replies with the same scan code โ€œEEโ€.
F3Set 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.
FEResend. Upon receiving a resend command, the keyboard resends the last scan code sent
FFReset. 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.

How to create & Debug a Project in Mplab using PIC16F using Hi-Tech Compiler.

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.