1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PIC + joystick = keyboard

Discussion in 'Microcontrollers' started by Kingpin094, Jun 25, 2003.

  1. Kingpin094

    Kingpin094 New Member

    Joined:
    Dec 29, 2002
    Messages:
    106
    Likes:
    0
    Location:
    Pensacola, FL USA
    While searching the internet for a way to interface my Nintendo controller to my computer.
    I stumbled on the idea to use a PIC to connect a controller to the PS/2 keyboard port.
    The idea is that since the output of the controller is just a serial bit-stream that could
    be converted to the apropriate scanline so each button on the
    controller has a different letter assciated with it.

    I have done some research on how the controllers inside the keyboard work. My problem
    is that I can't figure out the timing for the circuit. So far all of my attempts
    to get a letter to the PS/2 port have failed. If anyone could help with this I would be very thankful.

    Thanks to all that can shed some light on the subject.
     
  2. TheAnimus

    TheAnimus New Member

    Joined:
    Jun 22, 2003
    Messages:
    116
    Likes:
    1
    Location:
    England
    Hey i made a keylogger a while ago (for educational purposes only of course, it was an educational instatute where i used it ;P) and it took a little while for me to figure somthing rather important out. There is not fixed frequency for the PS2 there are lots of adviced ones but these tend to be ignored by the keyboard manifcaturers, any way, here goes

    there are 4 lines in total;
    Vdd
    Vss
    Clock
    Data
    Data and clock are both "open-state" what this means is there is a transistor that pulls each line low, or has no connection for a high.

    so to interface a keyboard to a PIC you would simply use say two 10k resistors, from the Clock and Data lines to Vdd, that way each one would be at TTL levels, so the pic could read it.

    The idle state for both lines is high, when the keyboard is sending data it generates a clock pulse, the characters can be read on the falling edge.

    Because this is open state, many people will say u will need to use transistors to take a line low. While u probably SHOULD do this, u can normaly hack around it by setting the TRIS for the pin to 0, and send out a logic 0 for a low, and just set the TRIS to 1 (input) to give a high impedance, this will give u open state compatable signals.

    THERE IS ONE MAJOR SAFETY ISSUE
    if the host device decides to pull the open state output upto say 13v, the PIC could get fried, so make sure u know where its going! (i have yet to notice a motherboard on a computer where this is done, but hey **** happens)

    Hope that helps, i might be able to dig out some code for reading charactors on a 12F629 if that would help.
     
  3. Kingpin094

    Kingpin094 New Member

    Joined:
    Dec 29, 2002
    Messages:
    106
    Likes:
    0
    Location:
    Pensacola, FL USA
    Any code would be much appriciated. My assembly skills are less then great and I have not got around to finding another compiler.

    My problem is that I am having trouble understanding exactly how the PS/2 port recieves data. I know that the clock goes low and the data pin goes low and then you send the 8 bit byte scan code. I have not figured out what to do with the parity bit.

    My idea is to map the serial input from an old nintendo controller to specific scan lines on the key board in such a way that the computer thinks that the joystick is just part of the keyboard. This allows me to use the controller inside my NES amulator without having to put any oter software on the computer.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. TheAnimus

    TheAnimus New Member

    Joined:
    Jun 22, 2003
    Messages:
    116
    Likes:
    1
    Location:
    England

    Hey sorry about the delay in my reply!

    Right the parity bit is used in error detection (NOT CORRECTION). The idea is that if in the 8 bits representing the scan code, there was only say 5 logic 1's (high) then the parity bit would be logic 1. this is the type of parity used by the keyboard protocol. The idea is too keep an even number of 1 bits, this is even parity. Be careful, this is only one naming convention, there is another where the opersite name is used, in this convention the last example would be odd parity, u have to love the person that made up these names.!

    A problem u will find with ur project is that the keyboard has to respond to the host a lot see http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html for specifics, now these commands allow for say the status LEDs to be updated. Handeling these is a pain. There are 2 ways i would go about dealing with this.

    A.
    Make a device that goes inbetween, and is rather passive but would only use 3 pins on the PIC. The data line is hooked up to a pin, and there is a clock to the comp, and clock to the keyboard pin.
    There would be 3 "modes" for the PIC, mode one, copy all of the computers clock signals to the keyboard, mode two, the keyboard is sending data to the computer, the pic outputs the keyboard clock to the computer. mode three, intercept mode, the keyboard clock line is driven low, this forces the keyboard to buffer any keypresses, allowing you to send ur JoyPad's data to the computer.

    The problem with this method is it requires a fast PIC to update the opersite clock line in mode one and two. However a 20Mhz clock would be able (remeber that it will only cycle at 5Mhz thou!!)

    a simple block diagram would look like this (my ASCII art is poor btw)

    Code (text):

                                   Gamepad
                                      |
    +------------+              +----------+               +--------------+
    | keyboard!  |              |    PIC   |               |      Comp    |
    |    clk     |--------------|          |---------------|     clk      |
    |    data    |---------+    +----------+     +---------|     data     |
    +------------+         |         |           |         +--------------+
                           +---------+-----------+

     

    B.
    The PIC completely emulates a keyboard, this is harder to program, but all the information you need has been loveingly put together by Adam Chapweske on that link i gave you above. The PIC also emulates the Computer, there is actually little advantage in this method, in my opion any way, but i have seen it done before. This would be particually good if u wanted to say make a keyboard switch/emulator at the same time, to let u share 1 keyboard with 3 machines. The key thing is that the data line is broken into 2. This would probably be safer if the keyboard desided to ignore your request and write to the data, this way no harm would be done.

    I can't think of an advantage for this project, and have forgotten the reason i mentioed it :? !

    anyway a block diagram would like like this (ish)

    Code (text):

    +------------+           +-------------+          +----------+
    |  Keyboard  |           |     PIC     |          |   Comp   |
    |    clk     |-----------|kclk     cclk|----------|   clk    |
    |    data    |-----------|kdata   cdata|----------|   data   |
    +------------+           +-------------+          +----------+
     

    Hope that helps you with ur desing problems, if u send me a mail at alex at -dont-spam-me-darn-spam-harvesters- 0bytes.net and i will dig out some of the keyloger code that might be helpful.

    Also, please let us all know how ur project goes!
     

Share This Page