Project: PIC 4-Digit 99 Minute Timer

Discussion in 'The Completed Projects Collection' started by MMcLaren, May 1, 2012.

  1. MMcLaren

    MMcLaren Thread Starter Well-Known Member

    Feb 14, 2010
    I'd like to present a relatively simple 4-digit 99 minute timer project. The low parts count design is a result of experiments with some very bright miniature 4-digit displays available from Sparkfun that come in red, green, yellow, white (all $1.95), and blue ($2.50) colors.


    In addition to the Digital 99 Minute Timer presented here, the circuit design, with minor modifications, might support several other projects, including, but not limited to;

    () Digital Voltage Monitor
    () Digital Thermometer
    () Digital Thermostat
    () Repeating On/Off Interval Timer
    () Digital Alarm Clock
    () Digital Clock/Calendar/Alarm/Timer
    () Digital Stopwatch
    () Digital RPM Indicator


    You'll need a programmer of some sort to program the PIC16F1828 device and you'll also need a regulated 5 Vdc power source to power the circuit. You'll also need a simple transistor, opti-isolator, SSR (Solid State Relay IC), or similar driver circuit which will take its input from the active high output signal on the RA2 pin. The output circuit you choose depends on what you need to drive (relay, etc.)



    The wiring diagram shows several components that are not required for the 99 Minute Timer application. Specifically, you don't need to install the crystal and capacitors in the oscillator block and you don't need to install the two discrete 'A' and 'B' LEDs for this particular project.

    The little 4-digit displays are so bright that I decided to drive them one segment at a time (1/32nd duty cycle) directly from the PIC16F1828 I/O pins (no column/digit driver transistors). Due to the low duty cycle, I omitted current limiting resistors for the cathode segments, relying instead on 250 ohms combined RDS(ON) resistance of the I/O pin FET drivers inside of the PIC (about 150 ohms for a high-side 'sourcing' FET driver and about 100 ohms for a low-side 'sinking' FET driver).

    While the circuit supports any color display, please note that the red, green, and yellow displays have a VF of 2.1 volts, while the white display is 3.1 volts, and the blue display is 3.4 volts. This means that while the blue and white displays are bright and easily visible in a brightly lighted room, the red, green, and yellow displays are a bit brighter. I hope to add a software PWM brightness control to a future version of the program.

    Parts List
    Code ( (Unknown Language)):
    2. 1 ea. PIC16F1828-I/P (DIP package)
    3. 1 ea. 0.1-uf (100nf) ceramic capacitor
    4. 1 ea. 10 kOhm, 1/8th watt carbon film resistor
    5. 4 ea. 1N914 or 1N4148 silicon switching diode
    6. 1 ea. [URL=""]Sparkfun COM-09481[/URL] Blue Common Anode 4-Digit Display
    7. 4 ea. generic momentary contact switch
    8. 1 ea. [URL=""]Soberton GT111P Piezo Speaker[/URL]
    9. Misc. sockets, connectors, prototype circuit board

    The attached HEX file can be used directly with a PICKIT2 or PICKIT3 to program the PIC16F1828 device. The program was written using the free/lite version of BoostC from Sourceboost. While you can use the Sourceboost IDE (Integrated Development Environment), I chose the option to install BoostC into the MPLAB IDE. The program uses packed BCD "minutes" and "seconds" variables and the code to increment and decrement those variables may look a little strange to some programmers.


    The <SET> switch is used to toggle between "set" and "run" modes. Press <SET> to enter "set" mode and the "minutes" display group will flash at a 2-Hz rate. While in "set" mode the <Rt> arrow key is used to toggle between the "minutes" and the "seconds" display group and the <Up> and <Dn> arrow switches are used to increment or decrement the value of the current flashing display group. The "minutes" display group will rollover from 99 to 0 or from 0 to 99 and the "seconds" display group will rollover from 59 to 0 or from 0 to 59. The <Up> and <Dn> arrow switches will repeat when held down. Press <SET> again to exit "set" mode and enter "run" mode (display stops flashing). In "run" mode you press the <Rt> arrow switch to start or stop the Timer. An active high signal is available on the RA2 pin when the Timer is running. While the Timer is running and counting down, the <SET> switch, the <Up> switch, and the <Dn> switch are disabled. Those switches are still sampled and debounced in the interrupt service routine and you'll still hear a "new press" beep when you press one of them but they're ignored and cleared by the logic in the program. If you have any questions, please ask...
    Last edited: Jun 11, 2012
    Kim Alfred and RRITESH KAKKAR like this.
  2. Wendy

    Wendy Moderator

    Mar 24, 2008
    What replaces the crystal oscillator? If you add the crystal and 2 capacitors, do they take over the timing function? If they do, is it more accurate?

    I may take the time to make a PCB for this design. If I do I will publish it here. I will get with you off line.
  3. MMcLaren

    MMcLaren Thread Starter Well-Known Member

    Feb 14, 2010
    This project uses the factory calibrated internal oscillator running at 8 MHz. It's rated at 1% accuracy so it's probably more accurate than what you might expect from an ordinary 555 RC oscillator but you could still be off by plus or minus one minute at the end of 99 minutes.

    Adding the crystal and capacitors and modifying the program will allow using the crystal as the time base for the timer function. A 32 kHz watch crystal is typically rated at 30ppm and so accuracy should improve dramatically. I'll be testing the 32.768 kHz crystal option soon and I can probably provide a second version of the 99 Minute Timer program that supports the crystal time base in the future, if requested.

    I'd love to see what you come up with, Bill. I also think this project board would be a good candidate for the Seeed or Itead PCB service. Basically, both of these Chinese companies offer ten 50x50 mm (1.97"x1.97") boards with plated through holes, solder mask, and silkscreen for $13 (including shipping). I can make this design fit into that size footprint but I don't have the funds to take advantage of the service. However, if someone is interested in ordering boards from Seed or Itead, I would be happy to layout the board and provide the Gerber files for it (maybe that person will send me one of their ten boards in return for the favor, grin).

    Cheerful regards, Mike, K8LH
    Last edited: May 3, 2012
  4. SgtWookie

    SgtWookie Expert

    Jul 17, 2007
    That would be great - but ... a PCB would be of somewhat limited usefulness unless any individual using it would want to come up with their own gamma correction PWM scheme, unless you would also care to publish yours.

    As it stands now, without such a scheme, there is no current limiting on the 7-segment LED display, so both the PIC and the display would be operating at far higher current than their ratings. I suggest that it would be quite challenging for a beginning programmer to come up with a suitable gamma corrected pwm scheme in time to prevent the display or PIC I/O pins from burning out.

    It might be a good idea to leave optional provisions for resistors on the board.
  5. Wendy

    Wendy Moderator

    Mar 24, 2008
    Something I learned in the last couple of years, with CMOS operating at low voltage (which 5V qualifies) you don't need current limiting, the CMOS transistors do it just fine by themselves. The low voltage on the power supply is critical though, but this works well on CMOS 555's and 4017s. The clock project I am working on for the next submission also uses this.

    The key is the power supply voltage though, it must be low.

    Goes to my tag line, it is good enough.
  6. MMcLaren

    MMcLaren Thread Starter Well-Known Member

    Feb 14, 2010
    Hi Sarge,

    I understand your concerns but they may not be relevant. I'm using the RDS(ON) resistance of the PIC I/O pin FET drivers for current limiting. If you get a chance, could you test the circuit? I could write a version of the program for the 16F690, if that would help. Anyway, I'd be delighted if you could show me how to get more than the 30-mA "peak" current spec for those RED displays using "direct drive" from a pair of PIC I/O pins which have a combined RDS(ON) resistance of approximately 250-ohms (VDD = 5 vdc).

    As for adding software brightness control... I have it working on an example (assembly language) Clock/Calendar/Alarm/Timer program but it will be a while before I can add that brightness control code to the 99 Minute Timer (C) program. I suspect there are many people who will just duplicate the project but for those few that want to study the code, I'm taking extra time to convert my assembly language code to C because I feel C code may be easier to understand.

    Cheerful regards, Mike
    Last edited: May 6, 2012
  7. panic mode

    panic mode Well-Known Member

    Oct 10, 2011
    is that what you count on for buzzer too? listed part number is rated for only 1.4..2V
  8. Wendy

    Wendy Moderator

    Mar 24, 2008
    No, the buzzer is piezo device. There are many devices like these that can be overvoltaged safely, as long as some common sense is use. It is fundamentally a high impedance device, and draws almost no current.
  9. MMcLaren

    MMcLaren Thread Starter Well-Known Member

    Feb 14, 2010
    FYI: I just added a 32.768 kHz watch crystal (and capacitors) to the circuit and initial tests indicate it's working great. This is the first time I've ever used a 32.768 kHz crystal and the low power Timer 1 oscillator module.


    I don't have a new 99-Minute Timer program that uses the crystal time-base yet but here's my first "test" program that I used to verify that (1) I could initialize TMR1OSC correctly, (2) poll the TMR1IF flag correctly, and (3) preload TMR1 registers correctly for precise one second or half second overflows;

    Code ( (Unknown Language)):
    1. ;******************************************************************
    2. ;                                                                 *
    3. ;       File: 16F1828 32k v1.asm                                  *
    4. ;     Author: Mike McLaren, K8LH                                  *
    5. ;    (C)2012: Micro Application Consultants                       *
    6. ;           : All Rights Reserved                                 *
    7. ;       Date: 22-Oct-2012                                         *
    8. ;                                                                 *
    9. ;   First Attempt Running Low Power 32.768-kHz TMR1 Oscillator    *
    10. ;                                                                 *
    11. ;                                                                 *
    12. ;      MPLab: 8.84    (tabs=8)                                    *
    13. ;      MPAsm: 5.44                                                *
    14. ;                                                                 *
    15. ;******************************************************************
    17.         #include <P16F1828.INC>
    18.         errorlevel -302
    19.         list st=off
    24.         radix dec
    26. ;---< variables >--------------------------------------------------
    28.         cblock  0x20
    29.         endc
    31. ;---< constants >--------------------------------------------------
    34. ;******************************************************************
    35. ;  reset vector                                                   *
    36. ;******************************************************************
    37.         org     0x000
    38. v_res
    39.         clrf    STATUS          ;                                 |B0
    40.         bra     init            ;                                 |B0
    42. ;******************************************************************
    43. ;  interrupt vector                                               *
    44. ;******************************************************************
    45.         org     0x004
    46. v_int
    48. ;******************************************************************
    49. ;  main init                                                      *
    50. ;******************************************************************
    51. ;
    52. ;  setup TMR1 for operation as low power 32.768-kHz oscillator
    53. ;
    54. init
    55.         banksel T1CON           ; bank 0                          |B0
    56.         movlw   b'10001101'     ; 10------ TMR1CS source TMR1OSC
    57.                                 ; --00---- T1CKPS prescale 1
    58.                                 ; ----1--- T1OSCEN TMR1OSC 'on'
    59.                                 ; -----1-- T1SYNC asynchronous
    60.                                 ; ------0- unused
    61.                                 ; -------1 TMR1ON timer 1 'on'
    62.         movwf   T1CON           ;                                 |B0
    63. ;
    64. ;  setup INTOSC for Fosc = 8-MHz operation (Tcy = ~500 nS)
    65. ;
    66.         banksel OSCCON          ; bank 1                          |B1
    67.         movlw   b'01110000'     ;                                 |B1
    68.         movwf   OSCCON          ; INTOSC = 8-MHz                  |B1
    69. stable  btfss   OSCSTAT,HFIOFS  ; osc stable? yes, skip, else     |B1
    70.         bra     stable          ;                                 |B1
    71. ;
    72. ;  configure I/O ports
    73. ;
    74.         banksel ANSELA          ; bank 3                          |B3
    75.         clrf    ANSELA          ; ADC off, digital I/O            |B3
    76.         clrf    ANSELB          ;   "                             |B3
    77.         clrf    ANSELC          ;   "                             |B3
    78.         banksel LATA            ; bank 2                          |B2
    79.         clrf    LATA            ;                                 |B2
    80.         clrf    LATB            ; anode digit enable pins 'off'   |B2
    81.         movlw   0xFF            ; active lo segment lines 'off'   |B2
    82.         movwf   LATC            ; cathode segments 'off'          |B2
    83.         banksel TRISA           ; bank 1                          |B1
    84.         clrf    TRISA           ;                                 |B1
    85.         clrf    TRISB           ;                                 |B1
    86.         clrf    TRISC           ;                                 |B1
    87.         banksel 0               ; bank 0                          |B0
    89. ;******************************************************************
    90. ;  main loop                                                      *
    91. ;******************************************************************
    93. loop
    94.         btfss   PIR1,TMR1IF     ; TMR1 rollover? yes, skip, else  |B0
    95.         goto    loop            ; loop (wait for rollover)        |B0
    96.         bcf     PIR1,TMR1IF     ; reset TMR1 interrupt flag and   |B0
    97.         bsf     TMR1H,7         ; set b15 for 1.0 second period   |B0
    98.         bsf     TMR1H,6         ; set b14 for 0.5 second period   |B0
    99. ;
    100. ;  flash segment A on left most digit at one second intervals
    101. ;
    102.         bsf     PORTB,4         ; digit 1 anode hi (enabled)      |B0
    103.         movlw   1<<RC0          ; mask for segment A cathode      |B0
    104.         xorwf   PORTC,F         ; toggle digit 1 segment A        |B0
    105.         bra     loop            ; loop forever                    |B0
    107. ;******************************************************************
    108.         end
    So, anyone interested in a 4-digit Alarm Clock program? Or, would anyone like an updated 99-minute timer program that uses the crystal timebase?

    Cheerful regards, Mike
    Last edited: Nov 7, 2012
  10. SPQR

    SPQR Member

    Nov 4, 2011
    This newbie would love to do it!
    I really like projects like this where I get something useful, have fun making it, and learn about electronics.
    (e.g., I just learned that sometimes you don't need current limiting resistors in line with LED's if you don't supply too much voltage, and have other semiconductors in line - posts 5 and 6 - thanks!)

    The potential use for other projects is also VERY interesting.

    I'll add it to my long "to make" list.

    Thanks very much for the post.