Converting a Commodore C16 Keyboard to USB

Dave "The 8-Bit Guy" Murray recently bought a whole batch of Commodore Vic 20 motherboards and C16 keyboards cheap. Don't ask why. However, he's selling them off cheap, so I decided to snap up one of the keyboards.

Now, I was always a Sinclair Spectrum child, but a friend of mine had a Commodore C64 and I was always jealous of his posh keyboard with real keys, and fancy sound and graphics. So I have good memories of the early Commodore computers being nice to type on. Certainly the Amiga 500 that I progressed to in later life was a big step up from the Spectrum +2 I had before.

And I also think those old keyboards are even nicer to type on than the majority of modern keyboards, where the keys are often flat and square, with almost no travel to them at all. Very much like going backwards to the days of the Spectrum...

So why not take one of the old style keyboards and convert it to USB and use it on your computer as your main keyboard? I did it once with my old Amiga 500 keyboard, which worked well (until I dropped it and it broke). The C16 keyboard, though, is a prime candidate for conversion. With a simple matrix interface instead of the custom serial interface that the Amiga keyboard has, and a better selection of keys than the C64 had )(such as four separate cursor keys), it should be a doddle to get going.

So one was ordered, and in short order it arrived. You can see me unboxing it here.

The interface to it is a simple 20 pin 0.1" pitch socket. Two of the pins are not there (one is blanked off as a key), so there's only 18 actual wires. As it turns out two of those wires don't appear to actually do anything (red/white and brown/white), so that leaves 16 pins to deal with, which splits nicely into a matrix of 8x8 giving 64 buttons, which is exactly the number the C16 has (that is, when you realise that both shift keys and the shift lock key, which is a proper toggle switch, are all the same button).

I found all that by plugging the header directly into pins 0-19 of a prototype chipKIT Pro MZ board (the best board I had that had enough pins in a straight line like that) and wrote a small sketch to scan through all the pins looking for pins that get connected when a key is pressed and displaying them.

Here's the program in case you want to do the same thing yourself:

void setup() {
    Serial.begin(115200);
    for (int i = 0; i < 20; i++) {
        pinMode(i, INPUT_PULLUP);
    }
    while (!Serial);
    delay(1000);
    Serial.print("\e[2J");
}

void loop() {
    Serial.print("\e[1;1H");
    for (int i = 0; i < 20; i++) {
        Serial.printf("%2d: %c\r\n",
            i, scanPin(i) ? '*' : '-'
        );
    }
    delay(5);
}

bool scanPin(int p) {
    pinMode(p, INPUT_PULLUP);

    for (int i = 0; i < 20; i++) {
        if (i == p) continue;
        for (int j = 0; j < 20; j++) {
            pinMode(j, INPUT_PULLUP);            
        }
        pinMode(i, OUTPUT);
        digitalWrite(i, LOW);
        bool r = digitalRead(p);
        pinMode(i, INPUT_PULLUP);
        if (!r) return true;
    }
    return false;
}