PS2 keyboard based character logger

Introduction

In the present, different types of systems for data communication, such as computers, are widely used in almost every field of work. So now we have super computers, personal computers(desktops / laptops) even in Tabs the very basic idea is to communicate with certain hosts,to do that we simply input raw data, we process it and finally we get the data as a useful information, as a output.

To develop such a system each task should be considered separately and the first task is data logging. So to develop or to observe the task simply we need an input device and a method of communicating with it along with a device to observe the input data. Since these devices are different for different systems interfacing the input device to host of a particular system, plays the major role.

The ‘PS/2 keyboard based character logger’ uses ‘PS/2’ serial communication for communicate with the input device, in this case a keyboard is interfaced with a 16×2  LCD Module using PIC16f877A microcontroller.

Materials and Methods

Materials

PIC16F877A Microcontroller

The microcontroller is the most important part of the system and its controlling whole system, for develop this device  PIC16F877A microcontroller is selected.

It’s belongs to 16-bit MCU family and it has 33 I/O peripherals, these requirement more than enough to implement the design.

Mainly i used capture and interrupt functions available in this microcontroller to develop this system.

The pin diagram of the PIC16F877A microcontroller given below in Figure 1,

                        Figure 1 : Pin Diagram

LCD Module (HD44780)

Dot-matrix LCDs produces in two formats:

1) Fully functional, Alphanumeric Modules.

2) fully-populated Graphic modules.

For this system fully functional, Alphanumeric LCD Module selected.

Alpha-numeric modules display, characters, numerals, symbols and some limited graphics. Interface is achieved via a bi-directional, parallel ASCII data bus. Necessary features such as Character Generation, Display RAM Addressing, Cursor Scrolling, Blanking, and Handshake are all included. These LCD modules range from 8 to 80 characters per line. One, two or four character lines may be chosen.

In summary, these modules are the simplest and most economic means to communicate meaningfully between any micro-system and the outside world.

To develop this system 16×2 hd44780 LCD module selected.

Pin Description and LCD registry instruction menu shown in Table 1 and Figure 2respectively.

Table 1 : LCD Pin Description

Pin No. Symbol Signal Description
 1 Vss Power Supply (Vss=0)
 2 Vdd Power Supply (Vdd=5 V)
 3 Vo Operating voltage for LCD (variable)
 4 RS Register Selection input

High = Data register

Low = Instruction register

Busy flag address counter (for read)

5R/WRead/Write signal input is used to select the read/write modeHigh = Read mode

Low = Write mode 

6EStart enable signal to read or write the data 7 -10DB0 – DB3Four low order bi-directional three-state data bus lines.Used for data transfer between the MCU and the LCD module.

These four are not used during 4-bit operation.

11 – 14DB4 – DB7Four high order bi-directional three-state data bus lines.Used for data transfer between the MCU and the LCD module.

DB7 can be used as a busy flag.

15A Vdd( backlight) 16K Vss

Figure 2  : LCD registry instruction menu

PS/2 Keyboard

History

IBM company first designed the keyboard called “AT keyboard” which refers “Advanced Technology “and then they developed “PS/2 Keyboard” which refers “Personal Systems /2 “keyboard that also compatible with AT keyboard technology.

So actually PS/2 means not the port the port is called as Mini-Din 6 port but as PS/2 port its more popular in society.

To design this system PS/2 keyboard with 104 keys is selected. A unique SCAN code is assigned for every key as shown below in Figure 3

Figure 3 :PS/2 Keyboard Layout

Basically keyboard gives bi-directional communication, so it can configure to communicate to device to host or host to device, here device identified as the PS/2 keyboard while microcontroller identified as the Host.
To implement this device, here only consider “device to Host communication”.
In this system, there are two interfacing methods are used. Which are,
1.       Device to Host interfacing
2.       Host to LCD module interfacing
PS/2 Keyboard communicates with special port called ‘Mini-Din 6’ port, most people called it as the PS/2 port and this port communicates signals using serial communication protocol.
When a key is pressed in the keyboard it sends the corresponding SCAN CODE through PS/2 port to Host. For more information about communication with Host refer 2.2.2 in METHODS section.
PS/2 Keyboard connected to host using Mini-Din 6 Connector (PS/2 Female Connector) as shown in Figure 4

Figure 4 : Mini-Din-6 Female Port

Methods

The Algorithm that used to interface PS/2 keyboard to LCD Module;

 1.       Initialize the Capture module for capture every falling edge of the keyboard clock.

2.       Initialize the Timer1, enable 16 bit timer 1, pre-scale set to 1:1 and sync enable.

3.       After first falling edge clear interrupt flag (to avoid processing unwanted interrupts).

4.       Declare the interrupt Handler on ISR ()

5.       Read the data from keyboard data line (connected to RDO pin) and collect it in a variable

6.       Get the 8 data bits

7.       Compared against case statements

8.       Pass the Output to LCD.

Keyboard To Host Protocol

Keyboards consist large number of keys and it all connected to each other like a matrix of keys, all of these keys are continuously scanning by keyboards internal processer to identify whether a key is being pressed, released, or held down, and if any key is pressed, released, or held down, the processor send appropriate scan code to the host, to identify which key is being, released, or held down. This processor have 16-byte buffer and it can handle any scan code with it.

 There are two different types of scan codes,

1.       Make codes

A make code is sent when a key is pressed or held down. Make codes contains only one hex code,

I.   Extended make codes,

It contains two hex codes, E0 hex common to all extended codes; keyboard sends extended code with E0 hex code and along with another

hex code.

2.       Break codes

A break code is sent when a key is released. Break code contains two hex codes, F0 hex code common to all break code. Keyboard sends break

codes with F0 hex code and along with releasing key make code.

I.   Extended break codes,

It contains three hex codes, E0 hex is common and keyboard sends extended break code with E0 and along with its releasing key break code.

Every key in keyboard is assigned to its own unique make code and break code so the host can determine exactly  which key is active by looking at the corresponding  scan code.

The set of make and break codes shows in the Example in Table 2

Table 2 : Scan code set example (A-C)

KEY

MAKE

BREAK

A

1C

F0,1C

B

32

F0,32

C

21

F0,21

Extended codes

Up ARROW

E0,75

E0,F0,75

Down ARROW

E0,72

E0,F0,

See Attachments  for complete scan code table

Keyboard using bi-directional communication protocol, but the host has the ultimate controlling power to select the direction of communicating because host controlling the keyboard clock and data line states. As in Figure 5 host normally given both lines to high states using pull-up resistors and making C and D port low, if host want to control the lines states it can be done easily by changing C or D port states.

Figure 5 : Host can change the states of the lines, BY changing c and D pin state
When data line and clock line in high state (logic 1) which is also known as idle state of the keyboard is the only state where host allows to keyboard to transmit the data. This is happen only in device to host communication when host need to command the device which is start to host to device communication, host needs to bring the clock line in the keyboard to the low state (logic 0) after that host should take the data line to low state, then only host can send commands to device and device will prepare to accept the commands from the host.
Keyboard sends 11bit frame Scan codes to the Host, host read each bit in the falling edge of the clock.  This 11 bit frame code consist of,
    1.    Start bit –as the first bit (this is always logic 0)
    2.    8 data bits – this contains the hex corresponding hex of the key (LSB first)
    3.    Parity bit – odd parity (for error checking purposes)
    4.    End bit – as the final bit (this is always logic 1)
Figure 6 shows ideal transmission and Figure 7 shows practical transmission

Figure 6 : Ideal general transmission signal

This is the scan code corresponding to A key that i got from a digital CRO.

Figure 7 : practical transmission signal of, ‘a’ key scan code (‘1C’ hex code)

The keyboard clock frequency is 10-16.7 kHz.  The time from the rising edge of a clock pulse to a Data transition must be at least 5 microseconds.  The time from a data transition to the falling edge of a clock pulse must be at least 5 microseconds and no greater than 25 microseconds.

The host may inhibit communication at any time by pulling the Clock line low for at least 100 microseconds.  If a transmission is inhibited before the 11th clock pulse, the device must abort the current transmission and prepare to retransmit the current “chunk” of data when host releases Clock.  A “chunk” of data could be a make code, break code, device ID, mouse movement packet, etc.  For example, if a keyboard is interrupted while sending the second byte of a two-byte break code, it will need to retransmit both bytes of that break code, not just the one that was interrupted.

If the host pulls clock low before the first high-to-low clock transition, or after the falling edge of the last clock pulse, the keyboard/mouse does not need to retransmit any data.  However, if new data is created that needs to be transmitted, it will have to be buffered until the host releases Clock.  Keyboards have a 16-byte buffer for this purpose.  If more than 16 bytes worth of keystrokes occur, further keystrokes will be ignored until there’s room in the buffer.  Mice only store the most current movement packet for transmission.

Capture Method

The CCP modules are identical in operation, so it can precisely capture when event occurs. Capture module contains a 16-bit register which can operate as a 16-bit capture register, see Figure 8

Figure 8 : CCPR1 Register

In Capture mode, CCPR1H:CCPR1L registers captures the 16-bit value of the TMR1 register when an Event occurs on pin CCP1. An event is defined as:

• Every falling edge

• Every rising edge

• Every 4th rising edge

• Every 16th rising edge

For this system event is defined as capture every falling edge on the keyboard clock line. An event is selected by control bits CCP1M3:CCP1M0 in CCP1CON register, Figure 9 shows the CCP1 capture mode in block diagram.

Figure 9 : CCP1 capture mode block diagram

When a capture is made, the interrupt request flag bit, CCP1IF, is set. The CCP1IF bit must be cleared in software. If another capture occurs before the value in register CCPR1 is read, the previous captured value will be lost. So to avoid occurring unwanted capture interrupts, after capture is made CCP1IE and CCP1IF should cleared in software.

High Level Design

PS/2 Keyboard clock line connected to RC1/CCP1 pin and data line connected to RD0 pin, RD1-RD3 connected to RS, RW, E in LCD module and RB0-RB3 connected to high nibble(D4-D7) of the LCD module.

So the basic idea of the software design is, it should read the fall down edge of the clock line in the same time it should assigned the data value from RD0 pin to a variable. Then variable value must scan with the keyboard hex codes an pass the output to LCD module to display which key is being pressed or held down.

Figure 10 : Block diagram of the device

Hardware construction

Motherboard

This motherboard contains the PIC16f877a microcontroller and all the requirements to get it in to working condition, with all port outs and VDD and VCC outs.

Also this contains serial port which allows programming the PIC using serial communication and ICSP port which allows programming the PIC using USB.

Figure 11: Mother board pcb layout

Figure12 : Mother board pcb layout with components

Figure13 : Complete Mother board

Daughter Card

This is used to connect LCD module and PS/2 Port from the keyboard to motherboard, daughter card getting power from the mother board and then it supplies power to LCD module and to the keyboard.

Figure14 : Daughter card PCB layout

Figure15 : Complete Daughter card with LCD module

power

12 V DC supply provided to Mother board, mother board contains internal 5 V regulation circuit, it will provide the input voltage to the all components. 500 mA current supply provided to mother board to get all components to a perfect working condition.

PIC16F877A microcontroller

Input voltage         = +4.5 V to +5.5 V

Current                  = 300 mA (max)

LCD module

Input voltage         = +2.7 V to +5.5 V

Current                  = 50 mA (with back light on)

PS/2 Keyboard

Input voltage         = +4.5 V to +5.5 V

Current                  = 275 mA (max)

Results

It works fine, the device can display any characters, symbols, and perform well when combined with shift keys, Caps lock and Number lock functions working perfect.

Also Delete,Backspace, Arrow keys, Enter functions working well.

Figure 16 : Complete Full device circuit

Figure 17 : Final Enclosed device with keyboard in working condition

This is a video example of working device.

YouTube Video

Improvements and Suggestions

The importance of using mother board and daughter card separately instead of using full circuit, is that then i can identify the faults and isolate it quickly, its a kind of method we following always because when we developing the system we can not hope to create circuit 100%  accuracy in first time, So following the separation method always useful when developing new systems.
However in this device there’s no such a big and complex circuit but still main advantage is error finding and then we can focus more on developing circuit rather than spend a lot of time to identify the faults.
PIC16F877A microcontroller can used 20 MHz oscillator as its maximum. So we can not use faster oscillators to execute this is the major limitation of this project.
  • 20 MHz oscillator is not good enough to get keyboard scan code quickly.
  • If statement is powerful but it contributes poorly when compiler optimization starts.
  • Switch statement not powerful as if statement but it contributes to get the fastest compiler optimizing execution.
  • Can develop a host to device communication so then we get the data from LCD module.
  • Save it in a external memory card module (MMC, Micro SD) or external EEPROM IC.
  • Then it can be use as a real time data logger.
  • Recreate the same project using PIC 18F series microcontroller (ex: PIC18F452 or PIC18F458) instead of PIC 16F877A then using higher oscillator (40 MHz) will give the perfect results with very less delays.
  • Also in this project scan codes are not saved in the EEPROM because there’s not enough memory to save all those scan code corresponding to all 104 keys, so as a suggestion if we interface external EEPROM and save the scan codes over their may be we can hope little bit of fast execution on the code, so we can type key after key faster than in this project with less delay, then it can use as a real time faster data logger.

Conclusion

High speed scan code decoding is not impossible, its just we need to know the protocol exactly because all these devices are logical, when we identified the protocol exactly and when we have a good understanding of logical states in the device, it is the key for catching and decoding any high speed code and it can interface easily.

Appendix

Appendix 1: Mother Board components list

Component Quantity
PIC16F877A 1
40-pin IC base 1
Max232a 1
16-pin IC base 1
20 MHz crystal oscillator 1
22 pF capacitor 2
LM7805 regulator 1
0.1 µF capacitor 4
o.1 µF electrolyte 4
220 µF electrolyte 2
10 k Ω resistor 1
330 Ω resistor 2
LED – RED 2
LED – GREEN 2
On-Off switch 1
2-way dip switch 1
Push button 1
8-way wire connector 3
6-way wire connector 2
3-way wire connector 1
2-way wire connector 1
DB9 –female connector 1

Appendix 2 : Daughter Card component list


Component Quantity
50 Ω resistor 1
5 kΩ variable resistor 1
16 – way connector 1
10 – way wire connector 1
4 – way wire connector 2

Appendix 3 : PS/2 keyboard Scan code list

Scan code table can be downloaded from attachments.

Code listing

C code and hex file can be downloaded from attachments.

Schematic diagrams

Shematic diagrams of mother board, Daughter card and Full schematic can be downloaded from attachments.

References

PS/2 Keyboard Protocol

CHAPWESKE, Adam.

CHAPWESKE, Adam.

PS/2 Keyboard.

PS/2 Keyboard.

PS/2 keyboard interface.

PS/2 keyboard interface to LCD.

PS/2 Keyboard protocol.

16×2 LCD interfacing

Interface 16×2 LCD.

16×2 LCD module interface to pic16f877a.

Capture module

Capture Module.

Interrupts

About interrupt.

How to use interrupt.

Hardware interrupts.

Multi task.

Interrupt_Service_Routines.

Coding

how do i use interrupts in hitech c.

 Speed up interrupt.

Hi-Tech C speed optimization.

Speed Test of If,Else,Switch statements.

General references

Pic microcontrollers.

Hi-Tech C Sourcecode.

Data Sheets and Reference manuals

HITACHI. HD44780U (Dot Matrix Liquid Crystal Display Controller/Driver).

MICROCHIP TECHNOLOGY INC. 2003. PIC16F87XA Data Sheet.

MICROCHIP TECHNOLOGY INC. 2003. PICmicro™ Mid-Range MCU Family Reference Manual.

Attachments

by Lahiru Hettiarachchi :dn.lahiru@gmail.com

Lahiru Nirmal Hettiarachchi |