USB serial to Apple II ASCII keyboard connector

6 posts / 0 new
Last post
grimmware's picture
Offline
Last seen: 5 years 9 months ago
Joined: Jun 3 2018 - 12:52
Posts: 3
USB serial to Apple II ASCII keyboard connector

tl;dr: Skip to the bottom for link to the video.

Hello! I'm new here because my dad found an Apple II Europlus that he has no recollection of ever owning in his attic, and I promptly pinched it saying that I would get it up and running for him.

It has a few problems, for one Applesoft BASIC will dump registers and pop it's clogs upon trying to run anything, but the system monitor is fine. Before I debug this problem, however, there's the fact that there are about 5 Datanetics DC-51 switches onm the board that are either entirely dead or as close as makes no odds. I've got a few potential options for fixing this problem, but all of them are somewhat involved or expensive, so I thought in the meantime I'd have a pop at skipping the keyboard altogether and inputting directly via the keyboard connector:

      +---------+
   +5V|1 o    16|NC
strobe|2      15|-12V
 reset|3      14|NC
    NC|4      13|data 1
data 5|5      12|data 0
data 4|6      11|data 3
data 6|7      10|data 2
   GND|8       9|NC
      +---------+


|-----------+--------+--------------------------------------------------|
|       Pin | Name   | Description                                      |
|-----------+--------+--------------------------------------------------|
|         1 | +5v    | +5 volt power supply. Total current drain on this|
|           |        | pin must be less than 120mA                      |
|         2 | STROBE | Strobe output from keyboard. This line           |
|           |        | should be given a pulse at least 10µs long each  |
|           |        | time a key is pressed on the keyboard. The strobe|
|           |        | can be of either polarity.                       |
|         3 | RESET  | Micorprocessor's RESET line. Normally high, this |
|           |        | line should be pulled low when the RESET         |
|           |        | button is pressed                                |
|    4,9,16 | NC     | No connection                                    |
| 5-7,10-13 | Data   | Seven bit ASCII keyboard data input              |
|         8 | Gnd    | System electrical ground                         |
|        15 | -12V   | -12 volt power supply. Keyboard should draw      |
|           |        | less than 50mA                                   |
|-----------+--------+--------------------------------------------------|

I chose to use a 5V Arduino Pro Micro clone for this, because I've built keyboards with them before - normally I'd use plain AVR C, but I promised myself I'd stop screwing around and actually get something done for once, so I opted to use the Arduino libraries. This actually made my code *really* short:

#include 

int outputMap[7] = {2, 3, 4, 5, 6, 7, 8};
int strobePin = 9;
int strobeDuration = 5;
int delayBetweenKeys = 100;

void setup() {
    Serial.begin(9600);
    for(int i=0; i<7; i++) {
        pinMode(i, OUTPUT);
    }
    pinMode(strobePin, OUTPUT);
    digitalWrite(strobePin, LOW);
}

void loop() {
    int inByte;
    if(Serial.available() > 0) {
        inByte = Serial.read();
        for(int i=0; i<7; i++) {
            int state = LOW;
            int bitmask = 1 << i;
            if(inByte & bitmask) state = HIGH;
            digitalWrite(outputMap[i], state);
        }
        digitalWrite(strobePin, HIGH);
        Serial.write(inByte);
        delay(strobeDuration);
        digitalWrite(strobePin, LOW);
    }
    delay(delayBetweenKeys);
}

I wrote the above whilst in a field at a hacker camp and had it blink out the ASCII codes on LEDs, but I finally got to test it out on the Apple II+ on Saturday (this is using a slower refresh than above):

USB serial to Apple II keyboard input

As you can see, a bunch of character codes are coming out transposed (e.g. "@" instead of " "). I'm pretty certain this is because the data 4 pin is pulling high. Hopefully I'll have a chance to test this over the coming days with a multimeter, and then I'll likely just need to put in a pull-down resistor to make it work again.

Once this is done, I'm going to try adding in a USB keyboard host to serial adapter so I can plug in my USB keyboard directly, and then tie it all together into a neat little PCB :) All sources etc will go on to Github.

Thanks for reading!

Offline
Last seen: 2 days 20 hours ago
Joined: Nov 20 2004 - 00:26
Posts: 53
Possible programming bug ?

Hi grimmware,
Well that's a brilliant idea! I'd thought about that to load code into an A1.
Anyway, I'm not much of a C or Arduino person but I think I spotted a 'bug' in your setup() procedure:.

You are using pins {2, 3, 4, 5, 6, 7, 8} as output, but the code to setup those pins as an OUTPUT pin only inits pins 0 through 6, leaving pins 7 and 8 undefined in the loop:
for(int i=0; i<7; i++) {
pinMode(i, OUTPUT);
}
On an Apple II, the ASCII screen code for "space" is $A0 and for " @" is $C0.

I suspect that as pins 7 and 8 are undefined you get @ for a space. Does that make sense? I'd be keen to know.
Regards, Laurie

grimmware's picture
Offline
Last seen: 5 years 9 months ago
Joined: Jun 3 2018 - 12:52
Posts: 3
Hi Laurie!

Hi Laurie!

Thanks for the reply!

outputMap is an array of the pinouts, and they go from 2 to 8 because of the physical layout of the board. Because I want to map them from the spec, I've just placed them in an array so that if I choose to change board, I can just change the array and continue to reference the pins as outputMap[0] (which is 2) to outputMap[6] (which is 8). This still covers all 7 pins, so I don't think this is the problem.

I've tested the full range of ASCII keycodes, and only $C0 to $DF come out properly - everything else maps up or down. These are the ranges where the fifth bit (data pin 4) is set high, meaning that the internal pull-down resistor in the microcontroller probably isn't pulling the voltage down enough to set to logical LOW. I'm pretty certain that once I add in an external pull-down resistor this will be fixed, as I could get keycodes outside of that range when the original keyboard was plugged in Smile

Offline
Last seen: 1 month 2 days ago
Joined: Jun 5 2008 - 07:26
Posts: 475
you might take a look at my

you might take a look at my version, as it may include some ideas that you haven't thought of.
http://www.willegal.net/appleii/appleii-kb-int.htm

regards,
Mike Willegal

grimmware's picture
Offline
Last seen: 5 years 9 months ago
Joined: Jun 3 2018 - 12:52
Posts: 3
Ooh that's really handy Mike!

Ooh that's really handy Mike! Thank you!

Did you run into any problems with weak pull-down or anything like that?

Offline
Last seen: 1 month 2 days ago
Joined: Jun 5 2008 - 07:26
Posts: 475
It was kind of tricky

It was kind of tricky supplying the power to the PS/2 keyboard from the 16 pin dip socket. The PS/2 spec says keyboards can draw up to 300 ma. I had to play with AVR brown-out settings to avoid problems during power up, but since you are only using serial, you shouldn't experience that.

regards,
Mike Willegal

Log in or register to post comments