How does a keyboard work
Moderator: Moderators
-
- Vic 20 Newbie
- Posts: 9
- Joined: Wed Feb 22, 2006 11:06 pm
How does a keyboard work
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???
- eslapion
- ultimate expander
- Posts: 5037
- Joined: Fri Jun 23, 2006 7:50 pm
- Location: Canada
- Occupation: 8bit addict
Re: How does a keyboard work
Its a grid of 8x8 wires.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???
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.
-
- Vic 20 Newbie
- Posts: 9
- Joined: Wed Feb 22, 2006 11:06 pm
-
- Vic 20 Newbie
- Posts: 9
- Joined: Wed Feb 22, 2006 11:06 pm
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??
- Mike
- Herr VC
- Posts: 5134
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Re: How does a keyboard work
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.eslapion wrote:The restore key, I believe isn't among these as this exception key indirectly drives the NMI.

@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
Re: How does a keyboard work
LOL after all those years. THATS why it gave me soo much pleasureMike wrote:which incidently was dimensioned wrong on the C=64, so you needed to thrash the RESTORE key rather than press it.![]()
-
- Vic 20 Newbie
- Posts: 9
- Joined: Wed Feb 22, 2006 11:06 pm
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.
-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.
http://sta.c64.org/cbm64kbdlay.html
Note that C=Key works on VIC-20s as well:
www.jbrain.com/vicug/gallery/c=key
Note that C=Key works on VIC-20s as well:
www.jbrain.com/vicug/gallery/c=key