How to Interface PS/2 with LPC2148 ARM

PS2 Interfacing With ARM7 Primer

Tags: ARM7 LPC2148 Primer Board, PS/2 Interfacing , interfacing PS/2 port to microcontroller, Interfacing PS/2 with LPC2148, PS/2 keyboard connector, Circuit Diagram to Interface PS/2 with LPC2148,, C Program to control PC through PS/2, C Program to interface PS/2 using LPC2148, Source Code o interface PS/2 using LPC2148
Overall rating
ARM7 LPC2148 Primer Board

The ARM7 LPC2148 Primer 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 serial port.

NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 32-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 LPC2148

The ARM214X Primer 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 ARM7 kit. PS/2’s DATA (P8) and CLK (P10) lines connected to LPC2148 I/O Lines.

Pin Assignment with LPC2148

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 ARM7-2148 in this case.

Circuit Diagram to Interface PS/2 with LPC2148


Source Code

The Interfacing PS/2 with LPC2148 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 Keil 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 LPC2148


Title : Program to control PC through PS/2




#include<LPC214x.h>     // Define LPC2148 Header File

#include <stdio.h>

#include "ps2.h"

#include "Serial.h"

#include "Utility.h"


unsigned char raw_data[16],raw_pos;

unsigned long n;


int main (void)



   PINSEL0 = 0;

   PINSEL1 = 0;

   PINSEL2 &= 0x0000000C;

UART0_init(38400/*bps*/,15000/*kHz*/,length_8_bit,        stop_bit_1, parity_disable, parity_odd);

   U0THR = 'A';

   // Init PS/2 Keyboard

   Ps2_Init(&IOPIN1, 16/*CLK*/, 17/*DATA*/);


   // Delay 100ms, Wait for keyboard to finish



   // Set Keyboard Delay and Typematic Rate

   Ps2_Adjust_Rate(TR_Delay_750ms, TR_Rate_10);






      Ps2_Data_Read(&raw_pos, raw_data);

      if (raw_pos != 0)


         for (n=0; n<raw_pos; n++)


            UART0_WriteNum ("0x", raw_data[n]);

            UART0_sendchar (' ');


         UART0_sendstring ("\n");


   }   while (1);




To compile the above C code you need the KEIL 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 Keil, 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 LPC2148 Primer Board.

The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148 through UART0.

Testing the PS/2 with LPC2148

Give +3.3V power supply to LPC2148 Primer Board; a keyboard is connected with LPC2148 Primer 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 Keil. If you want to see more details about debugging just see the videos in below link.

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 LPC2148 Primer Board.
       - Tutorial of how to create & Debug a project in Keil.

Join the World's Largest Technical Community

we respect your privacy.