OBJECTIVE
- Students understand the PS/2 PC keyboard interface circuit
- Students understand the assembly programming language for PC keyboard data retrieval.
- Students can display PC keyboard data on various displays, including Character LCD and 7 Segment.
Theory
The standard keyboard interface is a serial communication connection. "Serial" indicates that data is sent one bit at a time on a single line. Serial communication was chosen for the keyboard interface because it is simple and easy to implement, and not much data per second needs to be sent over the keyboard interface.
There are four lines connecting the keyboard and the PC. Two of these are the power supply and ground, which are used to provide power to the keyboard circuitry. The other two signals are as follows:
- Keyboard Data: This is the path for data bits and commands that will be sent to the PC system from the keyboard.
- Keyboard Clock: This is a regular clock signal, with a value that oscillates from logic "1" to "0" in a regular pattern. The purpose of this clock signal is to synchronize the keyboard and the system, so that they always work together.
- The connected PC AT keyboard only uses four cables, these cables are shown in the image below, for the 5 DIM and PS/2 pins.
Figure 12.1. Keyboard connector (a) XT (b) PS/2
The diagram below shows the scan codes that mark individual keys. The scan codes are shown on the bottom of the key. An example of a scan code for the ESC key is 76. All scan codes are shown in Hex.
Figure 12.2. PC keyboard data map
As you can see, the scan code is marked with a random code. In some cases, the easiest way to translate the scan code to ASCII is to use the look up table method.
Data transmission, from the keyboard to the system, is done in 11-bit frames. The first bit is the start bit (logic 0), followed by 8 data bits (LSB first), one parity bit (odd parity) and a stop bit (logic 1). Each bit must be read on the falling edge of the clock.
Figure 12.3. Clock and data signals
Figure 12.4 PS/2 PC keyboard circuit interface
Figure 12.5. 2x16 character LCD interface circuit
Experiment 12.1. PC keyboard character data capture and display to LED
In this experiment, PC keyboard data is interfaced to the microcontroller using port P3 bits 2 and 3. The results of the keyboard data retrieval are then displayed on the LED. To perform this experiment, do the following steps:
- Connect the two-wire keyboard jumper to the microcontroller circuit bit P3.2 and bit P3.3.
- Install the LED_EN jumper
- Connect the PS/2 PC keyboard module to the keyboard connector.
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog122a.asm )
dispclr equ 00000001b
funcset equ 00111000b
entrmod equ 00000110b
dispon equ 00001100b
KbdData bit P3.2
KbdClock bit INT1;P3.3
DataKeyboard equ 30h
;
org 0h
Forever:
JB KbdClock,$
acall ambilKeyboard
jnc Forever
Mov P0, Datakeyboard
clr C
sjmp Forever
;
;============================================
;Subrutine AmbilKeyboard
;Subrutine berikut berfungsi untuk mendeteksi apakah keyboard
;sedang ditekan atau dilepas. Bila tombol tidak dilepas maka
;akan menuju pada subrutine ;Kybd_tidakLepas
;dan memberi tanda pada bit C = 0 sehingga bila C = 0
; maka data tidak akan dicetak
;Bila tombol keyboard dilepas maka data scan code diambil
;dan dikonversi ke dengan dengan menggunakan
; look up table serta menandai bit C = 1
;===========================================
AmbilKeyboard:
acall ambil_scancode
cjne a,#0F0h,kybd_tidakLepas
JB KbdClock,$
acall ambil_Scancode
Setb C
Acall Konversi_Scancode
ret
;
Kybd_tidakLepas:
Clr C
ret
;
;======================================================
;Subrutine Konversi_ScanCode
;Pada subrutine ini digunakan untuk menerjemahkan scan code ke bentuk
;karakter
; dengan menggunakan look up table.
;======================================================
Konversi_ScanCode:
Mov DPTR,#TableKeyboard
movc A,@A+DPTR
mov DataKeyboard,A
ret
;
Ambil_scanCode:
clr A
Jnb KbdClock,$
mov R0,#08h
Ambil_ScanCode2:
Jb KbdClock,$
Mov C,KbdData
Rr A
mov acc.7,C
Jnb KbdClock,$
Djnz R0,Ambil_ScanCode2
Jb KbdClock,$
Jnb KbdClock,$
Jb KbdClock,$
Jnb KbdClock,$
ret
;
delay: mov R5,#0
delay1:mov R4,#02fh
djnz R4,$
djnz R5,delay1
ret
;
TableKeyboard:
DB 00
DB 0F9H
DB 00
DB 0F5H,0F3H,0F1H,0F2H,0FCH
DB 00H
DB 0FAH,0F8H,0F6H,0F4H
DB 0C0H
DB '~'
DB 00H
DB 00H
DB 0C1H
DB 0C2H
DB 00H
DB 0C3H
DB 'Q1'
DB 00H,00H,00H
DB 'ZSAW2'
DB 00H,00H
DB 'CXDE43'
DB 00H,00H
DB ' VFTR5'
DB 00H,00H
DB 'NBHGY6'
DB 00H,00H,00H
DB 'MJU78'
DB 00H,00H
DB ',KIO09'
DB 00H,00H
DB './L;P-'
DB 00H,00H,00H
DB 27H
DB 00H
DB '[='
DB 00H,00H
DB 0C5H
DB 0C6H
DB 0AH
DB ']'
DB 00H
DB '\'
DB 00H,00H,00H,00H,00H,00H,00H,00H
DB 08H
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 0DH
DB 00H
DB 0FBH
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 0F7H
;
end
- Save the program you typed and name it: prog122a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Perform microcontroller programming using the ISP Software Program (See Instructions for Use)
- Make observations for the keyboard data displayed.
Experiment 12.2. PC keyboard character data capture and display to Character LCD
In this experiment, PC keyboard data is interfaced to the microcontroller using port P3 bits 2 and 3. The results of keyboard data retrieval are then displayed on the Character LCD screen. To perform this experiment, do the following steps:
- Connect the two-wire keyboard jumper to the microcontroller circuit bit P3.2 and bit P3.3.
- Connect the PS/2 PC keyboard module to the keyboard connector.
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog121a.asm )
dispclr equ 00000001b
funcset equ 00111000b
entrmod equ 00000110b
dispon equ 00001100b
KbdData bit P3.2
KbdClock bit INT1;P3.3
DataKeyboard equ 30h
;
org 0h
nop
call init_LCD
;
Forever:
JB KbdClock,$
acall ambilKeyboard
jnc Forever
acall LCD_Out
clr C
sjmp Forever
;
;============================================
;Subrutine AmbilKeyboard
;Subrutine berikut berfungsi untuk mendeteksi apakah keyboard
;sedang ditekan atau dilepas. Bila tombol tidak dilepas maka
;akan menuju pada subrutine ;Kybd_tidakLepas
;dan memberi tanda pada bit C = 0 sehingga bila C = 0
; maka data tidak akan dicetak
;Bila tombol keyboard dilepas maka data scan code diambil
;dan dikonversi ke dengan dengan menggunakan
; look up table serta menandai bit C = 1
;===========================================
AmbilKeyboard:
acall ambil_scancode
cjne a,#0F0h,kybd_tidakLepas
JB KbdClock,$
acall ambil_Scancode
Setb C
Acall Konversi_Scancode
ret
;
Kybd_tidakLepas:
Clr C
ret
;
write_char:
mov dptr,#word1 ;DPTR = [ address word1 ]
mov r3,#16 ;R3=16,number character to be display
mov r1,#80h ;R1=80h,address DDRAM start position
acall write_inst
;
write1:
clr a ; A = 0
movc a,@a+dptr ; A = [A+ DPTR]
mov r1,A ; R1 = A
inc dptr ; DPTR = DPTR +1
acall write_data;
djnz r3,write1 ; R3 = R3-1,
ret
;
;======================================================
;Subrutine Konversi_ScanCode
;Pada subrutine ini digunakan untuk menerjemahkan scan code ke bentuk
karakter
; dengan menggunakan look up table.
;======================================================
Konversi_ScanCode:
Mov DPTR,#TableKeyboard
movc A,@A+DPTR
mov DataKeyboard,A
ret
;
LCD_Out:
mov R7, #80h
acall write_inst
mov R7,DataKeyboard
acall write_data
ret
;
Ambil_scanCode:
clr A
Jnb KbdClock,$
mov R0,#08h
Ambil_ScanCode2:
Jb KbdClock,$
Mov C,KbdData
Rr A
mov acc.7,C
Jnb KbdClock,$
Djnz R0,Ambil_ScanCode2
Jb KbdClock,$
Jnb KbdClock,$
Jb KbdClock,$
Jnb KbdClock,$
ret
;
Init_lcd:
mov r1,#00000001b ;Display clear
acall write_inst ;
mov r1,#00111000b ;Function set, Data 8 bit,2 line font 5x7
acall write_inst ;
mov r1,#00001100b ;Display on, ;cursor off,cursor blink off
acall write_inst
mov r1,#00000110b ;Entry mode, Set increment
acall write_inst
ret
;
Write_inst:
clr P3.6 ; RS = P2.0 = 0, write mode instruction
mov P0,R1 ; D7 s/d D0 = P0 = R1
setb P3.7 ; EN = 1 = P2.1
call delay; call delay time
clr P3.7 ; EN = 0 = P2.1
ret
;
Write_data:
setb P3.6 ; RS = P2.0 = 1, write mode data
mov P0,R1 ; D7 s/d D0 = P0 = R1
setb P3.7 ; EN = 1 = P2.1
call delay; call delay time
clr p3.7 ; EN = 0 = P2.1
ret
;
delay: mov R5,#0
delay1:mov R4,#02fh
djnz R4,$
djnz R5,delay1
ret
;
TableKeyboard:
DB 00
DB 0F9H
DB 00
DB 0F5H,0F3H,0F1H,0F2H,0FCH
DB 00H
DB 0FAH,0F8H,0F6H,0F4H
DB 0C0H
DB '~'
DB 00H
DB 00H
DB 0C1H
DB 0C2H
DB 00H
DB 0C3H
DB 'Q1'
DB 00H,00H,00H
DB 'ZSAW2'
DB 00H,00H
DB 'CXDE43'
DB 00H,00H
DB ' VFTR5'
DB 00H,00H
DB 'NBHGY6'
DB 00H,00H,00H
DB 'MJU78'
DB 00H,00H
DB ',KIO09'
DB 00H,00H
DB './L;P-'
DB 00H,00H,00H
DB 27H
DB 00H
DB '[='
DB 00H,00H
DB 0C5H
DB 0C6H
DB 0AH
DB ']'
DB 00H
DB '\'
DB 00H,00H,00H,00H,00H,00H,00H,00H
DB 08H
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 0DH
DB 00H
DB 0FBH
DB 00H,00H,00H,00H,00H,00H,00H,00H,00H,00H
DB 0F7H
word1: DB ' Welcome Home '; here is the data to be look up
end
- Save the program you typed and name it: prog121a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations for the keyboard data displayed.
- Modify the program to display PC keyboard data for more than one character on the character LCD.
Experiment 12.3. PC keyboard character data capture and display to 7 Segment
In this experiment, PC keyboard data is interfaced to the microcontroller using port P3 bits 2 and 3. The results of keyboard data retrieval are then displayed on a 7-segment display. To perform this experiment, do the following steps:
- Connect the two-wire keyboard jumper to the microcontroller circuit bit P3.2 and bit P3.3.
- Connect the PS/2 PC keyboard module to the keyboard connector.
- Install the 7 Segment jumperEN
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.