Archive

Electronics

This circuit has not been tested. It might work. It might not. Who knows? All diodes should probably be Schottky.

I contrived a diode-transistor logic (DTL) circuit for a SR latch[1] with a CMOS push-pull output and a break-before-make mechanism to prevent simultaneous push and pull (shoot-through). I have no idea whether it actually works and there are probably easier ways to do it, but it was kinda fun.

The upper schematic lays out the concept in terms of OR gates with different inversions (clockwise from upper left: NOR, OR, AND, NAND). The output is fed back into the inputs through diode-resistor level shifts so that the one being fed into the high side is high when the output is high but low if the output is either low or off (high-impedance), and the one into the low side is low when the output is low, but high if the output is high or off. This feedback should prevent either output FET from turning on until the complement is turned off (or close to off).

The lower schematic is the upper schematic rendered in diode-transistor logic. The lower-left gate, a NAND, is implemented using the textbook DTL NAND with an NPN transistor. The NOR in the upper left is actually the same as the NAND, but with the diodes reversed and a PNP on the output. Note that in both cases the feedback diode-resistor shift is now part of the gate itself. The other two gates are just diode logic since OR (upper right) and AND (lower right) can be implemented without a transistor on the output.

This contrivance is for a power logic buffer; the intended supply voltage would be somewhere from 12 to 48 volts and the current capacity on the output MOSFETs would be at least a couple of amps. I haven’t tested the thing. Please don’t use it, except in test circumstances, unless you know what you’re doing.

  1. [1]Technically, an S AND NOT R latch.

I’ve been spending a lot of time thinking about op amps and comparators lately.

One of the common uses of an op amp is as a “unity gain” buffer, meaning that the output voltage is as close as possible to being the same as the input voltage. At a glance this might seem like a trivial thing, but it’s used to great advantage.

For example, a resistive voltage divider can be used to derive some fraction of a larger voltage by representing the ratio with resistors. Given a 9V supply, a divider over 100K and 47K would yield about 2.9V (9V * 47K/(100K+47K)). Unfortunately, this is of only limited use directly, since driving a load from the output causes that voltage to drop. This happens because the load itself acts as a resistor to ground in parallel with the divider, changing its resistance.

The divider does okay, though, feeding to a high-impedance input (i.e., one that doesn’t pull a lot of current). The inputs on an op amp fit that description, and its output is low-impedance (meaning that it can supply current without trouble). That’s a huge part of why op amps are useful. If you take the resistive divider’s output into the non-inverting (“+”) input of an op amp and the amp’s own output into the inverting (“-“) input, you get a unity gain buffer, an amplifier that outputs the same amplitude as the input (“unity gain” refers to the 1:1 ratio of input to output). The buffer continuously compares its output to the input. If the output is too high, it’s lowered; if too low, it’s raised.

In general, the op amp’s job is to perform an analog computation on its inputs that amounts to subtraction followed by multiplication. It subtracts the voltage of the inverting input from the voltage of the non-inverting input, then multiplies it by a factor called gain. By default, the gain is an arbitrarily high number called the open-loop gain of the amp. However, by feeding the output back into the inputs in different ways, it’s possible to fix the gain to a more useful value, such as 1 in the above scenario.[1]

Anyway, this ability to continuously adjust an output to a certain point reminded me of linear voltage regulators, like the 7805 and the LM317. I hypothesized that these 3-terminal regulators are basically heavy-duty op amps with a fixed voltage reference on one of the inputs. A little research confirmed this.

A linear voltage regulator is a voltage reference stuck to an op amp with high output current capability. Now you know—and knowing is half the battle.

[Note: The above image replaces a previous version that was less accurate.]

The data sheet for the fixed 7805 (and other 78xx) regulators suggested a circuit that allows the use of the regulator in an adjustable mode for voltages higher than the nominal voltage. Under normal circumstances, the regulator simply adjusts the output pin to be exactly 5V above the ground pin. In this mode, the ground pin is not tied to ground. Instead, the output and ground pin are placed across the high side of a resistive divider. The regulator fixes the voltage across the upper resistor to its face value. The lower resistor gets all of the current from the upper resistor, plus the garbage quiescent current from the machinery in the device. Multiplying this sum by the lower resistance yields, via Ohm’s law, the voltage across the lower resistor. The sum of that voltage and the regulated voltage is the output.

The data sheet for the LM317 shows a circuit essentially identical to the one described above with a few name changes. In fact, the ‘317, when used in the 7805’s nominal configuration, is itself a fixed 1.25V[2] regulator.

Knowing that these regulators are so alike, then, why should we ever choose ‘317 over 7805? The answer: ‘317 has a subtle difference that makes it far more suitable in its adjustable mode: The current out of its adjustment terminal is a couple of orders of magnitude smaller[3], enough smaller in fact that it could be disregarded in many cases when calculating Vout. This is especially useful in cases where the application doesn’t adjust the output voltage, but that requires a regulator for an awkward voltage (like the 13V programming voltage of a PIC). Using a 7805 in such an application might require a trimmer to fine-tune the output, while the ‘317 would probably be okay with a fixed-value resistor instead.

Pictured are the much simplified equivalent schematics, plus my derivation of how Vout is calculated. R2Iadj should be dominated by the rest of the expression.

  1. [1]Incidentally, a comparator is basically a specialized op amp that has been optimized to only output high or low (i.e. in the logical sense). It works as specified above, but boils the problem down to “is the non-inverting input greater than the inverting input?” yielding high if so and low if not. An op amp can be used as a comparator, but actual comparators do the job faster and better.
  2. [2]1.25V is less arbitrary than you might think: It is the output of a silicon bandgap reference, which is designed to be steady and reliable in spite of temperature and environmental changes. Cool.
  3. [3]7805 IQ is 5mA typ., LM317 Iadj is 50µA typ.

image

For reasons explained elsewhere in detail[1], iPods (and iPhones, etc.) newer than the oldest generations refuse to charge if all you give them is a USB port with the power lines connected and the data lines disconnected. Presumably, it has partly to do with the fact that some of the newer devices can negotiate for more than the 500mA upper limit of USB proper, and possibly that the additional requirements hinder the proliferation of unlicensed peripherals, if only temporarily, so that Apple has an easier time pushing their own.

Fortunately, there’s no actual (digital) data transfer going on. To make the charger visible to the device, a bit of static information is provided on the data lines. Each data line can be held to a voltage as low as 0V (ground) or as high as about 3.3V (USB nominal logic high). Simply setting the lines to some combination of these high and low values[1] reports to the device that it can provide 100mA or 250mA for charging. Newer devices look for cues between high and low; the device reports that it can supply 500mA by holding both data lines at around 2.0V, or 1000mA (1A) by holding D+ to 2.0V and D- to 2.8V. The latter two configurations are what the schematic describes.

This charger should also generally be okay for other USB-charging devices; nothing going on with the data lines is in principle electrically unsafe for a USB device.

  1. [1]ladyada’s notes on iPod chargers’ finicky requirements

The following draft description runs down the basics for a single port that allows the direct connection of Sega Genesis controllers and connection via a mostly passive adapter for several other systems’ controllers, including NES, SNES, PlayStation and PS2, Nintendo 64, and GameCube. Additional styles could be added later, and a structure is made available for controllers custom-built to this spec. Read More

[Strong Bad’s e-mail has undergone a tremendous Interface Screw, and the screen on his computer has literally drained all of its content onto the floor.]
Homestar: Never fear, Strong Bad! I know how to fix your computer box.
Strong Bad: No, no, don’t touch that!
Homestar: Your super box needs words.
sbemail 118

While cleaning up for an event at my house yesterday, I happened upon the lifeless husk of a DVD player that was ruined by a lightning strike just over a year ago.

This was once a DVD player.

Today being my day off, I took it apart just to see what might be salvageable. Granted, the defibrillation it took probably derated most or all of its electronic components, but perhaps some of the switches and connectors came through intact. While I was taking it apart, it occurred to me that its most attractive feature would have been untouched by the strike: Its case. On the front, it has a panel of six tactile buttons on a breakout board (if they were damaged, they’re a common style that’s easily replaced), three optic pipes on the right for PCB-mounted status LEDs, a transparent area for an IR receiver, and the tray door, which is spring-biased closed by default. On the back, there is a place for a power cord and an array of analog AV connectors (RCA and S-video). And, of course, the form factor is worth noting: It’s the size and shape of a DVD player, and as such would be a good fit for a set-top box.

Now, what could I put in that box to make it useful? One possibility would be the vastly underused Acer Aspire One I bought two and a half years ago.

As the name suggests, this computer aspires to be used on a regular basis.

This computer has served me nicely on a few rare occasions that I’ve needed a computer without expecting to. However, it would have been a better fit for my previous life in DC, and most uses I had for it have fallen onto the Droid I started carrying for work. It’s no powerhouse; you’d be hard-pressed to run any particularly impressive graphics on it, or even complex Flash movies. But it runs Ubuntu, it can do YouTube, it’s adequate with emulators, and it can reportedly even run MythTV (think tinkerer-friendly TiVo). As far as I know, it doesn’t have TV in or out, but it has a DE-15F VGA connector, and my current TV (which replaced a TV that was zapped in the aforementioned incident) happens to have VGA in. Nice. The computer is easy on both power and ventilation requirements, and, well, it’s a netbook—the guts are small enough to be mounted in a wide variety of alternative cases.

This being an interesting place to have a computer, it would have to have a variety of interesting interfaces. The Acer already does wi-fi, and that’s a very good start. There’s a place for a IR receiver, and that would definitely be a must; LIRC would be the jumping-off point there.

Aside from that, you may have noticed that I’ve been writing a lot lately about game controller interfaces. I’ve been turning over in my head the possibility of a DE-9M gameport that is electrically compatible with controllers for Sega (Genesis), Nintendo (NES, SNES, N64, GameCube), and Sony (PS, PS2), with firmware extensible to more designs as necessary. It would be possible to connect a Sega controller directly; all of the others would utilize an almost-passive[1] adapter. DE-9 connectors are compact (most of the aforementioned controller connectors are huge by comparison) and very easy to source and replace.

Using a single connector on the console end instead of a smattering[2] simplifies the wiring and the board design while offloading the hairy details to external adapters. This last point is important because it’s tricky to find many controller connectors in panel-mount forms[3], but extension cables are common as accessories, usually not as tricky to source.

So, folks, interesting possibilities!

  1. [1]Adapters for non-Sega controllers would contain circuitry to identify which alternate configuration it should use, probably as simple as a transistor and two resistors totaling less than a quarter in parts.
  2. [2]The RetroN 3 takes this approach, with two ports and a cartridge slot each for three different systems.
  3. [3]I’ve actually made panel-mount PS controller ports using extension cables, metal L-shapes, and a generous application of epoxy. Didn’t turn out too badly, either.

See also a previous post about game controller signaling.

A Serious Serial Protocol: Sony PlayStation and PlayStation 2

  • Number of pins on connector: 9 (proprietary male)
  • Number of inputs to encode: 14 on the original gamepad (up, down, left, right, select, start, BT/”triangle”, BO/”circle”, BX/”cross”, BS/”square”, L1, L2, R1, R2), but arbitrarily more, including analog inputs
  • Mapping strategy: One pin for all inputs
  • Reading strategy: A fairly comprehensive full-duplex serial protocol, including provisions for acknowledgement and additional instructions sent by the system, similar to and probably compatible with SPI
  • Number of pins used: 8 = 3 power (1 ground, 1 logic supply of nominally 3.3V but possibly more[1], 1 rumble motor supply of nominally 9V but generally closer to 7[1]) + 2 data lines (1 CMD from system, 1 DAT from controller) + 3 signaling lines (1 ATT from system, 1 CLK from system, 1 ACK from controller). (Actual pin names vary.) One pin is left unconnected.
  • Fun fact: The same protocol (with different headers and commands) is also used for memory cards.

Sony’s system uses a serial protocol, as was the case with Nintendo, but the Sony version is somewhat more flexible—and quite a bit heavier. As described elsewhere, while it’s possible to implement this protocol using standard logic chips, it takes six of them just to implement the original controller[2]. This protocol very much assumes the presence of at least a microcontroller (which is okay—they’re pretty cheap, and replacing hardware with code often has some serious advantages).

Electrically, the controller ports form a bus. With the exception of the ATT line, all lines are shared among all nodes. The lines that are input into the system, ACK and DAT, are open-collector inputs (to put it one way, instead of “low” or “high” the line expects “low” or “nothing”), allowing them to be shared across devices. The system addresses one controller at a time by lowering the ATT line for that controller.[3] It would make sense for a controller to tri-state ACK and DAT whenever ATT is not low.

When ATT drops, a new session (or packet[1]) begins, and the system and controller prepare to exchange data 1 byte (where a byte is 8 bits in little-endian order) at a time. The controller outputs data on the DAT line and accepts input from the system on the CMD line. Both lines are clocked from CLK. The data lines are set up on a falling edge and read on a rising edge. After each 8 bits, the controller acknowledges receipt by pulling ACK low for a short time[4] The last byte of a session shouldn’t be ACKed; if the hardware tri-states when ATT is off then the ACK pulse wouldn’t be output anyway.

The number of bytes in the session depends on the content of the session itself. The system starts by clocking three bytes: 0x01 (start session), 0x42 (poll), and an idle byte (0xFF). At the same time, three bytes are received from the controller: an idle byte (0xFF), a 1-byte controller ID (such as 0x41 for the original gamepad), and then 0x5A (“here comes data”). (If the ACK is not received after any byte, the system considers the connection broken and moves on.)

The amount of data that comes afterward depends on the controller ID. A digital gamepad yields 2 data bytes (encoding 14 buttons in 16 bits), the classic analog is 6 bytes (the same as digital, plus L3 and R3 stick triggers and 8 bits each for LX, LY, RX, and RY), and, on the PS2 using DualShock 2, a whopping 18 bytes: 6 bytes as with the original analog, plus an 8-bit pressure level for each of the main 12 action buttons!

It’s also possible to send other commands to the controller. Among them is a sequence of commands that can be used to allow some of the idle bytes in the 0x42 command to be used as levels for the vibration motors.

(Sources: [1],[2])

  1. [1]http://store.curiousinventor.com/guides/ps2/
  2. [2]http://www.gamesx.com/controldata/psxcont/psxcont.htm
  3. [3]ATT roughly corresponds with the NES latch line OUT 0, but is active-low and stays low for the duration of the session.
  4. [4]Sources conflict as to exactly when and for how long, but it’s on the order of one cycle width on CLK. The standard logic version mentioned before uses a missing pulse detector on the CLK line since it goes idle between bytes. It’s probably not too sensitive either way.

A hard truth that I think everyone who makes stuff has to come to terms with is that it’s basically impossible to do everything one wants to do within a lifetime. The proportion of imagination (the plans) to wherewithal (the means) has got to be at least exponential. (Perhaps that’s for the best.)

In other words, my time and resources have never really been able to keep up with my ideas. At any given time, unless I’m worn out or recuperating from something, I can expect to have several new ideas in the hopper before I’ve finished the one I’m already on. This doesn’t end well. Some of the time I can actually slog my way through to the end of what I’m working on, generally at the expense of the exciting new ideas. More frequently, however, I end up with another project at 30 to 50 percent completion. I think I inherited this from my dad. He was a very smart and inventive guy, and my childhood was practically littered with impressive projects started by him and then preempted by life.

Anyway, my angle here is that I have ideas that I like but am not currently working on. I offer a sampling [Edit: See Back Burner for an up-to-date version of this list.]:

  • Arcade-style dance machine. Summarily, a StepMania/DDR cabinet with custom input and output interfaces. This was my timesink around 3 or 4 years ago. I invented a special soft-touch sensor square that offers a bit of shock absorption and has LEDs embedded in the top of the panel. I created an RS-232 interface for animating LEDs. I was even working on convincing an 18F PIC to identify itself to a computer as a USB gamepad so I’d have something better to deliver than a hacked-apart controller. I bought a used TV for the purpose.
  • A solid-state dual AC switch/dimmer with opto-isolated microcontroller input. This would be more or less a twin solid-state relay (SSR) with dimming capabilities. It could be used to make a computer turn on the marquee light of an arcade machine or act as a speed controller for a rotary tool. This circuit could easily be built into an outlet box with a pair of sockets. An application note by Fairchild (AN3003, PDF warning) has as figure 9 a very basic SSR that seems suitable. I’d be making one of these per outlet.
  • A knockoff of a Griffin PowerMate that isn’t made out of a scroll wheel. Basically, a rotary encoder with a hand-sized cap attached.
  • A really big rotary encoder wheel. Actually, that was part of the DDR machine concept. I bought and dismantled a pair of rollerblades from Goodwill to harvest their bearings. Those are also still waiting for my attention.
  • A RepRap. But who doesn’t want one?
    • Not to mention all sorts of creative apparatus, including a 3D plotter, CNC mill, CNC vinyl cutter, on down to a UV station for photoresist boards, but these are far more likely to be bought than made.

And I’m confident that I’m capable of making all of these things and many, many more, but not necessarily in the same lifetime.

Fortunately, I’m not presently in despair about this; it’s merely an irritation. It’s frustrating to know from the get-go that whatever I’m starting isn’t likely to come to a meaningful conclusion.

On the other hand, someone of the mindset that it’s not the destination but the journey would call this a victory—a life that’s all journey and no destination. Sometimes I can see it this way, also. :-)

♫ SEEEEEEEEEGAAAAAAAAAAAAAAA!!! ♫

Ahem.

To complement my previous post, here is a schematic of the regular Sega Genesis controller. You could actually make one of these from scratch from non-specialty items; unlike the NES controller, which uses a proprietary 7-pin connector, Sega used the common-as-dirt DE-9 female D-sub connector, following in the footsteps of Atari both physically and electrically.

The circuit above could be built in fairly little time using almost exclusively items from RadioShack, if it’s well stocked. There’s probably still a DE-9 connector kicking around there. You’re not likely to find a 74HC157 in a local store, but it’s easy enough to make a 2-1 mux using 74HC00 quad NAND ICs. If you can’t track those down, it appears to be possible to make a non-inverting 2-1 mux in as few as 9 or so transistors (probably MOSFETs), but my personal recommendation, which is more time-intensive but overall less masochistic, is to have a working stock of a key few 74HC-series ICs available for when you get curious.

If I were to do it this way, I’d pull a couple of 74HC00 from my stash. A 2-1 mux—let’s call it MUX(M,N,S)—implements the expression MS OR N(NOT S); that is, “reflect M if S is high; reflect N if S is not high”. That’s equivalent to the expression (M NAND S) NAND (N NAND (S NAND 1)), which is four NAND gates. One 74HC00 = 4 NAND gates = 1 mux.

A 74HC157 packs four of these, but only two are genuinely in use. Most game software probably ignores the left and right signals while the select line is low, so it’s most likely okay to pass the left and right buttons directly out pins 3 and 4 of the port (as is already the case with up and down). As for the rest, pin 6 would be MUX(A button, B button, select) and pin 9 would be MUX(start button, C button, select).

Of course, as if it even bore mentioning, this controller would be a cinch to implement on an Arduino-like platform using just a DE-9 breakout cable. (Hint: Vcc on pin 5, ground on 8, an input on 6, and the rest are outputs.) Do this only on a temporary basis, though—you have better things to do with an Arduino. :-)

A couple of weekends ago I found myself in conversation with Jon, my brother-in-law, a vintage game systems collector and proud owner of an Action 52 cartridge, about NES controllers—specifically, how all eight buttons can be crammed down only seven controller pins (a trivial setup would require nine). I just happened to know most of the mechanism already and gave him the surprisingly simple rundown. I got curious about the parts I didn’t know, so here’s a digest of my research.

Read More