Fanie Duvenhage, Product Marketing Manager, Microchip
Chances are that if you ask the typical electrical engineer to generate a 120-Hz sine wave, delay a signal by 300 µsec, or create a 10-µsec pulse once every second, most engineers would not think of using a microcontroller. Most engineers would dust off their electronic cookbooks or jump on their favorite search engine looking for the solution. Once they do that, no doubt they would most certainly get an answer that requires a few resistors, a few capacitors, a few op amps or comparators, or maybe a few discrete logic components. The circuit would probably work until you change the specification to a 150-Hz sine wave, a 100-µsec delay or two 10-µsec pulses once every second. Then it would be back to the drawing board. The changes might be easy, but it would require replacing a few components or probably redesigning the whole circuit.
The problem with many of the thousands of cookbook circuits is exactly that—there are thousands of variations and different configurations. Even with the fancy search engines we have on the Internet it can take forever to find the right circuit or component that does exactly what you need, and most of us don't have the luxury to design and build our own chips.
The fact is that the circuits you see in textbooks using 555 timers, discrete logic, and various op amp or comparator configurations are often outdated. Many of these circuits can be replaced with a small microcontroller that is a lower cost, occupies less board space, and provides a solution that can be changed by simply reprogramming the microcontroller. The newer ultra-low pincount microcontrollers require very few external components (if any) and they all have Flash memory, so you can reprogram them without having to worry about special equipment to erase the device. They also come in small 8-pin packages, and now even in 6-pin SOT-23 packages—that is the size of a transistor. If you are an electrical engineer and you have not written code for a long time, you better start.
Take, for example, the first problem of generating a 120-Hz sine wave. I can utilize a Wien Bridge oscillator, but the output amplitude is at the rails, and that causes high-output distortion. There are ways around this, but you'd have to throw more capacitors, resistors, transistors, and maybe another op amp in there. I could also implement a Phase Shift, Bubba, or Quadrature oscillator to do the same thing a little better. But how do I choose which one? The thing is, all of these solutions give you one frequency, and please don't expect it to stay at that frequency if the supply voltage or temperature changes. All of the different components have different specifications related to stability and tolerances, so to protect the behavior of your design with all this variability is almost an impossible task.
A better way to do this would be to use a microcontroller with one resistor and one capacitor to filter a pulse width modulation (PWM) output as a low-cost digital-to-analog converter. The internal oscillator of the microcontroller is probably more stable over voltage and temperature than any of the previously mentioned topologies. Changing the frequency is easy—just write a few lines of code, program the microcontroller in the circuit and you are done. To change the amplitude, just change a few values, program the microcontroller and you are done. Gating the output based on a control signal is also easy—just write a few lines of code… you get the picture. If you really want accuracy, you can run the microcontroller clock from a crystal. I would like to see how you do that with a phase shift oscillator.
Reach Fanie Duvenhage at firstname.lastname@example.org