LogoChip Port A, setbit/clearbit, and analog inputs

February 16th, 2007 by Keith Neufeld

Twice before, I’ve run into problems with the lines on the LogoChip’s port A not working the way I expected–while programming an interface to the A6276 LED driver, and again when trying to interface to the DS1302 real-time clock. The second time I ran into it, I fixed the problem by changing port A out of analog input mode–but didn’t know why that made a difference. Last week, poring over the PIC18F2320 datasheet, I finally figured out what’s going on and why the analog input mode matters.

I was working on output routines for TASD’s Thing 3: Simple Outputs, and we’re using port A because it’s the most conveniently placed on the LogoBoard. Every time I’d do a setbit or clearbit command against a line on port A, all the other outputs would change to 0.

Tom McGuire had run into this problem recently, and worked around it by reading and writing the port A latch, LATA, instead of the port register itself. But that still didn’t explain why it was happening in the first place. So I sat down with the datasheet and looked at the port operation diagrams.

Generic I/O Port

PIC18F2320 datasheet, Figure 10-1

Here’s the diagram for a generic I/O port, from page 103 of the datasheet. Writing to the port or latch register (WR LAT or Port) puts data into the data latch, which is accessible by reading the latch (RD LAT) register. Additionally, when the data direction latch (TRIS latch) is in output mode, the data latch drives the I/O pin for output. Finally, the input buffer continually reads the I/O pin (even when outputting) and makes its current state accessible by reading the port register (RD Port).

So you can always write output data to the port or latch register; and when the port is in output mode, the last data written will be output to the pin. The last data written is always readable from the latch register; and when in output mode, the data is also readable from the port register.

Port A

Now look at port A, from page 103:

PIC18F2320 datasheet, Figure 10-2

The most visible difference is that the simple output buffer from the data latch to the I/O pin has been replaced by an arrangment of an OR gate, an AND gate, and N- and P-channel FETs. As far as I can tell, there’s no operational difference between the two–Figure 10-2 simply shows what’s happening in more detail.

More significant is the addition of the Analog Input Mode line controlling the input buffer–this is the crux of my problem. When port A pins are receiving analog input, it doesn’t make sense to feed that input into a TTL register–in fact, it could cause excessive power draw and overheating. So the input signal is gated by the analog input mode before feeding to the port A read line (RD PORTA).

setbit and clearbit

The reason this matters is that the LogoChip’s setbit and clearbit commands are read-modify-write operators–they read the value of their target, OR or AND that value with a constant, and write it back to the target. When that target is the port A latch register, all works as it should–the latch is read directly back out the RD LATA line.

When the target is the port A data register, however, the current value is read through the input buffer, which is gated by the analog input mode line. When the pin is set in analog input mode, even though the data direction is set to output, the port data cannot be read because the gate is off, and always returns a 0.

So when setbit <bit> porta attempts to change one bit, it tries to read PORTA and returns %00000000 rather than the value of the data latch. It then sets one bit and writes the new (mostly-cleared) byte back out to the port A data latch–clearing every other bit of port A in the process.

Workarounds and Solution

Two workarounds are obvious. Tom’s method was to manipulate the port A latch (LATA) instead of the port A data register (PORTA). The latch can always be read and written directly, so this works flawlessly. My only objection is that porta is already defined as a constant in LogoChip Logo. I’m not wild about having to teach students to define a new constant for lata and then use lata, portb, and portc.

The second workaround is also simple. When using the LogoChip setbit and clearbit commands, clear the analog input mode on any pin you want to use as an output. Because the order of analog input pins is predetermined (A0, A1, A2, A3, A5, . . .), this might mean rearranging the pinouts–you can no longer have A0 and A2 as analog inputs and A1 as an output, for example. But it’s a pretty easy method.

The true fix is equally simple. Where port A’s TTL input buffer is gated by (active-low) analog input mode, it should in fact by gated by (active-low) analog input mode OR (TRIS Latch is set for output). That is, any time the pin is NOT in analog input mode, OR the pin is set for output, pass the pin’s value to the port data read buffer. I don’t see any drawbacks to this solution, other than the cost of one additional OR gate.

Having taken the time to figure that out, I’m rather surprised that Microchip didn’t build the PIC that way.

LogoBoard

January 29th, 2007 by Keith Neufeld

Well, it’s time for another round of John Harrison’s Technology: Art and Sound by Design class–and oh by the way, this time I’m co-teaching. John’s doing the hard parts (course vision, blogs, wikis, etc.), and I’m doing the technology lessons for a while.

We’re calling them “Thing a Week,” sort of like another Thing a Week series you might have heard of, only different. On Monday and/or Wednesday, we introduce a technical topic, Friday we build it in the lab, and then next Monday we show it off.

So here’s the first one–me soldering up a LogoBoard, a carrier board for the LogoChip, designed by Tom McGuire.

Thing 1: LogoBoard Assembly (may move to here)

LogoBoard

Funny how a 15-minute soldering job turns into two and a half hours when you stop after every part to take pictures. :-)

Theremax, Part I: Circuit Board

January 22nd, 2007 by Keith Neufeld

A Theremin is an electronic instrument played by waving your hands in the air near a couple of antennae. It has matched pairs of high-frequency oscillators, and the change in capacitance due to the proximity of your hand changes the frequency one oscillator in the pair, causing “beats” (heterodyning) between the pair that form audible wavelengths. The Theremin was invented early in the 20th century by Leon Theremin, a Russian physicist. It’s the spooky sound you hear in the Beach Boy’s “Good Vibrations”–although that was actually recorded with an electro-theremin, a different instrument designed to mimic the Theremin’s sound.

I don’t know when I first became aware of Theremins, but I know I’ve wanted one at least since February of 1996, when Electronics Now published “Build this Theremin,” by John Simonton of PAiA Corporation. The full PCB layout and parts were published in the magazine, and PAiA has also sold their “Theremax” kit continuously since then.

And I finally ordered one.

Two, actually; my friend Cort wants one as well.

They arrived about a week later.

Board Assembly

Theremax kit parts

I bought the kit with the PCB and all components, and the separate partial case kit with the antennae and a nicely screened front panel. The full case kit was another $60, and I figured I could make a case that I’d be happy with for less than that. I think it says a lot about PAiA and their understanding of the hobbyist marketplace that they offer so many options, instead of forcing everyone into the same cookie cutter package.

The first thing I did was sort out all of the components. There are dozens of resistors and capacitors, a handful of transistors, four tunable inductors for the oscillators, pots and jacks for the panel (included in the parts kit, even if you don’t buy the panel), and miscellaneous bits like LEDs and ICs. They even threw in hookup wire to connect the PCB to the control panel, and a gator jumper for bypassing the volume control during one stage of hookup–literally every part you need to get up and going, except solder.

Theremax PC board, component side

Theremax PC board, solder side

Next, I took a look at the PCB. It’s a nicely-made single-sided board, with silk-screening on the top side for component identification only. I have only two small complaints:

  • The identifying marks for resistors and capacitors are under the component, so they’re no longer visible once the component is installed. I know there’s not a lot of extra room on the board for labels outside of the component footprint, but having to refer to a separate parts placement diagram really makes troubleshooting more difficult.
  • The resistors and capacitors are roughly numbered from upper left to lower right; but some of the numbers stray across the board, making it hard to locate the correct part. Maybe I’m too picky, but I really like an orderly numbering system during the assembly and troubleshooting stages.

Theremax PC board, resistors installed

The assembly instructions give a reasonable order for soldering in the components, which I mostly followed–jumper wires and resistors first,

Theremax PC board, diodes installed

then diodes. Note the blue diodes near the center of the board–germanium, with a lower voltage drop for increased sensitivity. I’ve read a nice description of how that aids the Theremin circuit, although I don’t recall it at the moment.

Theremax PC board, inductors and ICs installed

Here I deviated slightly from the instructions’ assembly order, and installed the ICs and inductors next. Because I don’t have a cool PCB holder, I solder with the board upside down on my workbench; and because of that, I prefer to install the shortest components first and work up to the tallest,

Theremax PC board, ceramic capacitors installed

which means doing the inductors before the ceramic capacitors,

Theremax PC board, electrolytic capacitors installed

and the electrolytic capacitors last.

Theremax PC board, transistors installed

The transistors are actually shorter than the electrolytics, but they cling to the board pretty well, thanks to the hole spacing being considerably wider than their lead spacing.

Control Panel

Theremax control panel

PAiA’s professionalism really shines here, and my photo doesn’t do it justice. This is a very nicely lettered control panel, as good as those on most of the equipment I own. I like the retro feel of the white paint on black brushed aluminum, rather than some glitzy decals on colored plastic that you might see these days.

Theremax control panel, back

I installed all of the potentiometers and jacks, twisting them as needed to try to get the hex nuts on the front to line up evenly. (I like to use the phrase detail-oriented.)

Theremax control panel, back with ground connections

The instructions say to run bare wire from pot to pot for the ground connection; but I’m not wild about bare wire (especially when it has to loop and could be snagged and pulled out to contact something else), so I put some clear heatshrink over it for sleeving. I also built up the LED assemblies–replacing the provided dark red LEDs with my own green LED for the power light, and a matching red LED for the gate trigger indicator.

Theremax PC board, fly wires installed

Then I soldered all the fly wires to the PCB. The assembly guide gave very specific lengths; but I think things must have shifted since that was first printed, because the wires had completely different amounts of slack once connected. I shortened some wires and should have lengthened others, and made notes about all the changes I made and further changes I should have made. Here are my notes about the colors I used and the lengths I’d recommend:

Board Panel Signal Length Modify Color
A R79-2 P trim 9 1/2 -1 blue
B R79-3 P trim + 9 1/2 -1 white
C R80-3 V trim + 5 +1 white
D R80-2 V trim 5 +1 green
E R81-3 timbre short sine 9 1/2 red
F R81-1 timbre short square 10 1/2 -1 blue
G lug GND 10 1/2 -2? green
H R82-3 pitch CV 12 1/2 blue
J R83-3 volume 9 1/2 red
K R84-3 velocity 9 1/2 +1 white
L J3-T velocity CV 11 -2 red
M J2-T gate 10 1/2 -1/2 red
N J2-R trigger 10 1/2 -1/2 green
R J1-T audio out 12 3/4 -2 1/4 blue
S J5-T volume CV 16 -6 blue
T J6-T mute 16 -5 white
+ S1-1 +12V 12 -1 red
SG J1-S SGND 5 +1 white
lug S1-3 GND 10 green

Theremax control panel, back with signal wires installed

The last thing I did in that batch of soldering was run signal wires within the back of the panel. The instructions gave this step at the same time as the ground wiring, but I wanted to wait until I had the fly wires chosen so I could maintain the same color wire everywhere a signal traveled.

Next Steps

By now, I’ve connected the fly wires to the control panel, built a case, and semi-mounted the kit in the case. I’ll post those pictures soon.

Battery Meter

January 7th, 2007 by Keith Neufeld

Battery Meter Prototype

Here’s a project that I’ve been kicking around for a long time (three years–I guess that’s not so long compared to some of my projects :-| ) and finally built–a battery meter. My friend/enabler Slim Cummings in Pittsburg gave me a couple of surplus 3-1/2 digit, .2V panel meters, and I thought they’d be perfect for testing the freshness of AA and AAA cells.

Meter

The meter was the inspiration for the project. It has a 3-1/2 digit (a 1 plus three 7-segment digits–1888) display, a configurable decimal point, and an Intersil ICL7106CPL LCD/LED Display, A/D converter chip. You give it 9V supply, a jumper for the decimal point, and a voltage input; and it samples the voltage and drives the LCD. It’s a very nice package that just begs to be used for something interesting.

Tester Case and Battery Holder

Parts to Build a Battery Meter

The physical aspects of the project actually took much longer than the electrical. First off, I wanted to find a case with the following characteristics:

  • The case would be held in portrait orientation.
  • The meter would fit across the width of the case, with the battery holder below it.
  • I could fit a 9V battery inside the case to power the meter.

I figured I’d find a plastic project case from Radio Shack with a 9V holder in one end and enough room for everything else to fit . . . no such luck. I figured I’d find something in my junk bin that I could reuse–I was coming close with some old and broken copper-to-fiber media adapters, but they weren’t quite right. Finally I stumbled across the idea of using the plastic case from a data backup tape, and I rummaged around until I found this KAO 8mm data cartridge case.

I’m not wild that it’s translucent–I don’t like seeing the guts of things when I’m using them–but it’s the perfect size. The meter exactly fits across the width, the 9V battery exactly fits in the thickness, and it’s in portrait format. Plus I guess it’s kind of cute opening the case like a cassette case to change the 9V battery.

Single AA Cell Holder

I picked up a single AA battery holder at Radio Shack a couple of weeks ago, and had to mod it a bit to fit it onto the case the way I wanted. The leads originally routed out holes in the ends of the holder, but I wanted them to go straight down through the case. Fortunately, there was already a hole underneath the terminal at each end (you can see the one at the right end of the above picture), and I was able to fish the wires through and tug them into position to make it work.

The holder also had tabs curving slightly around the front, to help hold the AA cell inside. Since I want to be able to insert and remove cells quickly, I removed the tabs by scoring the plastic flush with the rest of the front of the holder, then snapping them off. I also confirmed at this point that a AAA cell would make contact with both ends of the holder, even though it wouldn’t be held as securely. (AAA cells are shorter than AA cells, as well as thinner.)

Project/Tape Case with Mounting Holes

I drilled mounting holes in the case for the meter and battery holder, as well as holes for the wires to go through. The plastic was so soft, I didn’t even bother chucking the bits into my drill for most of the holes–I just turned the bits a few rounds with my fingers and I was through the case.

Stepped Drill Bit Set

I did get to use my stepped drill bit set to enlarge the meter’s mounting holes–the bezel’s posts have larger-diameter plastic shoulders at the base, my mini-drill doesn’t have bits that large, and the stepped drill bits do an excellent job of enlarging while remaining concentric with the pilot hole.

The last tricky bit of work with the drill was the big ugly hole in the center of the picture of the drilled case. I needed a power switch for the meter, but I was loath to have to have to slide a switch or press a button every time I wanted to use it–I wanted it to spring to life when a cell was inserted to test. Plus for reasons to be described a bit later, I planned for a pushbutton on the front already, and I didn’t want to have two.

The solution was simple enough–a pushbutton switch inside the battery holder, actuated by the insertion of the cell itself. I salvaged a tiny microswitch (“nanoswitch???”) from a dead CD-ROM drive (a limit switch from the optical sled), reamed much too large a hole in the case and battery holder, poked the switch in from underneath, and hot-glued it in place.

Meter Wiring

Digital Panel Meter, Component Side

The meter board is built for a range of 0 – .1999 (.2) V, but has pads to set the placement of the decimal point and to provide your own voltage divider to adapt for other ranges. The documentation gave the resistor values to adapt for 20V and 200V, from which it was easy enough extrapolate the nominal values for 2V operation: 9MΩ and 1MΩ. Well, I have 1MΩ resistors on hand, but not 9MΩ, and certainly not precision.

Fortunately, 9:1 ≅ 10:1.1, so I put a 10MΩ resistor into the pads for RB (upper left) and chained a 1MΩ and a 100kΩ in series between the pads for RA. (Those lap-soldering skills from a summer of module assembly at IFR Systems do come in handy.) Also fortunately, the calibration potentiometer had a generous range, so I was able to work around the 1% difference from the nominal ratio.

I also guessed (correctly) that the proper position for the decimal-select jumper was P1, which was missing from the voltage range chart.

Assembly

I had one more set of components to add, but I couldn’t resist wiring it up to see how it worked so far.

Battery Meter, Component Side

I soldered the 9V battery clip’s ground lead to the ground connection at the lower left of the meter board, the positive lead to the microswitch glued into the AA holder, and the normally open (NO) lead from the microswitch to the supply connection on the meter. I also soldered the AA battery holder’s leads to the Vin connections at the upper left. If you look closely enough, you may see that I just tack-soldered them in place with a lot of exposed wire a the end; I plan to come back and organize the wiring more carefully, and didn’t take a lot of extra effort at this point.

Meter in Action

Battery Meter in Action

And it works as planned! Not that there was much surprise, but it’s still nice when a project comes together. I was also pleased that the (undocumented) position of the decimal point was correct. Further, I was hoping to avoid cutting a large hole in the box for the panel meter; and I find it adequately legible reading through the nearly transparent case, without a hole.

I put a AA cell into the holder, the switch activated beautifully, the meter sprang to life, and I had a reading of the AA cell’s voltage. I measured the cell’s voltage in situ with my best voltmeter, calibrated the panel meter’s reading via the potentiometer on the back, and measured with my good meter once more to make sure the voltage hadn’t drifted while I was adjusting. Good enough!

Load Testing

Finally, I drilled two more small holes in the case, poked a pushbutton switch through (in the lower left of the above photograph), and wired a 10Ω resistor in series with the switch, and the switched resistor in parallel with the AA holder. From my first inspiration for this project, I’ve wanted to be able to test cells under load as well as unloaded. I find that some cells I’ve owned–particularly, I believe, when they’ve lost capacity due to age rather than use–will show a relatively high unloaded voltage even though they have very few mAh left. I very specifically wanted to be able to test under load to identify this condition.

And it’s proved itself already. The cell shown under test reads 1.163V, which is by no means new, but might still appear to be usable in some types of devices. But when tested with a load, it drops immediately to ∼.8V and continues to fall toward .7V–not nearly as promising. In contrast, another cell I’ve tested measured ∼1.2V unloaded, but still ∼1.1V under load–very likely quite usable yet.

Sorting Batteries

I’ve already switched to using NiMH cells for all my projects and consumer hardware, but I have a number of alkaline AA and AAA cells around the house from the old digital camera and the Visor PDA, with no good idea which are fresh and which need to be discarded. (Y’know, it’s such a pain to test cells with a voltmeter and only two hands.) I can finally conveniently test and sort them into four categories:

  • Unused: Save for when I don’t have any rechargeables ready, or give with battery-powered gifts to nieces and nephews who don’t do the whole “rechargeable thing.”
  • Still strong: Save for my LED flashlight, which seems to get quite a bit more life out of used cells than do incandescent lights.
  • Weak: An interesting problem. They still have some energy stored, but not enough to use in traditional portable devices.

    I’m planning to build a “Joule thief,” a clever and tiny transformer feedback single transistor inverter that claims to be able to “provide a week of continuous low level light from a battery that would normally be considered dead.” Maybe I’ll end up with a bunch of “electronic candles” sitting around for the next ice storm; who knows.

    Update: Here’s another Joule thief project.

  • Dead: Take to work and toss into the battery recycling bin. (And scrounge “weak” cells out of the bin to take home and make more “candles,” har har! Just don’t tell my wife that I’m stealing other people’s dead batteries now . . .)

Credit

A huge thank-you to my friend Slim, who gave me the panel meters that started this all; and who’s always generous with his vast experience and equally vast stores of electronics surplus.

LED Wrist Rest

January 3rd, 2007 by Keith Neufeld

Last fall, my friend Jeremy and I were killing time at Best Buy when we ran across this blue gel keyboard wrist rest. He was looking for a wrist rest anyway; and since we’re obsessed with blue LEDs, I offered that I could rig it up to glow. He loved the idea, and his wrist rest has been sitting at my house begging for attention ever since.

I’ve been tinkering around with ideas for a month or two, but finally yesterday I cleared some space on my workbench and got at it. There were four main components to the project:

  • The translucent blue wrist rest, which I hoped would have enough internal reflection to glow nicely
  • Blue LEDs, which I harvested from a string of Phillips Christmas lights
  • A USB cable, to supply power to the LEDs (Jeremy preferred USB so the lights would power on and off with the computer)
  • Some means of holding the LEDs in place

Glow Test

I’d been thinking I’d drill some holes into the wrist rest to embed the LEDs, but I wasn’t sure quite what the effect would be. I started testing by putting a couple of LEDs in my LED tester and shining them into the gel pad from different locations and angles, with disappointing results–they didn’t make any part of the pad glow like the edges of plexiglas (high internal reflections from the parallel surfaces) or milky plastic (high diffusion of the light beam).

So they kinda shine through instead of making the whole thing glow, and I just have to deal with that. On the other hand, that meant I didn’t need to bother drilling holes into the gel–I could butt the LEDs up against it and they’d work just as well. So I started thinking about etching a long, skinny circuit board to go across the back . . .

USB Cable

And meanwhile, tried to figure out where to get a USB cable to sacrifice. USB cables cost more than it seems like they ought to, so I was asking around for a dead USB mouse that I could salvage. I really wanted a black cable, to disappear on a dark desktop; and I really wanted a skinny and supple cable, to route well.

Then a couple of guys at work gave me a USB hot plate, I kid you not. It was from a trade show and was supposed to keep a coffee mug warm–which, no surprise, didn’t work out so well, given the limited current USB can supply. It had a skinny cable with silver foil inside–not black, but it still sorta fit with the theme. Perfect. Well, good enough.

Light Bar

Since it would have taken me a while to rig up a PC board for the LEDs, I was thinking about other ways to hold them in place and wire them together. I have some black plastic U-channel pieces about a foot long, which were supposed to be magazine or books supports for some wire cube shelves I bought, but which I’ve never used. Yesterday I cut the brackets off one, drilled holes in the back to poke leads through, and stuck four LEDs into it.

Several of the LEDs in the Christmas tree lights had 250Ω resistors already soldered to one leg, and that turned out to be a nice current-limiting value for a 5V supply, so I just left them in place. I daisy-chain soldered wires to the leads, connecting the LED/resistor assemblies in parallel, and attached the USB cable to one end. [Side note: In the USB cable, the black wire was positive and the white wire was ground. Good thing I checked first.]

Yup, I was soldering wires onto leads that were poking through (and touching) plastic. Surprisingly, I didn’t have much trouble with it–the wires were pretty clean, so they all soldered quickly, and apparently didn’t heat up much. I didn’t notice any melting plastic at all.

I used some big heatshrink to anchor the end of the USB cable in place and provide some strain relief (lower right end of the above pic), and the light bar was ready to go.

The LEDs are on the inside of the bar pointing out. That does a reasonable job of shielding the (annoyingly bright) light sources from direct line of sight, and plenty of light still makes it into the gel pad when the light bar is placed up against it.

This isn’t just a trick of the camera–it really does look like four beams of light aimed through the wrist rest. It’s not quite what I was aiming for, but isn’t a bad effect in itself.

The digital pic exaggerates the effect, but the four LEDs really are two different colors–two slightly more aqua, two slightly more violet. They’re all from the same string of LEDs, and I’d never noticed the effect before–I assume they’re from different manufacturing batches, with not particularly high quality control.

Credits

It wasn’t much of a project, but (1) it provided motivation to get some things cleaned off of my workbench; (2) it was itself part of the clutter on my workbench; and (3) it helped build momentum to get me back into tinkering. I’ve already started on my next project, which should appear here in a few more days.

Thanks to Jeremy Burkey for the photos. I forgot to take pics as I was working on it; and his camera did a much better job with the dark shot than mine would have anyway.

Arduino Circuits and Tutorial

October 15th, 2006 by Keith Neufeld

Tod Kurt is posting lecture notes from an Arduino class on his blog. It’s Halloween themed, and the two sets of notes posted so far look like a really nice introduction to electronics and the Arduino. Go, Tod!

EAGLE to PADS (FreePCB) Netlist Converter

September 16th, 2006 by Keith Neufeld

Based on a reader request, I’ve posted my EAGLE to PADS netlist conversion script, for drawing circuits in EAGLE and exporting them to produce in FreePCB (if the board is too big to draw in free EAGLE).

If you have questions, suggestions, or package translation contributions, please submit them as a comment attached to this post. If you’re viewing this post alone on a page, the comment box should be visible immediately below. If you’re viewing this inline in my blog, you should be able to click the “Comments” or “No Comments” link below to get to the comment box.

LED Clock: Serial Communication, MAX232, and Programming the LogoChip UART

August 22nd, 2006 by Keith Neufeld

For precision timekeeping, both to set the time initially and to correct any drift that may occur, I want the LED clock to have a means to connect to a PC. Further, having serial communication available means that a PC could be used to trigger some of the clock’s special modes, like nuclear meltdown. :-) A wireless ethernet connection would be ideal; but I don’t have the time to devote to it right now, and it would add significantly to the cost. Wired ethernet and USB share the same problems. Wired serial communication is easy.

Well, supposed to be easy. The LogoChip has built-in serial communication; the hitch is that receiving data halts the running program in order to process the data. In other words, any time you set the time, the program would stop running and be unable to receive the data. That’s a drawback.

There’s a better way. The PIC chip that runs the LogoChip firmware has a built-in Universal Asynchronous Receiver/Transmitter (UART–a device to run serial communication in hardware). The LogoChip firmware doesn’t use the UART, but I can use it myself through software–and that’s exactly what I’ve done.

Level Conversion and the MAX232

There’s one trick to using the UART: RS-232 specifies ±12V for its signalling, but the PIC chip runs at 0/5V logic levels. The LogoChip gets away with interfacing to RS-232 by using a substandard transmit voltage and a resistor-based divider on the receive voltage; but there’s one more reason I can’t do that when I use the native UART: Line driver chips invert the signal so that 5V source becomes -12V on the line. The LogoChip is doing the inversion in software when it drives its hacky serial ports, but the UART requires an inverting level converter and has no facility to invert for you.

That means I need an external line driver chip (or hack something together with transistors, but I’d rather not), and that means I need a MAX232. The MAX232 not only converts from 0/5V to ±12V, but also does so without requiring ±12V power connections–it generates ±12V from a single 5V supply.

I ordered samples of the MAX232 in three different packages a while back, and received one of my samples and one of someone else’s samples. I’m trying to get Maxim to send me the rest of my samples order . . . but that’s a whole ‘nother story.

Prototyping the MAX232

A week ago, I gave up waiting on the DIP MAX232 sample and soldered leads onto my surface-mount sample so I could stick it into the breadboard. I plugged in the five 1μF capacitors it uses to step up the voltage, wired it to the LogoChip, and got pretty quick success–I could translate voltage levels going in and out. Cool!

Programming the UART

Then I tried to configure the UART so I could send data, and I couldn’t manage to get anything meaningful to go across the link. Not cool!

On the datasheet, the UART looks a little intimidating to get set up, but it’s really not too bad once you get into it. (Well, mostly.) There’s a little more to it; but you mostly enable UART mode (the pins are regular I/O pins if you’re not in UART mode), set the number of bits you want, program the baud rate generator, and drop data into the transmit register or pick it up out of the receive register.

The baud rate generator (BRG) was my sticking point. It’s set as what divisor to use of the oscillator frequency; except I couldn’t find anywhere that said whether that was the oscillator running the chip or one of the programmable timers. Turns out it’s the main oscillator–mostly.

My first problem was a think-o on the write instruction I used to configure the baud rate generator speed–I had the operand and address reversed. The proper syntax is

write <address> <value>

but I wrote

write SPBRG-9600 SPBRG

I don’t know why, but write <address> <value> just feels wrong to me. English uses verb indirect-object direct-object syntax. (“Give me the money.”) I’m pretty sure the old 8-bit microcomputer BASICs used POKE <address> <value>. So what’s my problem???

Anyway, once I got that sorted out, I had a small matter of determining the correct divisor to put into the BRG. I thought I was trying all the appropriate values from the table in the datasheet, but I could never get real text to come out on my computer. Testing was a hassle, too, as I had only one serial port available on the PowerBook, so I had to keep moving it back and forth between the programming port and the UART.

The datasheet says the divisor for 9600bps from a 40MHz oscillator is 64, but I kept getting garbage on my screen. I thought I was also testing all the different receive rates on my computer each time to see whether I was accidentally at some other rate; but even then, I could never find good data. (In retrospect, that’s especially puzzling, since I should have been at different usable data rates a number of times.) I ended up spewing dummy data (capital “U” is 01010101 in binary) from the LogoChip and looking at it on my scope to see what speed it was.

With the BRG divisor at 64, I got one bit measuring about 5.3 divisions at 20μs/div, or about 9420 bits/second. After some fiddling around, that seemed to work properly (and matches what’s on the datasheet for 40MHz), so I thought I was fine. And at that point, I was able to transmit data to the computer.

Receiving Data from the UART

Receiving is much more difficult, apparently, as I was still unable to get data from my computer into the LogoChip. It would just sit and wait for incoming data, and never actually receive it into the buffer.

Since transmit and receive share the same BRG, I knew I didn’t have a speed problem. I wondered whether the MAX232 was bad; but with my scope, I was able to confirm the flow of 5V translated data from the MAX232 to the PIC. I wondered whether the PIC was bad; but I wrote a test routine to take the PIC out of UART mode, and I was able to see changing data levels on the receive pin when accessing it manually.

What the heck???

I knew Tom McGuire had got RS-232 communication working using the UART, and so I asked him about it. He sent me some sample code (which was virtually identical to mine) and some things to check (which were all okay), and then volunteered to help me troubleshoot it. I dropped by last night after work, we hooked it up to his digital scope and saw that data was flowing where it was supposed to, we compared it to more of his code and found it was the same, and so once again, what the heck???

And then we looked at the receive error flags. I’d been wondering whether I was going to have to deal with them, but figured I’d let the master inspect my circuit and code first. But he found some other of his code that examined the receive overflow flag, and when I tested mine, I found it was set. Overflow? The UART hadn’t received any data successfully; how could it overflow???

Well, you clear the overflow bit by taking the UART out of continuous receive mode (and putting it back in). I added code to my receive routine to check for and clear the error, and BLAMMO! Instant working serial communications!

Next Steps

As noted yesterday, I’ve got my board for the first third of the six-digit display laid out (with the other two-thirds requiring simple clone/shift/chop operations). I added some identifying text to it last night, and I have a tentative appointment with Tom on Friday to mill the board.

Meanwhile, I now know that my serial port hardware is all okay. So even though I haven’t written the software to sync time with a PC, I can go ahead and design and build the clock’s controller board.

I should have two-digit display pictures up by sometime Saturday or Sunday . . .

LED Clock: Laying Out the Full Display

August 21st, 2006 by Keith Neufeld

Background and Long Delays

It’s actually been six weeks since I got my one-digit prototype working. Since then, I’ve prototyped the DS1302 real-time clock chip, done troubleshooting on some weird voltage problems, finally fixed the voltage problems, and written a demo of timekeeping with the one-digit display. I haven’t exactly been idle; but I haven’t done anything about expanding the one-digit prototype into a full display.

Well, it’s a slight exaggeration to say I haven’t done anything. About three weeks ago, I cloned the single-digit schematic in EAGLE, edited out the colons and decimal points that won’t be used in the display (between the two digits of the hour, of the minute, and of the second), and got it all to fit onto one “sheet.” But I hadn’t done anything yet about expanding the data bus to make separate latch lines for the six digits, nor had I even started working on a board layout.

Part of the problem was that the freeware version of EAGLE only does board up to about 4″x3″, which was the size of my one-digit display. I knew I was going to have to switch layout software to build a bigger display, and I’m always slow getting around to installing and learning new software. Also, I talked to Tom McGuire, who volunteered to mill a display PCB for me, and he indicated that he couldn’t do a board quite as long as the full display (15-16″). He suggested milling half, rotating around a reference point, and milling the other half, but that sounds a bit tricky to get the alignment just right.

Last week, I finally resolved myself to making the board in three sections, for HH:, MM:, and SS. That makes it easier to manufacture; and if I eventually have a means to make the whole thing in one, I can presumably paste the pieces together into one large layout.

You’re Thinking About Building a Bigger One

Through a strange sequence of events, I was hanging out at Cort’s house Saturday morning, sitting around with Cort and Shannon and Brad talking about various things. I had my PowerBook on my lap and I was tinkering with EAGLE while we visited. That’s right; I finally got back to the clock display.

I saved my six-digit circuit in case I ever want to go back to it, but copied it to a new two-digit layout and chopped out the other four digits. I cut out duplicate connectors and started wiring things together. I laid out the latch lines for all six digits, and set up the bus to have input and output connectors at opposite ends, for daisy-chaining the three boards together (if I choose).

I agonized about how to select/connect the appropriate latch line for each digit. Ideally, I’d like to have DIP headers, and you plug the jumper across the line corresponding to which digit you’re configuring. That would allow reconfiguring one digit as another for troubleshooting, to determine whether a problem was in the controller or the display. Between lack of room on the board to position a full DIP header, and the difficulty of soldering it onto the back side of a single-sided board (so it could be accessed without removing the display from the clock case), I gave in and settled on using a jumper wire to bridge from the latch bus to each digit’s latch line.

EAGLE Circuit → FreePCB Board

I had intended to use FreePCB for designing larger boards, because it looked like a nice package, and I somehow got the impression it could read EAGLE netlists (more on that later), to import part and connection information into a board layout and prepopulate it. FreePCB was designed by an engineer who was dissatisfied with available, affordable board layout software and decided to write his own. He’s done a very nice job!

Only problem is, it does not read EAGLE netlist files. FreePCB reads PADS-PCB format files, and it turns out that’s not what EAGLE outputs. Late Saturday, I tried to transfer my netlist from EAGLE to FreePCB, and quickly found out that the formats are different. Between Google and the FreePCB user forum, the only thing I could find about EAGLE and FreePCB is that the FreePCB author figures EAGLE’s format is pretty simple, and someone could write a translation utility.

So I did. EAGLE generates one partlist file and one netlist file; PADS-PCB has the two combined into different sections of the same file. I wrote a Perl script to parse the EAGLE files and translate into PADS format, and it was really pretty simple.

Except for package types. The export/import files contain an indication of what package type each device has, so the PCB software can prepopulate your layout with appropriate footprints that you just have to drag and drop into place. Naturally, EAGLE and FreePCB use different package names, so I added a hash lookup to translate those as well. Unfortunately, that’ll be the trickiest part of the program–comprehensively identifying and accommodating all the different package types.

By midnight, I had a pretty good translation going and most of the parts placed (but no traces routed) for my two-digit board–and then I noticed that FreePCB doesn’t generate outline milling information. It makes Gerber trace files and Excellon drill files, but nothing for CNC milling. Since I really wanted to be able to mill boards with Tom, and since I insist on being able to mill boards for myself in the future, I quit for the night and waited until I could talk to Tom about options.

FreePCB Board → DeskPCB CNC Mill File

I talked to Tom early yesterday afternoon. I was hoping he had a way to generate outline milling code from standard Gerber trace files, and he pointed me to a piece of software called DeskPCB. It’s $95 for a license, and there’s a time-limited demo available for free download. Generating outline code is exactly what it does–it generates “trace isolation milling and drilling” files out of Gerbers and Excellons. Shows you the original board and the mill pattern right on the screen, too, so you can confirm it’s doing what you expected.

It’s pretty slick! And that means I now have a complete path from circuit design to board production:

EAGLE for circuit design
    → export EAGLE-format netlist and partlist
    → Keith’s script to convert to PADS netlist

→ FreePCB for board layout
    → create Gerber and Excellon files

→ DeskPCB for outline milling
    → create G-code

→ LinuxCNC (née EMC) for CNC mill control
    → mill a board

Very, very cool.

You Doin’ Anything with All That Fancy Software???

Yup, sure am. I sent Jeremy a screenshot of the two-digit parts placement to stitch together into a six-digit display so we could check the spacing. My first version had .6″ between adjacent digits and .4″ between the digits and the colon.

Six-Digit Layout with .4" to Colon

From across the room, that looked like there wasn’t really enough separation to the colon, so I asked Jeremy if he could scoot them another .1″ or .2″ apart.

Six-Digit Layout with .6" to Colon

The second version with .6″ on each side of the colon looked good enough that we’re going with it. It’ll be hard to know the exact effect until we see the real thing, but this is close enough to build the first clock. If we want to change the spacing from the first to the second, it’ll be easy enough to do.

I used my single-digit layout as a reference for placing most of the traces on the two-digit, and got all of the new stuff (different power layout, six-digit latch bus) incorporated as well. By late last night, the entire board was laid out and ready to produce.

I’ll still look at it to see whether I can reroute traces to eliminate some of the jumper wires, and I still need to put design and contact information onto it–but it’s a working design that I could now produce if I chose, and that feels great! Tom has Fridays off and offered to take me to his office on a Friday to mill a board, so I’ll see whether I can set something up for this week.

Next Steps

Because the margins around the perimeter of the display aren’t the same as the inter-digit spacing, I’ll actually have three different boards for HH:, MM:, and SS. I’ve laid out the board for HH:, and it’s a trivial matter of spacing to change the board outline for MM:, and spacing and chopping the colon to make the SS. I feel I should build and test the entire HH: board before producing the other two, so I hope to assemble the first board this coming weekend.

At 174 LEDs per full display, plus 31 already used for the prototype single digit (which I don’t plan to disassemble), I’ll only have used 379 of my 498 LEDs, leaving 119 unused–enough that I could comfortably leave an entire HH: board (59) assembled as a working prototype rather than a production board. I’d be happy for it to be the first real board for a clock, but it doesn’t have to if something doesn’t work out.

I have four sample A6276 LED driver chips, enough to power two digits. I’ll need to order more before I can build the entire display.

Tom was planning to help me debug some UART problems (which I haven’t blogged yet) tonight, checking timing with his digital scope and stuff. After that’s ironed out, I can move the controller circuit from breadboard to EAGLE and lay out and produce the controller board too.

We’re getting closer!

Tektronix Logic Analyzer

August 17th, 2006 by Keith Neufeld

A logic analyzer is a device somewhat like an oscilloscope, in that it displays the waveforms of electronic signals on its screen. It differs from a traditional scope in that it captures data in memory for longer study of ephemeral events rather than only displaying what’s happening in realtime (modern capture scopes do this, too); it typically has many channels (8, 16, or more plus clock and other “meta” inputs) rather than one, two, or four; and it can be configured for complex trigger events rather than only the level of a particular signal. In short, a logic analyzer does for digital system troubleshooting what a scope does for analog signal work.

I’ve never had a logic analyzer, and last spring when I started working more with the LogoChip and other digital parts, I started hankering for one. I found and bought this Tektronix on eBay for only $30 plus shipping:

Tektronix Logic Analyzer, Front

It has text-based menus, online help, and a charming monochrome green screen. It’s built almost like general-purpose 6502 luggable microcomputer, with a big stack of signal handling boards in it.

Tektronix Logic Analyzer, Interior

The only catch was, it didn’t come with any probes. I was hoping at first that all I needed was ribbon cables to fan out from the dual-row headers on the front panel, but I couldn’t find pinouts anywhere (including online). I emailed Tek’s tech support asking whether they might still have pinouts available that I could download (since they no longer sell or support this analyzer and wouldn’t be losing any money on me), and I got an incredibly helpful phone call back from them.

Their tech support representative indicated that the probes have active CMOS circuitry in them, so you need more than just pinouts; and he gave me the model numbers of three different probes that should work with that analyzer. I saved an eBay searched and waited, until a couple of weeks ago when one of them popped up and I bought it for about $25.

Tektronix P6444 Logic Analyzer Probe

There’s obviously a fair bit of circuitry in there that I wouldn’t expect to be able to duplicate, so I’m glad to have bought it. Now I still need cables to go from the probe module to the circuit being analyzed. And after I received the probe module, the seller emailed me to say that in the same box as the module, he also had these connectors that went with it; did I want them for another $25?

Tektronix Probe Wiring Assembly

Okay, if he’d included them in the auction with the module they go to, he probably wouldn’t have got any higher bids. It’s his job to maximize his profit, so I suppose he’s doing what he ought to. But I feel like a guy who just negotiated to buy an antique auto out of a farmer’s field, only to be asked whether I’d also like to buy the engine that goes with it that I have over here in the barn. Yes, sure I’d like the wires that go with the probe module you already sold me; and at the same time, no, I really don’t feel like sending you any more money for something that really belongs with what I bought the first time.

Furthermore, most of the time I’ll be using the analyzer, it’ll be with circuits on a breadboard, where I’m going to have to make my own cables terminating in pins anyway. These probe tips have really cute grippers on them that are perfect for grabbing onto classic DIP leads, but useless on a breadboard.

But I was still wavering, since I don’t know how to describe these tips well enough to save an eBay search that would pick them up, making this a rare opportunity to get something I might eventually want.

I called Cort last night to ask his opinion, and the moment he said “troubleshooting your arcade games,” I knew what the answer was.

I just sent the PayPal.