Interfacing with a PS/2 Keyboard

Anyone reading this post has undoubtedly used a keyboard. How they work, however, is a bit more complicated than “one button, one input.”  [PyroElectro] has a great tutorial about building a PS/2 keyboard interface with a 7-segment LED display (video after the break). The tutorial also includes quite a bit of theory behind it.

The system displayed below uses a PIC controller to display the letter or number pressed. A schematic of the whole project is given here as well as a detailed bill of materials.

As for how the PS/2 keyboard works, each keystroke is encoded into a binary number or “scan code”. Most of these codes are 8-bit, but some special symbols use a longer code. Although the article doesn’t fully address it, a very similar method can be used to send data back to the keyboard for such purposes as tuning on a “capslock” or “numlock” key. Although turning on a light is fun, we could see this being used as an expedient method to control a relay for automation purposes.


Comments

  1. svofski says:

    Anyone reading this post has undoubtedly used a keyboard.
    Hahahah, good one!

  2. _glytch says:

    This just made me realise how useful it would be to have a keyboard LCD which displays scan codes, HTML entities, etc for key presses while programming!

  3. matt says:

    If you’re writing an interrupt-based PS/2 keyboard routine in C on the AVR, don’t forget the “volatile” keyword!

    You might have something like this:

    static int keyPressed = 0;

    pin_change_interrupt()
    {
    // …
    if (bitsReceived == 11)
    keyPressed = 1;
    }

    int main()
    {
    // …
    while (1)
    {
    if (keyPressed)
    doSomething();
    }
    }

    You’ll run it and see that nothing happens when you press keys! The compiler isn’t smart enough to recognize that keyPressed can be set by the interrupt handler, so it thinks keyPressed will always be zero, and it optimizes out the entire if statement.

    To fix this, just declare keyPressed with the volatile keyword:

    static volatile int keyPressed = 0;

    The meaning of volatile is vague and it differs from platform to platform, but based on the assembler output, it looks like AVR-GCC never keeps a volatile variable in a register: when used, its value is always loaded from memory, and when it’s changed, the new value is always immediately stored back to memory.

    • notmyfault2000 says:

      IIRC, the basic C/C++ Standard meaning of volatile: This variable can change behind the code path’s back, so don’t make any assumptions about it’s value. Basically, the compiler has to check it’s in-memory value every time the variable is used, so no caching in registers and no optimizing away “obvious” unreachable code.

      • M4CGYV3R says:

        Basically, it tells the compiler never to cache the value of the variable, and always read/write instantly so it doesn’t get an old value which may have changed since it was cached.

    • Mike bradley says:

      Interesting, I will keep this in mind. However, my compiler, pic ccs, has not shown this issue. I have several vars that get updated in isr’s and main code that checks them.

  4. Mark A says:

    The letter ‘B’ was the same as the number 8.
    I was all ready to see how the letter ‘K’ would be displayed, but you ended on the letter ‘J’. :o(
    I expect H and K are the same.

  5. spiderwebby says:

    What happened with printscreen and pause/break?
    Did the engineer have a seizure while he was writing the spec?

  6. Agent420 says:

    It’s worth noting that many laptop touchpads also communicate via ps2. They are easily available for cheap and make slick input devices for uc’s.

  7. KillerBug says:

    I think there is an Arduino library for this…the only problem is finding a PS2 keyboard that still works…I only have one of them left…it is white, it is old, and some of the keys stick in spite of being cleaned because the springs are worn out. This would be a lot better with USB as you can get a brand-new keyboard in black from almost any store.

  8. Haku says:

    Several of the chips in the Picaxe range have ps/2 keyboard support built-in, as well as having the ability to turn the Scroll/Num/Caps Lock LEDs on/off.

  9. istiaq says:

    hey, I am converting ps2 protocol to uart protocol. I am using stm32f0. Can you please let me know how ps/2 devices send data? First ps/2 device pulls the clock line down and then it releases the data line. then the host takes the control of clock line. Host toggles the clock line and with each toggle ps/2 device sends data. Is it that? Somebody told me something like this. Please correct me if I am wrong. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 96,386 other followers