This code gives the interfacing of AVR microcontroller with PS2 keyboard. The keyboard has 2 wires for data transfer, in which one is data line and the other is clock line. For more details about the PC keyboard please check this link http://www.8051projects.info/exp8.asp

In this experiment the keyboard is interface with the AVR microcontroller and the typed character is displayed over the LCD display.

Circuit Diagram

Bascom Code

$regfile = "m32def.dat"
$crystal = 4000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Porta.2 , Db5 = Porta.3 , Db6 = Porta.4 , Db7 = Porta.5 , Rs = Porta.0 , E = Porta.1
Config Keyboard = Pind.2 , Data = Pind.4 , Keydata = Keydata

Dim Text As String * 20
Dim I As Byte
Dim L As Byte

$serialinput = Kbdinput
$serialinput2lcd

Text
= "This is a Test!"
L
= Len(text)
Cursor Off
Cls
Print Text
Wait 1
For I = 1 To 16
 
Shiftlcd Right
 
Wait 1
Next I

For I = 1 To L
 
Shiftlcd Left
 
Wait 1
Next I

Do

Cls
Cursor Blink
Cursor On
Print "Enter Text :"
Locate 2 , 1
Input Text
L
= Len(text)
Cursor Noblink
Cursor Off
Cls
Print Text
For I = 1 To 16
 
Shiftlcd Right
 
'Wait 1
Next I
Wait 1
L
= L + 16
For I = 1 To L
 
Shiftlcd Left
 
Wait 1
Next I
Loop

End

Kbdinput
:

$asm

push r16           ; save used register

push r25

push r26

push r27



Kbdinput1
:

rCall _getatkbd    ; call the function

tst r24            ; check for zero

breq Kbdinput1     ; yes so try again

pop r27            ; we got a valid key so restore registers

pop r26

pop r25

pop r16

$end Asm

Return

I
= Getatkbd()

Keydata
:
'normal keys lower case
Data 0 , 0 , 0 , 0 , 0 , 200 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , &H5E , 0
Data 0 , 0 , 0 , 0 , 0 , 113 , 49 , 0 , 0 , 0 , 122 , 115 , 97 , 119 , 50 , 0
Data 0 , 99 , 120 , 100 , 101 , 52 , 51 , 0 , 0 , 32 , 118 , 102 , 116 , 114 , 53 , 0
Data 0 , 110 , 98 , 104 , 103 , 121 , 54 , 7 , 8 , 44 , 109 , 106 , 117 , 55 , 56 , 0
Data 0 , 44 , 107 , 105 , 111 , 48 , 57 , 0 , 0 , 46 , 45 , 108 , 48 , 112 , 43 , 0
Data 0 , 0 , 0 , 0 , 0 , 92 , 0 , 0 , 0 , 0 , 13 , 0 , 0 , 92 , 0 , 0
Data 0 , 60 , 0 , 0 , 0 , 0 , 8 , 0 , 0 , 49 , 0 , 52 , 55 , 0 , 0 , 0
Data 48 , 44 , 50 , 53 , 54 , 56 , 0 , 0 , 0 , 43 , 51 , 45 , 42 , 57 , 0 , 0
'shifted keys UPPER case
Data 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
Data 0 , 0 , 0 , 0 , 0 , 81 , 33 , 0 , 0 , 0 , 90 , 83 , 65 , 87 , 34 , 0
Data 0 , 67 , 88 , 68 , 69 , 0 , 35 , 0 , 0 , 32 , 86 , 70 , 84 , 82 , 37 , 0
Data 0 , 78 , 66 , 72 , 71 , 89 , 38 , 0 , 0 , 76 , 77 , 74 , 85 , 47 , 40 , 0
Data 0 , 59 , 75 , 73 , 79 , 61 , 41 , 0 , 0 , 58 , 95 , 76 , 48 , 80 , 63 , 0
Data 0 , 0 , 0 , 0 , 0 , 96 , 0 , 0 , 0 , 0 , 13 , 94 , 0 , 42 , 0 , 0
Data 0 , 62 , 0 , 0 , 0 , 8 , 0 , 0 , 49 , 0 , 52 , 55 , 0 , 0 , 0 , 0
Data 48 , 44 , 50 , 53 , 54 , 56 , 0 , 0 , 0 , 43 , 51 , 45