Simple PIC based timer needed, any advice appreciated.


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2010
    Posts
    4

    Question Simple PIC based timer needed, any advice appreciated.

    Im new to this forum so hello everyone! Sorry if this is posted in the wrong place.

    I need a simple timer that will pulse for 2 seconds once in every (approx) 24 hours. Physical size must be small as it is to be fitted into existing equipment. Hence why I thought of a PIC solution. 5 volts available and it would need to trigger a miniture relay. Permanently connected to power so would need to reset and then wait a further 24 hours and send a pin high for 2 seconds again.

    I know its a simple request but Ive had no luck in finding anything on the web, so if anyone could point me in the right direction that would be very much appreciated!

    Chris

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    You could use instant interrupts elapsed timer example. Then look for the flag daychange. Check out this example for 24 hour timer: http://darreltaylor.com/DT_INTS-18/elapsed.html Keep an eye on DaysChange, and you should be able to make it happen.
    http://www.scalerobotics.com

  3. #3
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Hi Chris,

    Every 24 hours starting when? Starting with power-up?

    How precise for the 24-hour interval? Is ±10 secs / day from internal oscillator ok? Or, do you need crystal precision?

    Regards, Mike

  4. #4
    Join Date
    Aug 2010
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Timer

    Thanks for replies.
    I need this to force a hard reset on some telematics equipment. The devices do have built-in timers that can be used for a variety of events, including a system reset. However in practice they fail after a couple of months. This is why we need to have a completely independent timer that we can rely on to do its job. Like I say it would be permanently connected to 5 volts, so would need to start timing on power-up. Also after its 2 second 'on' period, restart again. The 24 hours isnt critical, just an ideal figure, once a day. If this fluctuated and became 22 hours or 26 hours, or a mixture it wouldnt ever be a problem. Neither is the 'on' time 2-5 seconds is fine.
    Thanks again.
    Chris

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quick and dirty
    Code:
        CNT_1 VAR BYTE
        CNT VAR WORD
        HIGH PORTC.4
        PAUSE 2000
        low PORTC.4
        PAUSE 2000
    START:
        FOR CNT_1 = 0 TO 24
        FOR CNT = 0 TO 600
        HIGH PORTC.4
        PAUSE 6000
        NEXT CNT
        NEXT CNT_1
        LOW PORTC.4 
        PAUSE 5000
        GOTO START
    Dave
    Always wear safety glasses while programming.

  6. #6


    Did you find this post helpful? Yes | No

    Default 24 hr tmr

    the devices with the 8 mhz int osc are about 1%, (12f283 for example) pretty good timing.
    i would blink an led at 1 sec and use the 1 second as a time base. the led just shows that your ckt is doing something.
    there are 86400 seconds/day but a var word is only 16 bit - max 65535 count so use 2 second count inc up to 43200.

    start:
    while count < 43200
    high led
    pause 1000
    low led
    pause 1000
    count=count+1
    end while
    outputRSTpin=1
    pause 2000
    outputRSTpin=0
    count=0
    goto start

    don f
    Last edited by amgen; - 15th August 2010 at 20:32.

  7. #7
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Chris,

    Would you like to try it on a 55 cent 6-pin 10F200? I just simulated a 63 word program with "cycle accurate" 23:59:58 "off" time and 00:00:02 "on" time. Accuracy will be as good as you can get with the internal oscillator.

    Regards, Mike
    Last edited by Mike, K8LH; - 16th August 2010 at 01:56.

  8. #8
    Join Date
    Aug 2010
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Mike, that looks interesting. I would like to try that. Using this type of chip how will it perform months down the line. eg in 7 months will I still be getting a 2 seconds 'on' once in 24 hours? What I suppose Im trying to ask is, will it gradually lose time or make time or fluctuate +/- so keepinmg an average of 24 hours?

    thanks again

  9. #9
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Hi Chris,

    The example I wrote will maintain a 24 hour cycle as long as it's powered up but the internal oscillator only has a 1% tolerance at room temperature so it will drift. Over a period of several months you might be an hour off from where you started but you should still be getting an interval very close to 24 hours. If that's a problem then you probably should use an 8-pin PIC (12F629, 12F617, 12F635, 12F675, 12F683) and a crystal which should get you accuracy to within a minute or so per year.

    Cheerful regards, Mike

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Mike,
    Do you plan on posting the code so Chris can use it or just continue talking about it?
    Dave
    Always wear safety glasses while programming.

  11. #11
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,

    I sent it to him via PM. Should I post it here for others to see too? It's assembler (yuch!) and it's an isochronous loop, which will look strange even to the assembly language programmers amoung us (lol).

    Regards, Mike

    <added>

    The program outputs a '1' on GP0 for 2 seconds then outputs a '0' for 23 hours 59 minutes and 58 seconds. The GP1 output is toggled at 500-msec intervals for an optional LED "running" indicator. You can verify "cycle accurate" intervals with MPLAB "stopwatch" by placing the simulator breakpoint at the "goto cycle" instruction at the end of the loop.

    Code:
    ;******************************************************************
    ;*                                                                *
    ;*                                                                *
    ;*     MPLAB: 8.50    (tabs=8)                                    *
    ;*     MPASM: 5.35                                                *
    ;*                                                                *
    ;******************************************************************
    
            radix   dec
    
            include "p10f200.inc"
            __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF
    
    ;--< variables >---------------------------------------------------
    
    delaylo equ     0x10            ; DelayCy() subsystem
    delayhi equ     0x11            ; DelayCy() subsystem
    hours   equ     0x12            ; Timer sub
    minutes equ     0x13            ; Timer sub
    seconds equ     0x14            ; Timer sub
    
    ;--< defines >-----------------------------------------------------
    
    
    ;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ;  K8LH DelayCy() subsystem macro generates five instructions     '
    ;                                                                 '
    clock   equ     4               ; 4, 8, 12, 16, or 20 (MHz)
    usecs   equ     clock/4         ; cycles/microsecond multiplier
    msecs   equ     clock/4*1000    ; cycles/millisecond multiplier
    
    DelayCy macro   delay           ; 12..524298 cycle range
            movlw   high((delay-12)/8)^255
            movwf   delayhi
            movlw   low ((delay-12)/8)^255
            movwf   delaylo
            call    uDelay-((delay-12)%8)
            endm
    
    ;******************************************************************
    ;  main.startup                                                   *
    ;******************************************************************
            org     0x000
    
    Startup
            movwf   OSCCAL          ; factory INTOSC calibration
            movlw   b'00001000'     ;
            tris    GPIO            ; GP3 input, all others output
            clrf    GPIO            ; clear output latches
            movlw   b'10011110'     ; 10011110
                                    ; 1-------, IOC off
                                    ; -0------, weak pullups on
                                    ; --0-----, T0CS source Fosc/4
                                    ; ---1----, T0SE edge hi>lo
                                    ; ----1---, PSA prescale WDT
                                    ; -----110, PS prescaler 64
            option                  ; set OPTION reg'
    ;******************************************************************
    ;  main.loop                                                      *
    ;******************************************************************
    ;  alternate 00:00:02 GP0 = 1 (on) and 23:59:58 GP0 = 0 (off)
    ;
    cycle
            DelayCy(500*msecs)      ;
            DelayCy(500*msecs-16)   ;
            movlw   23              ; 23 hours
            movwf   hours           ;
            movlw   59              ; 59 minutes
            movwf   minutes         ;
            movlw   57              ; 58 seconds
            btfsc   GPIO,0          ; GP0 off? yes, skip, else
            movlw   1               ; 2 seconds
            movwf   seconds         ;
            btfsc   GPIO,0          ; GP0 off? yes, skip, else
            clrf    minutes         ;
            btfsc   GPIO,0          ; GP0 off? yes, skip, else
            clrf    hours           ;
    timer   DelayCy(500*msecs-19)   ; 1 sec minus 22 cycle loop time
            movf    GPIO,W          ;
            xorlw   2               ;
            movwf   GPIO            ; toggle LED on GP1 pin
            DelayCy(500*msecs-3)    ;
            movf    GPIO,W          ;
            xorlw   2               ;
            movwf   GPIO            ; toggle LED on GP1 pin
            movlw   59              ; W = 59 (mins/secs reset value)
            decf    seconds,F       ; decrement seconds
            btfsc   seconds,7       ; negative? no, skip, else
            decf    minutes,F       ; decrement minutes
            btfsc   seconds,7       ; negative? no, skip, else
            movwf   seconds         ; reset seconds = 59
            btfsc   minutes,7       ; negative? no, skip, else
            decf    hours,F         ; decrement hours
            btfsc   minutes,7       ; negative? no, skip, else
            movwf   minutes         ; reset minutes = 59
            movf    seconds,W       ;
            iorwf   minutes,W       ;
            iorwf   hours,W         ; timer timed-out?
            skpz                    ; yes, skip, else
            goto    timer           ; branch
            movf    GPIO,W          ;
            xorlw   1               ;
            movwf   GPIO            ; toggle GP0 output
            goto    cycle           ; loop (new cycle)
    
    ;******************************************************************
    ;  K8LH DelayCy() subsystem 16-bit "uDelay" timing subroutine     *
    ;                                                                 *
            nop                     ; (delay-12)%8 == 7 entry point
            nop                     ; (delay-12)%8 == 6 entry point
            nop                     ; (delay-12)%8 == 5 entry point
            nop                     ; (delay-12)%8 == 4 entry point
            nop                     ; (delay-12)%8 == 3 entry point
            nop                     ; (delay-12)%8 == 2 entry point
            nop                     ; (delay-12)%8 == 1 entry point
    uDelay  incf    delaylo,F       ; subtract one 8-cycle loop
            skpnz                   ; borrow? no, skip, else
            incfsz  delayhi,F       ; done?  yes, skip, else
            goto    uDelay-3        ; do another 8-cycle loop
            retlw   0               ;
    ;                                                                 *
    ;******************************************************************
            end
    Last edited by Mike, K8LH; - 16th August 2010 at 14:15.

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I think you should post it for others that are following along. How many times have you looked for a solution to a problem and when you think you found it the solution was never posted??

    Kinda a pet peve of mine.

    But it is up to you.
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Aug 2010
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Mike, thanks for that. I will hunt down and dust off my programmer and give this a whirl. Might just do the trick. Keep you posted and thanks again.
    Chris

  14. #14
    Dagwood's Avatar
    Dagwood Guest


    Did you find this post helpful? Yes | No

    Default Re: Simple PIC based timer needed, any advice appreciated.

    Hi All,Very new to Pic Programming and very new to your forum. I found this thread searching Google for ideas on progrmming a PIC on a 24hour cycle. Ok the problem, I have a JDM programmer and when I try to write to a 10F200 PIC all I get is, [IMG]file:///C:/DOCUME~1/BENSHE~1/LOCALS~1/Temp/msoclip1/01/clip_image001.png[/IMG]I have tried running it from the ZIF sockets and the off board connectors????Am stuck now??? Any ideasCheersDagwood

  15. #15
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default Re: Simple PIC based timer needed, any advice appreciated.

    Moved from Schematics.

    Robert

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts