Use power line for baud-rate generation
Joseph Julicher, Microchip Technology Inc, Chandler, AZ -- 8/16/2001
One cost-saving measure associated with 8-bit embedded microcontrollers is to use a resistor-capacitor oscillator. These RC oscillators are inexpensive, but the trade-off is low stability with temperature and voltage. In many applications, the low cost of an RC oscillator is alluring, but the application requires a stable clock source for baud-rate generation or event timing. In these cases, you can find a low-frequency, stable clock source and use it to calibrate a baud-rate generator or event timer. One source of a low-frequency, stable clock is the line voltage. This voltage is a good source 50- or 60-Hz frequency that you can easily interface to the microcontroller's 16-bit timer. By counting CPU cycles for a half cycle of the external clock, you can determine the frequency of the microcontroller's internal RC oscillator and calibrate the baud rate.
The PIC16F627 flash microcontroller can benefit from this technique. This device has an internal 4-MHz RC oscillator. You can create a simple capacitor-coupled circuit to allow the microcontroller to see the pulses from the power line. You can power this PIC microcontroller from a separate circuit that includes voltage regulation (see Tech Brief 008 at www.microchip.com concerning a transformerless power supply). The power line supplies a solid 50- or 60-Hz reference frequency. You can use the 16-bit Timer 1 to time the internal oscillator. As the internal RC time constant drifts, the timer count changes, and you can use the value to determine new values for the baud-rate generator. If you adjust the baud-rate generator appropriately, you can maintain the baud rate to ±2% of the desired value. The incremental cost may be less than the cost of a crystal or ceramic resonator. You can also use this technique to periodically learn the value of the internal RC oscillator to calibrate time captures of external events. The following is an example of the technique: The base INTRC value is 4 MHz, and the power-line frequency is 60 Hz. Set BRGH for a baud clock 16 times the baud rate, and set Timer 1 prescale to 16. The desired baud rate is 9600.
The formula we use is ((Timr1 value×prescaler×reference frequency×baud multiplier))-1=SPBRG value. We used a spreadsheet to run some sample numbers, and the results are in Table 1. To use the technique with a PIC microcontroller, simplify the math to (Timr1/160)-1=SPBRG. You will find that 9600/(16×60)=160. This simplification causes a slight error; the rounded off value of SPBRG becomes 27 instead of 28. The error at 4.4 MHz becomes 2.3% instead of -1.22%.
© 2009, Reed Business Information, a division of Reed Elsevier Inc. All Rights Reserved.

