1 Reply Latest reply on Mar 14, 2018 12:47 PM by user_49271930

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


      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