Tweet Follow @jack_ganssle
Go here to sign up for The Embedded Muse.
TEM Logo The Embedded Muse
Issue Number 292, October 5, 2015
Copyright 2015 The Ganssle Group

Editor: Jack Ganssle,
   Jack Ganssle, Editor of The Embedded Muse

You may redistribute this newsletter for noncommercial purposes. For commercial use contact

Editor's Notes

I am doing public versions of my Better Firmware Faster seminar in Baltimore on Nov. 2 (sponsored by the Barr Group), Sydney, Australia Nov.9, and Auckland, NZ Nov. 16.

The good news is that embedded code is much better than any in the rest of the computer industry. Did you know that, on average, 95% of all of the bugs in firmware are removed pre-shipment, compared to 85% for the rest of the computer business? But 95% means too many bugs are getting out the door. We've got to do better.

Did you know the average firmware project spends half the schedule in defect removal? That's far too high.

Join me to learn how to greatly reduce bug rates while slashing the schedule! Over 5000 engineers have taken this class on six continents.

This is a very-fast-paced full day seminar. It's fun, it's informative, and it only covers practical ideas you can use today.


The seminar covers these subjects:

Languages: C, C++ or Java? What about code reuse - is it a myth? How can you benefit? How to control stacks and heaps.

Structuring Embedded Systems: Manage features... or miss the schedule! Using multiple CPUs. Architectures for faster development.

Overcoming Deadline Madness: Negotiate realistic deadlines... or deliver late. Scheduling, the science versus the art. Overcoming the biggest productivity busters.

Stamp Out Bugs: How to manage bugs to get great code fast. Quick code inspections that keep the schedule on-track. Cool ways to find hardware/software glitches.

Managing Real-Time Code: How to design predictable real-time code. Managing reentrancy. Troubleshooting and eliminating erratic crashes. Building better interrupt handlers.

Interfacing to Hardware: Understanding high-speed signal problems. Building peripheral drivers faster. Inexpensive performance analysis.

How to Learn from Failures... and Successes: Embedded disasters, and what we must learn. Characteristics of a healthy firmware team. A seven step plan to firmware success.

There's more information on all three public seminars here. Or, I can deliver the seminar on-site, at your facility, just to your engineers.

Quotes and Thoughts

Programs do not acquire bugs as people do germs - just by hanging around other buggy programs. They acquire bugs only from their authors. - Harlan Mills

Tools and Tips

Please submit clever ideas or thoughts about tools, techniques and resources you love or hate. Here are the tool reviews submitted in the past.

In the last issue I reviewed the book ARM Assembly Language by William Hohl and Christopher Hinds. Chris responded and noted they have a companion web site at It's a great resource and is recommended.

A reader who wishes to remain anonymous poses a question to Muse readers. How do you deal with geographically-disbursed teams, especially in the embedded world where hardware and software skills may be 8 or 10 time zones apart? Test equipment might be in one location, digital or analog expertise in another, and firmware somewhere else. How do you manage these projects?

Freebies and Discounts

Nicholas Graumann is the lucky winner of last month's contest; he got a Real-Time Current Monitor that was donated by

In the next article in this issue I review The Art of Electronics, third edition. This month I'll give away my copy of the second edition. It's a little tattered, but is packed full of wisdom about designing analog and digital circuits. Plus, a little mystery insert I've always stored between its pages is included.

The contest will close at the end of October, 2015. It's just a matter of filling out your email address. As always, that will be used only for the giveaway, and nothing else. Enter via this link.

The Art of Electronics, 3rd Edition

Embedded systems are a unique mix of firmware and hardware, the latter comprising electronics, mechanics and all sorts of disciplines. Electronics lives at the heart of the embedded world. The best book ever written about the subject is Horowitz and Hill's The Art of Electronics, second edition.

Until now. There's now a third edition of the book, which at 1192 pages is an expanded and updated version of the tome which has been in print now for some 35 years.

If you're not familiar with The Art of Electronics, it's a practical guide to the field written for students, but equally valuable - no, necessary - for engineers designing circuits. While incredibly accessible and written with a bit of whimsy with clear, non-academic prose, readers will get most out of the book if they're willing to tackle the math, which never goes beyond first-year calculus. If you took circuit theory in college you remember the mind-bending analysis that challenged third-year students; Horowitz and Hill leave all of that out. It isn't always an easy read, but is always worth your while to work to understand the material. An appendix does go over the math needed, but to really get the material one does need a decent grounding in trig, a little complex math, and some calculus.

If you are familiar with the second edition, the new version includes more on logic design, expanded discussion about switchers and power conversion as well as low-noise and precision circuits. Completely new topics are too numerous to list, but include much more on FETs of various kinds, transimpedance amplifiers, delta-sigma and precision multi-slope converters, serial buses and more.

Despite the 1192 pages there's more to the field of electronics than the authors can cover. RF, with the exception of transmission lines, is pretty much left out, as is power transmission. I know little about the latter, but RF is important to many of us, so figure on supplementing this book with the ARRL Handbook. The authors note that there's more they would have liked to have added, so will release The Art of Electronics: The x-Chapters at some point. I can't wait.

The book starts out with the very basics but by the second chapter goes into designing with transistors and op-amps. Yes, the theory is there; by page 90 they're into the Ebers-Moll model, but that is complemented by discussions of SMT part markings, connectors, and other matters necessary to actually build circuits, in addition to designing them.

I won't list the material covered as the table of contents is here. Suffice to say that for a couple of decades The Art of Electronics has been always within reach in my office, and this new edition is worth every penny. Unless and until a fourth edition comes out, you can pry my copy from my cold, dead hands.

Elektor is offering a 10% discount on the book to Muse readers here.

Debouncing Switches and Encoders

John Youngquist wrote the following essay about debouncing switches and encoders, and generously offered to share it with Muse readers:

Some of the worst code I have ever seen is that written to de-bounce switches and handle rotary encoders. I've seen screen loads and Kbytes spent when only a few instructions are required. I guess it is simple when you know how, but apparently it's not widely known or obvious.

I developed this de-bouncing method decades ago trying to force fit applications into the 1Kbyte code space of an 8048 processor. I have used variants of this simple technique ever since. The 8048 was Intel's first embedded processor to have 1K of on board program space in erasable EEPROM. I used it in many designs.

The code shown here for the rotary encoder is far more recent. Over the years I have written several convoluted quadrature decoding schemes but an elegant solution always eluded me. Finally this one is clean and simple. It's intended for polled rotary switches where speed isn't critical. For high speed motor encoders an interrupt driven method is required.

De-bouncing simple switches:
The methods described here depend on periodically calling the routine every few milliseconds. Ten milliseconds or so will work. It is assumed that with each call you read the current switch status from a port. This method will simultaneously de-bounce 8 switches in an 8 bit word. It requires only 3 instructions and one status storage byte. It can be triggered by positive or negative transitions. It is a simple logical edge detector that requires two sequential identical states to trigger it.

Logical edge detector and de-bouncer in 8051 code:


As written this code fragment puts a logic one in each bit position where the switch is closed. It returns zeros everywhere else. The bits change state only after two identical sequential states. It responds to a zero to one transition as shown. If you want the other polarity complement the input byte first.

Sensing both states:
In some applications like rotary encoders, the detection of both switch opening and closing is needed. In this case the logical edge detector must sense and de-bounce both positive and negative going edges. This is easy to do. Just duplicate the input bits into unused locations in the input byte and complement them. Now those will go true for a change of opposite polarity. All the sensing work is still done with the same three instructions. You can see how it's done in the rotary example below.

Quadrature Rotary Encoders:
Most rotary encoders toggle the signals on two lines to create quadrature code. It is also called "Gray Code" because only one of the two outputs change at a time. They also explain it as one of the two output signals lags the other by one quarter of a cycle. It is sometimes referred to as a 90˚ lag but that is confusing. Don't think it has anything to do with degrees of switch rotation. Beware that some ALPS encoders look like quadrature encoders but aren't.

Quadrature encoding plays a critical role in both linear and rotary position encoders because it can convey direction of motion or rotation. Of course knob encoders need direction too. Most quaddecoding methods are clumsy and convoluted but this one is not. In fact it is a surprisingly simple 3 step process.

1) Edge detect and de-bounce both polarities of both encoder phases.
2) Exit if zero (no change)
3) Logical test for direction

For a rotary encoder connected to P1.6, P1.7 the following sequence is the setup for dual polarity de-bouncing. It duplicates bits 6,7 in bits 4,5 and compliments them with an XOR instruction.


Now the ACC has the original bits read from the encoder in bits 6,7 and the complement of those bits in bits 4,5. Now the de-bounce edge detector logic previously described identifies both rising edges in ACC bits 6,7 and falling edges in ACC bits 4,5.

;At this point if the ACC is zero no rotation has occurred and we exit.

If the result is non-zero rotational motion has occurred, we need only determine the direction. While direction information is not available in the ACC it is easy enough to derive. First we do a 4 bit circular rotate of the ACC to the left. This means after the rotation we wrap the carry bit around to ACC.4 (accumulator bit 4).

          RLC A ;ROTATE LEFT
Now we extract the direction with a simple logical AND of the ACC and the STATUS byte used in the edge detector code:

The zero/non-zero status of the ACC tells the direction. You might increment or decrement your count accordingly.

It's hard to believe but that is all it takes. On an 8051 the entire process consumes about 25 cycles.

The Final Say on Prototyping With SMT Devices

Muses 290 and 291 had a lot of excellent suggestions from readers about prototyping with often-tiny SMT components. A few more have dribbled in.

Erik Beall wrote:

I've thoroughly enjoyed the SMT prototyping tips from readers in this and the last Embedded Muse. I know you've already done two on this topic, but I have one more for you in case you find it interesting enough, a video by Martin Lorton (co host of the Amp Hour) where I show a build of a prototype board that includes some QFNs and 0402s. This video (unfortunately quite long) can help give readers confidence that it is possible for nearly anyone to do. The video is at and the build bits start at 22:00 but the best may be a sped-up board build that starts a bit after 33:10.

Dave Harper suggested:

Regarding the "Prototyping With SMT Components" column, sorry I'm a bit late sending this in but I've been fighting these very issues recently. Most all aspects of SMT Prototyping have relatively low cost solutions, sometimes multiple ones allowing a user to pick what works best for them (ie: stencils vs. a low cost paste dispenser). The one piece that seems to be missing is a low cost Pick and Place tool. A lot of folks have mentioned good success just using tweezers or a small vacuum pickup tool. I suspect they may be in their 20's and 30's. Add another 30 years and they would find their eyesight is shot and even if it wasn't, their hands will no longer be steady enough to accurately place very tiny parts. After a lot of digging I found this link:

Extremely clever solution costing around $100 or so. Very cool video at the end. Building one of these is something I will be doing very shortly.

John Johnson added this:

The mechanical tools in some PCB Layout tools are lacking when it comes to odd shaped PCB boards or where some mechanical precision is required. Fortunately, the PCB Layout tools I have used have an import facility where a PCB outline defined in a CAD package can be imported.

The PCB outline can be defined in a CAD package and then exported as a DXF. The DXF file can then be imported in to the PCB Layout tool.

Note: At least one low cost vendor does not allow holes but does allow fairly complex perimeter routing. The CAD technique described above can be used creatively to address this problem.


Let me know if you’re hiring embedded engineers. No recruiters please, and I reserve the right to edit ads to fit the format and intents of this newsletter. Please keep it to 100 words. There is no charge for a job ad.

Joke For The Week

Note: These jokes are archived at

From Harold Kraus:

We follow the irRational Unified Process for our development life cycle.

It never terminates.

Advertise With Us

Advertise in The Embedded Muse! Over 23,000 embedded developers get this twice-monthly publication. For more information email us at

About The Embedded Muse

The Embedded Muse is Jack Ganssle's newsletter. Send complaints, comments, and contributions to me at

The Embedded Muse is supported by The Ganssle Group, whose mission is to help embedded folks get better products to market faster. We offer seminars at your site offering hard-hitting ideas - and action - you can take now to improve firmware quality and decrease development time. Contact us at for more information.