Channels ▼

Al Williams

Dr. Dobb's Bloggers

Force of Habit

February 27, 2012

Sometimes we do things the hard way out of habit. Instead of thinking about a design, we just rely on things we know work. I can't tell you how many times I've spent searching for a remote control to turn off a TV only to realize it would have been faster to just walk over to the TV and turn it off manually.

An old friend of mine showed me a Microchip PIC design to monitor a battery. The controller simply waited for the battery to reach the trip point and caused an indicator to blink. The battery drives a large load, so there is plenty of juice left over even at that point.

At first I didn't think there was anything odd about what he showed me. He selected an eight-pin device with an onboard A/D. A voltage divider brought the voltage in range and the software set a digital output at the right time.

Sounds fine right? Then it occurred to me that we had both fallen into the habit trap. How do you measure voltage? With an A/D, of course. In this case, he didn't actually need to know the voltage, just if it was under threshold. I suggested he use a digital input with the divider set to hit the input threshold.

This leads to simpler code, at least potentially. The really interesting thing, though, is to add back some code complexity and monitor the input with an interrupt pin. This might seem like overkill to use an interrupt, but using one will let the code mostly sleep. That leads to very low power consumption.

Of course, you could also argue you don't need a microcontroller at all for something this simple. That's true, although I've oversimplified the project a bit in the interest of clarity. Still, all the work only happened once the threshold tripped. Besides, the PIC in question was a PIC12F629, which is well under a dollar. Even if a comparator and an LED flasher would work, the difference in cost would be very small. Besides, there are even less expensive, small CPUs out there from Microchip and other vendors. The 12F629 just happened to be in the circuit.

This chip has an interrupt-on-change feature that can monitor I/O pins and tell you if any changed. However, for this case, a single interrupt line is sufficient. The GP2 pin serves as a general-purpose interrupt and can be set to trigger on a falling edge by setting bit 6 of the OPTION register. There's no need for an external clock, since the PIC can use its internal one. High timing accuracy is unnecessary in this application.

About the only flaw in this scheme is that the input pin threshold may not be exact. In this case, that wasn't critical, but there are still other tricks you can use. For example, the same PIC has a proper comparator and internal voltage reference. These operate during sleep (but do consume a bit more power) and can wake up the processor.

The moral of the story is really in two parts: First, don't just fall back on habits when making design decisions. Second, know your hardware and think about how to apply it to the problem at hand.

As an example of my second point, think about this question: How would you do a rough (+/- 1 degree C) temperature measurement? Habit would suggest a thermistor or a thermocouple or an integrated temperature sensor. If you are really old school, you might use an A/D to read a diode temperature junction. Once you've got your simplest answer, read this paper, also by Microchip. Did you break your habit?

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video