How does a keyboard work

Modding and Technical Issues

Moderator: Moderators

Post Reply
the dude of dudes
Vic 20 Newbie
Posts: 9
Joined: Wed Feb 22, 2006 11:06 pm

How does a keyboard work

Post by the dude of dudes »

I'm wondering about a C64 keyboard actually, but since the VIC uses the same keyboard I thought I'd ask here. Here is how I understand it, please tell me if I am correct or not. The VIA/CIA drives a pin connected to the keyboard matrix row high, and checks for a high signal on the port connected to the matrix columns. The keypress CLOSES the circuit, correct? And what is the +5v and GND pins used for, I am under the impression the VIA/CIA uses its own power to set the pins high or drive them low. The GND seems to be connected to RESTORE, but I'm not sure about the +5v line. Can anyone clarify all this for me???
User avatar
eslapion
ultimate expander
Posts: 5458
Joined: Fri Jun 23, 2006 7:50 pm
Location: Canada
Occupation: 8bit addict

Re: How does a keyboard work

Post by eslapion »

the dude of dudes wrote:I'm wondering about a C64 keyboard actually, but since the VIC uses the same keyboard I thought I'd ask here. Here is how I understand it, please tell me if I am correct or not. The VIA/CIA drives a pin connected to the keyboard matrix row high, and checks for a high signal on the port connected to the matrix columns. The keypress CLOSES the circuit, correct? And what is the +5v and GND pins used for, I am under the impression the VIA/CIA uses its own power to set the pins high or drive them low. The GND seems to be connected to RESTORE, but I'm not sure about the +5v line. Can anyone clarify all this for me???
Its a grid of 8x8 wires.

The VIA/CIA will let all 8 lines of the Y axis as input and drive all 8 lines of the X axis high one at a time while leaving all 7 other ones low to detect which one of the 64 intersection is active.

That's why, the VIC and 64 keyboards have a maximum of 64 keys. The restore key, I believe isn't among these as this exception key indirectly drives the NMI.
the dude of dudes
Vic 20 Newbie
Posts: 9
Joined: Wed Feb 22, 2006 11:06 pm

Post by the dude of dudes »

The RESTORE key is connected to GND when pressed. I'm wondering why the +5v line is on the pinout on the mainboard. The key matrix is directly powered by the CIA/VIA, so the +5v seems unnecessary, right?
the dude of dudes
Vic 20 Newbie
Posts: 9
Joined: Wed Feb 22, 2006 11:06 pm

Post by the dude of dudes »

The KeyScan routine actually sets ALL pins high except for the row/column (whatever you wish to call it) it wants to read, this pin stays low. Then it reads PORTB to read that row. All pins high on that port means no keys are pressed, a zero on any pin means that corresponding key is depressed. This is all fine and dandy, but doesnt make sense. Power should be applied to a single pin on PORTA, one by one, reading PORTB each time to see if there's a complete circuit, meaning the key is depressed. Testing an old keyboard shows continuity between a row and column when a key is pressed. Also, using a circuit to count how long PORTA pin 0 is high and low shows that its high for 20-30 microseconds, and low for 16640 microseconds (roughly 60hz, which is correct for keyboard scanning). Can anyone shed some light on this for me??
User avatar
Mike
Herr VC
Posts: 4832
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: How does a keyboard work

Post by Mike »

eslapion wrote:The restore key, I believe isn't among these as this exception key indirectly drives the NMI.
Yep. There is a low-pass filter between the RESTORE key and NMI input - which incidently was dimensioned wrong on the C=64, so you needed to thrash the RESTORE key rather than press it. :lol:

@the dude of dudes: The keyboard scan routine is located at $eb1e in the VIC kernal. Maybe you should simply take a look there.

Michael
davidv_
Vic 20 Hobbyist
Posts: 136
Joined: Wed Apr 26, 2006 4:08 pm

Re: How does a keyboard work

Post by davidv_ »

Mike wrote:which incidently was dimensioned wrong on the C=64, so you needed to thrash the RESTORE key rather than press it. :lol:
LOL after all those years. THATS why it gave me soo much pleasure
the dude of dudes
Vic 20 Newbie
Posts: 9
Joined: Wed Feb 22, 2006 11:06 pm

Post by the dude of dudes »

Okay, after some further investigation here's the low down on how a keyboard is read:

-Between scans PORTA is set to $7F
-At the start of a scan (scans occur at 60hz, meaning 60 times per second) PORTA is set to $00. If no key is down, PORTB will read $FF and the entire Scan routine is skipped.
-Each row is scanned by setting it's corresponding pin LOW, while leaving the rest HIGH on PORTA. Scanning row 0 means PORTA has a value of $FE, row 1 is $FD, row 2 is $FB etc.
-if a key is down on that row, PORTB will show a low in a bit position, the key completes a circuit and pulls the pin low. The ScanKey routine increments a counter each time a bit is read. When it finds a low it uses that counter as an index into a keyboard matrix table of 64 characters to find the ASCII value of that key. Which matrix table it uses depends on the status of the SHIFT/CTRL/C= keys.
-regardless if a keypress is found, the code scans all 64 keys. After the last key is scanned, PORTA goes from $7F (value for scanning row 7) to $FF.
-the scankey code does some checking if the key was down last scan, if so should it be repeated, then finally places it in the keyboard buffer.
-at the end of the code, PORTA is set back to $7F and the code exits

I'm posting this in case someone else in the future is looking for this info, hope it helps.
WillKemp
Vic 20 Drifter
Posts: 25
Joined: Thu Dec 27, 2007 4:19 pm

Post by WillKemp »

Can someone point me at documentation of which keys are on which columns and rows in the keyboard matrix?

Thanks.
brain
Vic 20 Nerd
Posts: 534
Joined: Sun Jul 04, 2004 10:12 pm

Post by brain »

WillKemp
Vic 20 Drifter
Posts: 25
Joined: Thu Dec 27, 2007 4:19 pm

Post by WillKemp »

Thanks!
Post Reply