Shop OBEX P1 Docs P2 Docs Learn Events
Simple PC Keyboard to BS2 interface — Parallax Forums

Simple PC Keyboard to BS2 interface

TechnoRobboTechnoRobbo Posts: 323
edited 2007-03-11 17:34 in Robotics
Simple and cheap.· Based on 4 inexpensive readily available chips LM339 (Quad Comporator),two·74HC595 Shift Register and the 74HC165 Shift Register.

The principle is simple clear the buffer (74HC595) release the clock for the keyboard so it starts sending data. Invert the incoming bits so the start bit can be used via the comparator to pull the clock low and force the keyboard to stop and store keystroke data. Then read the data (inverted) via the 74HC165.

Ok it "sounds" complex but it's not. heres the snippet of code that reads the keyboard.

getkey:
··· PULSOUT clear,20· 'clear buffer
··· PULSOUT start,250 'start cycle
··· DO
··· LOOP UNTIL stopbit=1 'Wait for cycle TO finish
··· PULSOUT load, 5 ' grab the switch inputs
··· SHIFTIN datain, clock, MSBPRE, [noparse][[/noparse]scancode]
· RETURN

Yes, that's all!

The program then converts the scan codes into ascii and outputs them out the serial port. If you choose not to use ascii you can work with the scan codes and turn the keyboard into a 144 button pad.

Here's a link to the document I used as a reference to the scan codes: http://www.beyondlogic.org/keyboard/keybrd.htm

Schematics and code for BS2 are attached. I scavanged the keyboard plug from an old pc motherboard.

·

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun


TR

Post Edited (TechnoRobbo) : 2/4/2007 7:44:32 PM GMT

Comments

  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-02 12:04
    I reposted the schematic - forgot a pullup resistor I'm using on Pin 12 of the LM339. If you have downloaded the schematic as of this posting - Please refresh your browser to see the change.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 2/6/2007 1:33:30 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-02-02 15:50
    TechnoRobbo

    Are you going to make an board for this project

    or are you just bread board it

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Tricky NekroTricky Nekro Posts: 218
    edited 2007-02-02 16:18
    A very very intresting data buffer but should be SXed... and given a module....

    As many need it...



    Well done!!!!smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rule your Destiny-
    --Be Good. Be Bad. Be Provas--

    The hellinic (Greek) robots portal: Greekbotics
    Many Projects and Schematics by the users·and also robotic news
    (Translate using babelfish)
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-03 00:36
    Sam_sam_sam,

    I'm·not quite sure what your question is.· I'm not looking to sell boards. I am making a board for myself.· I'm· releasing the design into the public domain and hoping people expand on it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 2/4/2007 4:09:49 PM GMT
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-03 15:27
    I reposted the schematic - I forgot a V+ power to the LM339 on pin3 and GND was on pin 13 should be on pin 12. If you have downloaded the schematic as of this posting - Please refresh your browser to see the change.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-02-04 22:01
    I built the circuit on my professional development board (PDB)·but got different characters out than I expected. Is it possible that PS2 keyboards are different in some ways? I also never saw the Caps, Num or Scroll Lock LED come on (when used) either (except when I turned power on to the PDB - all LEDs flashed on for a moment which should be normal). Some times no key output was displayed in debug and other times just the wrong key was outputed. But it was somewhat consistant meaning that if I hit the·'a' key I always got a ' key. Here is a chart below that might explain what I get a little better:

    Enter characters:

    a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

    Output characters: (Note: blank space means nothing was displayed)

    `····· d·· f z h i o2j

    Note: a 'g' gave multiple 'z' sometimes followed by a 'g'

    A further test revealed that if you were to keep pressing and releasing a key it would eventually come up. Sometimes nothing would get displayed first and sometimes like the case of the 'g' it would have 'zzzzz' in front of the g.

    Some characters such as f, h, m and others always came up correctly.

    I also tried it on a BS2px and similar results happened. I got different characters - which almost sounds like a timing issue since the BS2px is faster. I don't really know for sure.

    Any ideas how to solve this?




    Post Edited (T&E Engineer) : 2/4/2007 10:54:40 PM GMT
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-02-05 18:31
    I rechecked the wiring and everything looks fine there. I also uncommented the DEBUG statement that prints out the scancodes. The scancodes match up to the ascii code that is being displayed in the DEBUG screen. However, these scancodes and ascii characters are STILL WRONG to what key I am pressing down. Is there anything wrong with the DATA statements in the program? I have also read over the link and it looks like the only problem is that the keyboard is giving me incorrect scancodes which don't match up to the DATA statements.

    COMMENTS PLEASE...

    Thanks,
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-06 00:01
    A Few Things to Check

    In my earlier schematic I negelected to put in the power for the LM339.· Make sure you have the current schematic. I also did not include the BS2 ground - taking it for granted. It's now in the schmatic. I also clarified the·PS2·pin numbers since the reference article differs from the standard mindin p6 connector numeration.

    The LED's
    All· PS2 keyboards default to Scan Set2. The LEDs are not a keyboard function. They are turned on and off by the host. The keyboard only transmits scan codes (press and release).

    The buffer is one directional (input) requiring twice as many chips to merely·turn on LED's.


    The Problem & The BS2px

    It's more than likely a timing issue since they are critical to the buffer operation.



    The following routine is·based on the BS2 2 µs timing. The Bs2px is based on .8µs
    getkey:
    ··· PULSOUT clear,20· 'clear buffer
    ··· PULSOUT start,250 'start cycle
    ··· DO
    ··· LOOP UNTIL stopbit=1 'Wait for cycle TO finish
    ··· PULSOUT load, 5 ' grab the switch inputs
    ··· SHIFTIN datain, clock, MSBPRE, [noparse][[/noparse]scancode]
    · RETURN

    Try increasiing the timing as follows for the Bs2px:

    getkey:
    ··· PULSOUT clear,50 'clear buffer
    ··· PULSOUT start,625 'start cycle
    ··· DO
    ··· LOOP UNTIL stopbit=1 'Wait for cycle TO finish
    ··· PULSOUT load, 13' grab the switch inputs
    ··· SHIFTIN datain, clock, MSBPRE, [noparse][[/noparse]scancode]
    · RETURN

    To clarify: the pulsout start,625 command has to stay on long enough for the keyboard clock to shift the register 1 time. This forces the register to latch a zero at the stopbit pin from the reset command.· If you send too fast a signal it will retain the "one" bit from the last byte. ·The fact that your getting gibberish is a good sign as long as the gibberish is consistent.

    Once you have the timing adjusted the buffer will work flawlessly. My wife types 80 WPM and she can't get it to give an error.



    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 2/6/2007 12:25:14 AM GMT
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-02-06 00:39
    I have rechecked the old schematic to this new one and don't see any significant differences. BS2 GND and +5v come off of the Professional Development Board. I suppose my next step is to rebuild on another solderless breadboard and see if it makes any differences. I still get the same results if I use a BS2 or BS2px (with or without the timing differences).

    The attached DEBUG screenshot shows what happens when I enter all lowercase alphabet characters and then all uppercase alphabet characters.

    (e.g. abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)

    As you can see many times when a character was entered nothing came out and other times strange characters came out. Since it is fairly reproducible it is not noise or randomness but simply the wrong scancode coming out.

    Any other ideas? I will also attempt to rebuild the circuit and see if this helps on a different breadboard with wires from the BS2 P0-P5 and power (+5v and GND) from the Professional Development Board to the external soderless breadboard.
    1400 x 1050 - 77K
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-02-06 01:38
    I rebuilt the entire NEW circuit on a different solderless breadboard and ran the P0-P5 and Vdd (+5v) and Vss (Gnd) wires to my Professional Development Board and I also tried it on a Board of Education Rev C (BoeBot board) with a BS2 and BS2px and get the SAME results every time. I also tried switching PS/2 keyboards out and get the same results.

    Since I have completely issolated the problem out then it has to be one of two items:

    1. Bad IC chips

    2. Code that is customized for your setup.



    I will try replacing all of the IC chips 1 by 1 and report back.
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-06 01:47
    Please note that the resistor on pin Q1 of the 2nd (in the cascade) shift register is a pulldown resistor. Please also note that all the other 10k·are pullup.
    This line:
    PULSOUT start,625 'start cycle

    ...could be the key try increasing it to 800 - this is the only line that is dependent on the speed of the keyboards clock (hence specific to the keyboard) and requires at least 1 cycle to be transmitted by the clock.

    One method of trouble shooting would be to connect the 8 data lines direct to the stamp pins 8 thru 15. set them as inputs·(·DIRH=0)· and read them as a byte (·scancode=INH ).
    ·and rem out"
    ··· 'PULSOUT load, 5 ' grab the switch inputs
    ··· 'SHIFTIN datain, clock, MSBPRE, [noparse][[/noparse]scancode]
    view the input as HEX scancodes·to make sure your wires are in order.
    If F9 is pressed it should produce the codes :·· 01 F0 01
    If F5 is pressed it should produce the codes :·· 03 F0 03
    and so on.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 2/6/2007 12:07:24 PM GMT
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-02-06 02:16
    SUCCESS !!!!

    I·replaced the first 595 and it fixed itself. I got the·all the right codes:

    abcdefghABCDEFGH

    I did notice a couple of·times I got a0b0c0d0e0.

    However, when I swapped them out and pressed down·to get a·good connection, it is ok now. weird.

    All is working. This is really nice!!

    I would like to interface it to a SX Video module from Bean to make it sort of like the TV typewritter that you see on the Propeller chip (very limited though). However, neat idea all the same. I will also at some point rewrite the code for an SX-28 to bring the cost factor down (as I have many SX-28's but only 1 BS2 and 1 BS2px (more·cost too)).

    Great Job!

    BTW: Your timing specifications were correct for the BS2px too. If you left the original numbers (20, 250, 5) in the subroutine, it would not always send a character to the screen. However, if you used the (50,625 and 13) numbers - it worked correctly.






    Post Edited (T&E Engineer) : 2/6/2007 2:25:57 AM GMT
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-02-06 02:26
    smile.gif·'m really glad you got it working - feel free to post any modifications. I'm at hard working on the PCB layout and will post it later.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-03-10 22:20
    I finally finished the PCB - as you can see I still used the PS2 jack I scavanged from an old motherboard and I did decide on a DB9 for the microcontroller connection.
    Parts:
    B1·········· 9 Volt
    PIC OUT·· DB9 Custom
    R1········· 1Meg
    R2········· 1Meg
    R3··········10K
    R4········· 10K
    R5········· 10K
    R6········· 10K
    u1········· 74HC595
    u2··········74HC595
    u3········· 74HC165
    U4········· LM339
    U5········· PS2 Socket·scavanged
    U6········· LM7805

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 3/12/2007 3:57:38 AM GMT
    2040 x 1540 - 501K
    576 x 432 - 222K
    1152 x 864 - 609K
    1152 x 864 - 703K
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-03-11 00:27
    This looks pretty good. I haven't been working on this since I got my Hydra. However, I have a couple of SX-52 proto boards that I have a couple of projects in mind for - one of which is an SX conversion of your project. I tried for a while and could not get it to work right. I will try again in the near future when I get time.
    Congrats on the PCB!
  • ForrestForrest Posts: 1,341
    edited 2007-03-11 01:03
    Your circuit looks inexpensive to build from a parts cost standpoint. But it doesn't seem very simple when you can buy a prebuilt solution for $19.95 here multilabs.net/ezKEY.html or if you want to build it yourself, a Picaxe 18X plus (2) 4.7K resistors will also work for $8.45 here www.phanderson.com/picaxe/
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-03-11 17:34
    The cost to the circuit is less the $10 USD·and it's intended to be incorporated into other projects for the luxury of having 144 buttons that don't leech processing time from your BS2. This project was developed as an answer to a forum question on how to read the individual keys on a keyboard directly.

    I included the software as an example on how to read it.· And like most projects on a forum, it's intended to educate people on different possibilities, techniques·and present unconventional solutions to those that are learning.

    The circuit is the message - not the board. Recently i·heard that with the lapse of the Apollo program,·NASA had to re-learn how to launch rockets into space. Let's not forget how to invent.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 3/11/2007 5:53:07 PM GMT
Sign In or Register to comment.