Simple PIC based timer needed, any advice appreciated.


Closed Thread
Results 1 to 15 of 15

Hybrid View

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

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

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

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

Members who have read this thread : 0

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