Electronic Design

Just One Microcontroller Pin Sets Programmable Timer's Interval

Most digital timers require a set of dip switches or rotary BCD encoders to preset their interval. But those techniques consume eight or more inputs from a microcontroller's (MCU's) I/O lines. To alleviate the problem, this circuit forms a digital timer that only requires one input pin of the PIC16F872 MCU to adjust its interval (see the figure). The MCU performs all of the functions, a-d converting, timing, and decoding to drive the displays.

The timing interval range for this circuit is from 1 to 199 seconds with a resolution of 1 second. It can be adjusted with potentiometer R1, which generates an analog voltage to the PIC's AN0 input. This voltage is read by the analog-to-digital converter (ADC), which in turn generates the respective binary reading. Using lookup tables in the software routine, such binary readings are converted to BCD code and translated to a seven-segment code. This is the code required to drive the 2.5-digit LED digital display. The digital reading displayed now represents the timing interval in seconds.

Once the trigger is actuated by a negative-edge pulse generated by the pushbutton connected to RA1, output RA3 goes to a logic one, and the digital reading starts decrementing each second. When the digital reading reaches zero seconds, RA3 goes low and the display presents the original preset number. This way, the timer can be triggered again with the same interval if desired.

A trigger pulse on RA1 typically has a 50-ms duration, and the MCU reads this input every 73 µs, which is sufficient to catch such a pulse. Also, that means the MCU updates the display every 73 µs. Debouncing the pushbutton is performed through software because only the first high-to-low transition is read to start the timing routine.

A new timing interval can be obtained by adjusting R1 before pulsing the trigger input. Once the cycle starts, the interval can't be changed. The MCU is constantly reading the AN0 voltage. When this voltage is changed by the user, the reading is increased or decreased.

The software routine for the PIC16F872 MCU, PIC16F872 Routine.asm.doc, can be found in the online version of this article. By changing the constants in the subroutine "DELAY," the MCU can be adjusted to supply pulses from 1 to 199 milliseconds, minutes, or even hours.

When the input voltage is changed, it's first compared against the ADC reference voltages VREF+ and VREF2 , which are connected to VDD and VSS, respectively. Register ADCON0 configures these reference voltages. Once the a-d conversion is processed in 20 µs, the result is stored in register ADRESH. Then the BCD to Binary conversion routine "BCDBIN" is executed, and finally, this result is converted to a seven-segment format to drive the displays. The table shows nine typical cases. If the user tries to set a timing interval that's out of range, the display will present the message "Err" to indicate an error.

The output LED connected to RA3 can be substituted by an optocoupler or a transistor to drive a relay. For battery-powered circuits, the output pulse from RA3 can be replaced by a 1-kHz frequency with a duty cycle of 50% to drive a coil's relay and save energy. Also, the C.C. LED displays can be substituted by an alphanumeric LCD readout, but an additional subroutine is required to handle such a device. Finally, for industrial applications, it's recommended to use a 4.00-MHz crystal with an extended temperature range.

The timing interval on this MCU-based programmable timer is set by adjusting the analog voltage at the AN0 pin.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.