Use of PS2 Keyboard

hbl2013

Senior Member
I have been trying to use a PS2 type Keyboard with a 40X2 and had not much succes using the instructions on page 129 using D.7 for data and D.6 for the clk. A quick Forum search on this subject revealed that this combination is not as straight foreward as I thought. It seems that this type of keyboard uses a serial data stream that is way too fast for the PICAXE chips' BASIC, and the KB instructions listed can not be used as shown in the manual. (KBIN, KBLED)
(BTW the keyboard PS2 serial DATA stream is published as being 10-16kHz, 1STP-1STRT-Odd/PAR)
The question is now, what are the instructions actually valid for? What kind of keyboard can be used using the information on page 29 of the manual? Anyone has any suggestions or comments using this keyboard with PICAXE chips?
 

nick12ab

Senior Member
A standard PS/2 keyboard (I presume you don't mean a PlayStation 2 keyboard if such a thing exists) should work with the kbin command which reads what key is pressed.

The data stream may be too fast for bit banged interfacing in BASIC but the built-in command can do it since it isn't limited by the slow interpreter. Comments regarding that may refer to using a PS/2 keyboard with an older PICAXE without the appropriate commands or using a PICAXE to simulate a PS/2 keyboard.
 

hbl2013

Senior Member
nick12ab - I am refering indeed to the "standard" PS2 computer keyboard. I am rather surprised that nobody has any expierence using this keyboard since it is a rather inexpensive and readily available item for a character input.
I intended to use it to check a serial input LCD setup, where I would send hex ascii characters generated by the keyboard, to the LCD readout for display.
I found out that KBIN does not work with the 40X2 - (Connections: kbdata = D.7, kbclk = D.6.)
I used the sertxd command to display anything generated by the keyboard and it did either not respond to any keypresses, or produced garbage.

code used

keyboard:
kbin, b0
sertxd ("b0= ",b0,CR,LF)
wait 100
goto keyboard

erco - I will try it with a 20M2, I think I have one laying around somewhere.
 

nick12ab

Senior Member
I found out that KBIN does not work with the 40X2 - (Connections: kbdata = D.7, kbclk = D.6.)
I used the sertxd command to display anything generated by the keyboard and it did either not respond to any keypresses, or produced garbage.
Are you sure you've got clock and data the right way round on the PS/2 connector?

Are you aware that keystroke values and ASCII codes are not the same?
 

Technical

Technical Support
Staff member
As nick suggests your test program is at fault, you either have to use #b0 (to see the ascii formatted value received) or convert the raw data in b0 to the corresponding ascii code from the lookup table.
 

hbl2013

Senior Member
Thanks fellows. I am a little confused with this command and some display conventions. If I am using the command kbin b0, what is the result in reg. b0? Is it binary? If so, can I use the bintoascii command?What does the prefix "#" (#b0) do? (See below)
In the meantime I hooked up a keyboard to a 20M2 and it works. By using the sertxd command and specifying the #b0 to display, I get hex values of the keys displayed. So far so good.
Why should I have to go to a table now as technical suggests? Can't I use the "#b0" value and send it to my LCD display as a hex character? Or is it possible to define an other register to hold the hex value if I want to manipulate the hex value later on? (b1 = #b0 for example)
 

srnet

Senior Member
If I am using the command kbin b0, what is the result in reg. b0? Is it binary?
Yes, it is binary, variables or memory locations in microcontrollers can only store binary.

However that variable or memory location can be treated as or displayed as a binary, decimal, hexadecimal or ASCII, but the contents of the location or variable do not change.

If a memory location or variable contains;

01000001

That is;

01000001 as a binary number
66 as a decimal number
$41 as Hexadecinal
A in ASCII.
 

hippy

Technical Support
Staff member
KBIN b0

This will read a key-scan code into the variable 'b0'. That key-scan code is a binary value which then needs to be looked-up in a table to convert it to the ASCII character the key pressed represents.

KBIN #b0

This will read a key-scan code and convert to the ASCII value of the key pressed which is placed in the variable 'b0'. It basically does what has to be programmatically be done if using 'KBIN b0' to achieve the same result.

What Technical is saying is that you can use one or the other; 'KBIN b0' and a lookup table, or 'KBIN #b0'. Using 'KBIN b0' on its own won't produce meaningful or useful ASCII values.
 

Pongo

Senior Member
Am I correct in thinking that you would have to use KBIN b0 and the table if you want to be able to recognize keys such as the arrows, page up/down, etc. ?
 

Technical

Technical Support
Staff member
When a key is pressed, a keyboard 'keycode' number is sent to the PICAXE. This keycode is not the same as the ASCII number of the letter actually printed on the key.

So 'kbin b1' will end up with a keycode in b1. If you already know the keycode for the up arrow (easy to find by experimentation) you can just test b1 for that keycode directly.

If however you want the ASCII code e.g. "A" for the A key, you need to use kbin #b1
The extra # tells the PICAXE to convert the keycode into ASCII via an internal lookup table. If the key is actually 'up arrow', which is not ASCII, you will probably just see "?" in b1, this means the PICAXE couldn't do the the keycode to ASCII conversion.
 

hbl2013

Senior Member
I did some experimenting and wrote the following code to use a keyboard and check what kind of output you can expect.

Code:
start:
kbin [20,start],#b0 'read keyboard
sertxd("Key Press (#b0)= ",#b0,13,10)
sertxd("Key Press (b0)= ",b0,13,10)  
pause 1000
goto start

#rem
 When the A key is pressed:
	#b0 has the DECIMAL value 65
	b0 has the actual ASCII value of "A" or HEX 41
(Don't forget that the [U]sertxd [/U]routine will convert the value
(of b0 into a HEX value that will be displayed as "A" )
#endrem
 

nick12ab

Senior Member
I did some experimenting and wrote the following code to use a keyboard and check what kind of output you can expect.

Code:
start:
kbin [20,start],#b0 'read keyboard
sertxd("Key Press (#b0)= ",#b0,13,10)
sertxd("Key Press (b0)= ",b0,13,10)  
pause 1000
goto start

#rem
 When the A key is pressed:
	#b0 has the DECIMAL value 65
	b0 has the actual ASCII value of "A" or HEX 41
(Don't forget that the [U]sertxd [/U]routine will convert the value
(of b0 into a HEX value that will be displayed as "A" )
#endrem
Be careful there, # in sertxd is not the same as # in kbin. The former converts a variable into up to three ASCII characters representing the value in it and the latter uses a lookup table to convert the completely unrelated keyscan character codes into corresponding ASCII codes. Therefore both sertxd commands in your program use the keyscan code after it has been converted via the lookup table.

The first sertxd command converts this into up to three ASCII characters (e.g. "6" and "5") and the second one does nothing at all to b0, no hex conversion, nothing. The value in b0 is sent as a single byte which the terminal displays as the corresponding ASCII character.
 
Top