PS-2 Keyboard-to-ASCII Decoder for 5LP

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
jotic_1250681
Level 2
Level 2
First like received First like given Code Expert

I couldn't find a useful PS-2 keyboard routine that would produce ASCII codes.  This project does that and it includes use of the Shift keys to get upper-and lower-case characters and symbols.  Decoding works for only the main keyboard area and doesn't decode the Print Screen, Insert, Delete, and other keys on the right side of the main keys.  You could add these as needed.  You can add a value for the Fx keys, also.  The example includes an array for upper-case and an array for lower-case values that have ASCII codes. Spaces are "saved" with 0x0 so you can insert a code for, say, the F4 key that generates the keyboard scan code of 0x0C.  Find that location in the array and substitute the code you want the F4 key to provide.  I have included a table of the keyboard-to-ASCII translations that lists all the bytes for both arrays with an index between 0 and 0x7F.  This information helps you locate a keyboard scan code and insert the ASCII value you want to produce when pressed.  I also include a flow chart that shows how the code operates.

When you press a key, a PS-2 keyboard sends information in an 11-bit serial stream that looks like a UART transmission (LSB first).  Each key has an assigned non-ASCII code.  This code remains the same when shifted or unshifted.  The software looks for the shift key and handles data accordingly.  When you release a key, the keyboard send a "key-release" byte (0xF0) followed by the code for the letter.  Software handles this, too.  A clock signal from the keyboard will strobe the bits into an SPI register.  I used the LSB first and mode 1,1, as included in this project.

In typical use you would use the SPI port to generate an interrupt, but the example code simply runs an infinite "keyboard input" routine and echoes characters to a terminal program.  This setup makes the software easy to test.  I used a CY8CKIT-059 5LP Prototyping Kit.  I monitored keyboard data with an Saleae Logic analyzer and ran the Parallax Serial Terminal on my Win-10 PC to check the ASCII output from the project.  Have fun.  Jon Titus, Herriman, UT

3 Replies
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

Thanks for the code.

You can  use  UART  only  (baudrate = 12000, Odd)

I have a very simple example, but I don't remember the details anymore:
PS2_NumKey40.zip

0 Likes
JeMi_1216496
Level 1
Level 1
First like given

Thanks for the code.

Looking at the PS/2 documentation around (http://retired.beyondlogic.org/keyboard/keybrd.htm ), it looks like the SPI Mode should rather be CPHA=0,CPOL=1 (Clock idle high, data valid on clock falling).

But the code is also working as is. Thanks a lot for sharing.

JJ

0 Likes
cadi_1014291
Level 6
Level 6
25 likes received 10 likes received 10 likes given

Thanks for sharing your project, i was also thinking about a decoder for PS-2 keyboards but i was interested in develop it using the UDB programmable hardware, in your experience, do you think it is possible to do it that way or should i be fine using your approach?

Regards,

Carlos

0 Likes