NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Sending data over PS/2 connection

January 15, 2011
by wanderingjew
wanderingjew's Avatar

I have a little project in mind - a diy number pad for a computer. Three columns, four rows of buttons wired in an xy grid.

It's trivial to read which button has been pressed, and that part I have pretty much down. However, I'd like to create a PS/2 device using these buttons and have characters displayed on my screen.

I know there's a tutorial for reading PS/2 data, but for writing to a PS/2 port, google fails me.

The best I can come up with is just sending the scan codes over the data line of the PS/2 interface. That's how that would work, right?

January 15, 2011
by Hexorg
Hexorg's Avatar

Yea, you are right! Here's what that PS/2 keyboard project says:

"As stated in the video, the PS/2 Interface is quite easy to implement. To send a key stroke, the keyboard begins driving the clock line. On the falling edge of the clock line, the data line represents the current bit. Each keystroke is sent as 11 bits: first a start bit of 0, then the 8 bits of the scan code (least significant bit first), then a parity bit (odd parity, which we did not implement for simplicity's sake), and finally a stop bit (always 1)."[tutorial page]

so my suggestion would be to use commands like PORTB |= (1<<PB0) and PORTB &= ~(1<<PB0) to set pins to highlow, because you do need to have 11 bits if information, and SPI supports only 8bit-transfer.

January 15, 2011
by wanderingjew
wanderingjew's Avatar

Since the PS/2 spec says the clock should be between 10kHz and 16.7kHz, here's what I'm thinking for the clock and data lines: Turn the clock and data lines off (or on) every 100-167 us.

With a 14.7465 MHz crystal, all I would need to do is divide that by 1024 and I'd get a 14.401kHz clock.

I'd need to write out a table in binary for each of the scan codes for both the make and break with the appended start/parity/stop bit, but I don't think that's unreasonable.

Am I going down the right path here, or is there a more efficient way to do this

Post a Reply

Please log in to post a reply.

Did you know that signed numbers need to be sign-extended when chaging variable sizes? Learn more...