Interfacing AVR to MOS logic

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm doing something atmega8-based that plugs to a Commodore 64 (heh) joystick port. This something causes me some unexpected trouble. C64 joystick port lines are shared with keyboard matrix lines, so screwing with them makes the keyboard malfunction. Normally keyboard is being scanned with a running 0 and joystick is being checked when keyboard is not active. Joystick is typical Atari, normally open, switches close to the ground. Joystick port 1 is connected to keyboard row lines, joystick port 2 is connected to keyboard column lines.

(for the really curious, the schematic part that shows control ports and keyboard connections is here: http://zimmers.net/anonftp/pub/c... )

My atmega8 has joystick lines connected directly to PORTC pins. The plan was to keep pullups off and lines tristated. When I'd need to imitate a switch closing, I'd write "1" to corresponding DDR bit and that will be it. Hard to imagine anything more simple than this.

But not all is well. When I plug my magic device in, C64 basically goes nuts. Namely, certain keyboard keys, probably related to one of the pins, get broken readings. This is possible, because some pullup or output could interfere. But this happens even when the entire code consists of, just to be really sure:

cli(); SFIOR |= PUD; while(1);

It keeps going on even when the device is in reset state. I checked all lines, I get at least 30MOhm to the ground on each pin.

Now I never really dealt with MOS logic before. Is there something very special about it that has to be taken care of? I don't even know where to start because I seem to have no control of the situation (except for plugged/unplugged ;) To add to curiosity, stick emulation works on some lines and on some it doesn't. Probably those that don't work are the same that are shared with keyboard lines that get affected. I'm perplexed. Any random ideas from your trick bags?

I realize this question is bordering with GE forum, but it's also very AVR-specific so I posted it here.

[update]
6526 CIA datasheet:
http://archive.6502.org/datashee...

Says that PA/PB are TTL/CMOS compatible... Even weirder now.

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you observed the lines with an oscilloscope ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeps, they look pretty normal to me, not particularly flat or anything. But I probably should collect a profile on each line, how it behaves with and without the device and with other joysticks too. It may be a little hard to do because they're being affected by the keyboard scanning too so the patterns I see are somewhat irregular, plugged or not.

[edit] after poking into some old hardware I can understand your avatar much better ;D

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try isolating the AVR outputs from the C64 with an series of opto isolators.

Also are you powering off the joystick port?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I sometimes connect a line under observation
to 2.5 Volts via a 10k resistor, so that
I can distinguish high/low from high-Z.
It sometimes helps.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I'm powering off the joystick port. Docs say 50mA tops, but it seems to be directly connected to the main +5V bus inside of the C64 without any tricks. I wouldn't exceed 50mA anyway. The power near atmega8 is filtered with a few tankish and a couple of smallish capacitors, as usual. You think I could filter it better? Add some L-C-L stuff? The AVR itself doesn't seem to object to power quality: it also works with the analog inputs and it handles them perfectly well at the same time.

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think i would be inclined to directly emulate the joystick hardware either with an opto or relay and then work back trading off parts for compatability. Although the parts cost for a couple of mocd217's and a few 1k resistors is less than a quid. There shouldn't be any issue with tri stating the outpus without pullups etc but clearly something is going on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the joystick is connected to the matrix, but is not scanned as matrixed, since joystick can just pull line to GND or not.

Your thinking is correct how to drive the lines. I guess more oscilloscoping is needed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahh, those haunting '4066s (U18, U21, etc.)!

That smells like messing with scanning matrices and, in my opinion, it has nothing to do with MOS (Metal-Oxide Semiconductor) logic. Actually, all these years we are mostly dealing with Complementary MOS logic, also known as C-MOS, where NMOS and PMOS devices are used in complementary pairs instead of open-drain NMOS transistors with pullup resistors, resulting in circuits that consume power only when they are switching state.

As I can understand it, when U1 decides to scan the Control Ports, sets the analog switches through PA6 and PA7, which, in turn, select the "POT X" and "POT Y" signals of U9 to be used with the PA0..PA4 and PB0..PB4 keyboard lines. So, when you pull these keyboard lines, even when PA6 and PA7 are inactive, you are actually interfering with the keyboard scanning procedure, resulting in confusing U1. All you will have to do is to wait for the Control Port signals to select the Control Ports and only then to pull the PA0..PA4 and PB0..PB4 keyboard lines to the desired state; when the Control Port signals deselect the Control Ports you should immediately tristate the AVR drive lines that control PA0..PA4 and PB0..PB4. But, since I am not sure what kind of signals the "POT X" and "POT Y" are, because as I can see U9 is the SID chip, I would suggest using an AVR to control another set the '4066 type of analogue switches; unless, of course, you have the time to figure this out and use a bare AVR to do the job.

A very similar discussion about driving scanning matrices has been started here, where most of the members praise the use of the '4066 type of analogue switches. There is, of course, an heretical opinion, coming from a couple of members that suggest skipping the use of any external switches, transistors or relays(!) and manipulating the matrix scanning scheme directly, in the manner I have just described, above.

-George

I hope for nothing; I fear nothing; I am free. (Nikos Kazantzakis)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Giorgos! A joystick is a very dumb and simple device: it just closes switches, it can't monitor when something is being scanned or not. Yes, it interferes with keyboard scanning when it's being used and it's a known feature, but when all switches are open they are open and no interference must happen. So I hoped it will be with my circuit but alas, it's close but no cigar.

POTX/Y are comparator inputs with additional capability to be pulled to ground. They are used to discharge a capacitor to ground and then measure the time it takes to charge it back to about +5V through an analog paddle potentiometer. With those I interface perfectly well BTW, they behave.

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The keyboard *could* be a part of matrix if the buttons would be rows and the common would be some column for example, but since the buttons are returned to GND instead of the matrix, so it is not a matrix.

However I can't stop thinking about the movie "Matrix" now.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I solved the problem with optics. It was a tiny bridge between two PCB traces. I checked each pin to ground individually, but never checked resistance between the pins. Heh heh. I talked to a friend about my woes and he told a story of a bridge between pcb traces he had in a board once that he could only find with a microscope. I came home and — d'oh, what's that glitter!?

So thank you everyone, it's solved now! It's a PS2 -> 1351 mouse adapter just in case anyone's interested :)

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, Svo! I am glad you solved it!

By the way, thanks for sending me out there for research, since it seems I had to do some homework before answering your message in the first place; you see, I think I have not even seen a C64 joystick, ever... :oops:

Anyway, after a brief search I could see what is going on at the C64 Control Ports: The potentiometer inputs have nothing in common with the keyboard connections so they can work independently; but the digital inputs share all their signal lines with the keyboard. This means that the keyboard loses 1..5 of the 8 scan-lines it has in total while any of the two joysticks are in use; but it resumes its full functionality when the joysticks are inactive or disconnected. "This causes the machine to 'type' characters when the joystick is operated," as it has been exactly reported, definitively confirming the "known feature" you were talking about! :-)

-George

I hope for nothing; I fear nothing; I am free. (Nikos Kazantzakis)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

svofski wrote:
I solved the problem with optics. It was a tiny bridge between two PCB traces. I checked each pin to ground individually, but never checked resistance between the pins.

It happens, and usually last thing to measure :)

Sometimes you just can't see or fix where the problem is, like if you have more than two PCB layers, and the short is in the middle layers or vias.

In those cases, you can try to blow it like a fuse, with very low voltage like 1V or less if components are mounted, and then increase the lab power current limit until current flows no more. If the PCB was already broken, it can either fix the problem, not fix the problem, or make things worse like blow a trace from a wrong place, so usually you have nothing to lose.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Sometimes you just can't see or fix where the problem is, like if you have more than two PCB layers, and the short is in the middle layers or vias.

In those cases, you can try to blow it like a fuse, with very low voltage like 1V or less if components are mounted, and then increase the lab power current limit until current flows no more. If the PCB was already broken, it can either fix the problem, not fix the problem, or make things worse like blow a trace from a wrong place, so usually you have nothing to lose.


Wow, I would never think of that but it's a trick worth remembering. Thanks!

The Dark Boxes are coming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I remember it the C64 can't see the difference between some of the joystick possitions and the keyboard. (The adc's is only used for the pads).

Jens