Project: PIC 4-Digit 99 Minute Timer

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

  1. MMcLaren

    MMcLaren Thread Starter Active Member

    Joined:
    Feb 14, 2010
    Messages:
    655
    Location:
    Michigan, USA
    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.

    [​IMG]

    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

    Requirements

    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.)

    [​IMG]

    Hardware

    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:
    1 ea. PIC16F1828-I/P (DIP package)
    1 ea. 0.1-uf (100nf) ceramic capacitor
    1 ea. 10 kOhm, 1/8th watt carbon film resistor
    4 ea. 1N914 or 1N4148 silicon switching diode
    1 ea. Sparkfun COM-09481 Blue Common Anode 4-Digit Display
    4 ea. generic momentary contact switch
    1 ea. Soberton GT111P Piezo Speaker
    Misc. sockets, connectors, prototype circuit board
    
    Software

    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.

    Operation

    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...

    Attached Files:

    Last edited: Jun 11, 2012
    RRITESH KAKKAR likes this.
  2. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,237
    Location:
    Dallas, TX (GMT-5 w/ DST)
    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 Active Member

    Joined:
    Feb 14, 2010
    Messages:
    655
    Location:
    Michigan, USA
    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

    Joined:
    Jul 17, 2007
    Messages:
    22,093
    Location:
    In the vast midwest of the USA; CST
    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. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,237
    Location:
    Dallas, TX (GMT-5 w/ DST)
    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 Active Member

    Joined:
    Feb 14, 2010
    Messages:
    655
    Location:
    Michigan, USA
    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

    Joined:
    Oct 10, 2011
    Messages:
    1,103
    Location:
    Mississauga, Ontario
    is that what you count on for buzzer too? listed part number is rated for only 1.4..2V
  8. Bill_Marsden

    Bill_Marsden Moderator Staff Member

    Joined:
    Mar 24, 2008
    Messages:
    19,237
    Location:
    Dallas, TX (GMT-5 w/ DST)
    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 Active Member

    Joined:
    Feb 14, 2010
    Messages:
    655
    Location:
    Michigan, USA
    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.

    [​IMG]

    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:
    ;******************************************************************
    ;                                                                 *
    ;       File: 16F1828 32k v1.asm                                  *
    ;     Author: Mike McLaren, K8LH                                  *
    ;    (C)2012: Micro Application Consultants                       *
    ;           : All Rights Reserved                                 *
    ;       Date: 22-Oct-2012                                         *
    ;                                                                 *
    ;   First Attempt Running Low Power 32.768-kHz TMR1 Oscillator    *
    ;                                                                 *
    ;                                                                 *
    ;      MPLab: 8.84    (tabs=8)                                    *
    ;      MPAsm: 5.44                                                *
    ;                                                                 *
    ;******************************************************************
    
            #include <P16F1828.INC>
            errorlevel -302
            list st=off
    
      __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF
      __CONFIG _CONFIG2, _PLLEN_OFF & _LVP_OFF
    
            radix dec
    
    ;---< variables >--------------------------------------------------
    
            cblock  0x20
            endc
    
    ;---< constants >--------------------------------------------------
    
    
    ;******************************************************************
    ;  reset vector                                                   *
    ;******************************************************************
            org     0x000
    v_res
            clrf    STATUS          ;                                 |B0
            bra     init            ;                                 |B0
    
    ;******************************************************************
    ;  interrupt vector                                               *
    ;******************************************************************
            org     0x004
    v_int
    
    ;******************************************************************
    ;  main init                                                      *
    ;******************************************************************
    ;
    ;  setup TMR1 for operation as low power 32.768-kHz oscillator
    ;
    init
            banksel T1CON           ; bank 0                          |B0
            movlw   b'10001101'     ; 10------ TMR1CS source TMR1OSC
                                    ; --00---- T1CKPS prescale 1
                                    ; ----1--- T1OSCEN TMR1OSC 'on'
                                    ; -----1-- T1SYNC asynchronous
                                    ; ------0- unused
                                    ; -------1 TMR1ON timer 1 'on'
            movwf   T1CON           ;                                 |B0
    ;
    ;  setup INTOSC for Fosc = 8-MHz operation (Tcy = ~500 nS)
    ;
            banksel OSCCON          ; bank 1                          |B1
            movlw   b'01110000'     ;                                 |B1
            movwf   OSCCON          ; INTOSC = 8-MHz                  |B1
    stable  btfss   OSCSTAT,HFIOFS  ; osc stable? yes, skip, else     |B1
            bra     stable          ;                                 |B1
    ;
    ;  configure I/O ports
    ;
            banksel ANSELA          ; bank 3                          |B3
            clrf    ANSELA          ; ADC off, digital I/O            |B3
            clrf    ANSELB          ;   "                             |B3
            clrf    ANSELC          ;   "                             |B3
            banksel LATA            ; bank 2                          |B2
            clrf    LATA            ;                                 |B2
            clrf    LATB            ; anode digit enable pins 'off'   |B2
            movlw   0xFF            ; active lo segment lines 'off'   |B2
            movwf   LATC            ; cathode segments 'off'          |B2
            banksel TRISA           ; bank 1                          |B1
            clrf    TRISA           ;                                 |B1
            clrf    TRISB           ;                                 |B1
            clrf    TRISC           ;                                 |B1
            banksel 0               ; bank 0                          |B0
    
    ;******************************************************************
    ;  main loop                                                      *
    ;******************************************************************
    
    loop
            btfss   PIR1,TMR1IF     ; TMR1 rollover? yes, skip, else  |B0
            goto    loop            ; loop (wait for rollover)        |B0
            bcf     PIR1,TMR1IF     ; reset TMR1 interrupt flag and   |B0
            bsf     TMR1H,7         ; set b15 for 1.0 second period   |B0
            bsf     TMR1H,6         ; set b14 for 0.5 second period   |B0
    ;
    ;  flash segment A on left most digit at one second intervals
    ;
            bsf     PORTB,4         ; digit 1 anode hi (enabled)      |B0
            movlw   1<<RC0          ; mask for segment A cathode      |B0
            xorwf   PORTC,F         ; toggle digit 1 segment A        |B0
            bra     loop            ; loop forever                    |B0
    
    ;******************************************************************
            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

    Attached Files:

    Last edited: Nov 7, 2012
  10. SPQR

    SPQR Member

    Joined:
    Nov 4, 2011
    Messages:
    379
    Location:
    Los Angeles
    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.
Similar Threads
Forum Title Date
The Completed Projects Collection Project: PIC 4-Digit Single-Chip 24 Hour Clock Dec 5, 2012
The Completed Projects Collection Project: PIC 4-Digit DS18B20 Temperature Monitor May 6, 2012
The Completed Projects Collection Project : Battery Buckets Friday at 2:01 AM
The Completed Projects Collection Project : Electronic Load Sep 16, 2014
The Completed Projects Collection Project: TA1000PS - PSU for audio amplifiers with protection, sofstart and fan contro Jul 20, 2014

Share This Page