Elapsed timer not working as expected at 64MHz


Closed Thread
Results 1 to 40 of 56

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Quote Originally Posted by longpole001 View Post
    each 100th is 10ms on the i/o debug section btw
    My output pin changes state every 2.5ms at 64MHz.

    Code:
    '***************************************************************************
    '*  Name    : Elapsed_INT-18.bas                                           *
    '*  Author  : Darrel Taylor                                                *
    '*  Date    : JUL 11, 2006 : 7/11/2010                                     *
    '*  Version : 1.2                                                          *
    '*  Notes   : Must have DT_INTS-18.bas loaded first                        *
    '*   ver 1.2: Now works at any OSC frequency without using the prescaler   *
    '***************************************************************************
    DISABLE DEBUG
    
    ; syntax =     Handler  IntSource,        Label, Type, ResetFlag?
    DEFINE  Elapsed_Handler  TMR1_INT,  _ClockCount,  asm,  yes
    ; the above define can be used in the INT_LIST macro, if desired (optional)
    
    EL_Ticks            VAR BYTE   ; Counts timer Overflows
    EL_100th            VAR BYTE
    ;T1Post           VAR BYTE   ; Timer1 postscaler
    EL_Seconds          VAR BYTE
    EL_Minutes          VAR BYTE
    EL_Hours            VAR BYTE
    EL_Days             VAR WORD
    
    EL_100thChanged     VAR BIT
    EL_SecondsChanged   VAR BIT    ; idicates that the value has changed
    EL_MinutesChanged   VAR BIT
    EL_HoursChanged     VAR BIT
    EL_DaysChanged      VAR BIT
    
    GOSUB ResetTime             ; initialize the Elapsed Timer
    
    Goto OverElapsed            ; skip over the routines
    
    ' -------------- calc timer reload Constants -------------------------------
    ASM
    T1PS = 1                             ; start with 1:1 postscaler
    TimerConst = ((OSC*1000000)/4/100)   ; how many timer ticks will it take
      while TimerConst > 65400           ; if it's more than the timer can count
    T1PS = T1PS * 2                      ;   double the postscaler
    TimerConst = TimerConst / 2          ;   halve the count
      endw
    TimerConst = 65536 - TimerConst + 8  ; final reload value
      
      
    ; -----------------  ADD TimerConst to TMR1H:TMR1L -------------------------
    ADD2_TIMER   macro
        BCF     T1CON,TMR1ON, 0       ;  1 Turn off timer
        MOVLW   LOW(TimerConst)       ;  1
        ADDWF   TMR1L,F, 0            ;  1    
        BTFSC   STATUS,C              ;  1/2
        INCF    TMR1H,F, 0            ;  1
        MOVLW   HIGH(TimerConst)      ;  1
        ADDWF   TMR1H,F, 0            ;  1
        endm
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1 ------
    RELOAD_TIMER  macro
        ADD2_TIMER
        BSF     T1CON,TMR1ON, 0       ;  1   Turn TIMER1 back on  (8 cycles)
        endm
    
    ; -----------------  Load TimerConst into TMR1H:TMR1L ----------------------
    LOAD_TIMER  macro
        MOVE?CT  0, T1CON,TMR1ON
        MOVE?CB  0, TMR1L
        MOVE?CB  0, TMR1H
        ADD2_TIMER
        endm
    ENDASM
    
    ' ------[ This is the Interrupt Handler ]-----------------------------------
    T1PS  CON EXT
    ClockCount:
    @ RELOAD_TIMER                   ; Reload TIMER1
    EL_Ticks = EL_Ticks  + 1    ; each tick is 2.5ms 
           IF LATD.3 = 0 THEN           ' debug 
              LATD.3 = 1
           ELSE 
              LATD.3 = 0
           ENDIF   
      if EL_ticks // T1PS = 0  THEN      ' if modulas of tips (4) = 0 
           IF LATD.3 = 0 THEN           ' debug 
              LATD.3 = 1
           ELSE 
              LATD.3 = 0
           ENDIF   
              EL_100th = EL_100th + 1 
              EL_100thChanged = 1
              IF EL_100TH  = 100 THEN
                  EL_100TH = 0
                  EL_Seconds = EL_Seconds + 1      
                  EL_SecondsChanged = 1
                  IF EL_Seconds = 60 THEN
                     EL_Seconds = 0
                     EL_Minutes = EL_Minutes + 1
                     EL_MinutesChanged = 1
                  eNDIF
                  IF EL_Minutes = 60 THEN
                     EL_Minutes = 0
                     EL_Hours = EL_Hours + 1
                     EL_HoursChanged = 1
                  ENDIF
                  IF EL_Hours = 24 THEN
                     EL_Hours = 0 
                     EL_Days = EL_Days + 1
                     EL_DaysChanged = 1
                  ENDIF
              eNDIF
          endif
    '    Ticks = Ticks + 1
    '    IF Ticks = 100 THEN
    '       Ticks = 0
    '       T1Post = T1Post + 1
    '       IF T1Post = T1PS THEN
    '           T1Post = 0
    '           Seconds = Seconds + 1
    '           SecondsChanged = 1
    '           IF Seconds = 60 THEN
    '              Seconds = 0
    '              Minutes = Minutes + 1
    '              MinutesChanged = 1
    '           ENDIF
    '           IF Minutes = 60 THEN
    '              Minutes = 0
    '              Hours = Hours + 1
    '              HoursChanged = 1
    '           ENDIF
    '           IF Hours = 24 THEN
    '              Days = Days + 1
    '              DaysChanged = 1
    '              Hours = 0
    '           ENDIF
    '       ENDIF
    '    ENDIF
    @ INT_RETURN                     ; Restore context and return from interrupt
    
    '-----====[ END OF TMR1 Interrupt Handler ]====-----------------------------
    
    StartTimer:
        T1CON = 1                    ; 1:1, FOSC4, TMR1ON
    RETURN
    
    ; --------------------------------------------------------------------------
    StopTimer:
        T1CON.0 = 0                  ; Turn OFF Timer1
    RETURN
    
    ; --------------------------------------------------------------------------
    BitSave  VAR  BIT
    
    ResetTime:
        BitSave = T1CON.0            ; Save TMR1ON bit
    @   LOAD_TIMER                   ; Load TimerConst
        T1CON.0 = BitSave            ; Restore TMR1ON bit
    ;    T1Post = 0                   ; clear the postscaler
        EL_Ticks = 0
        EL_100th = 0
        EL_Seconds = 0
        EL_Minutes = 0
        EL_Hours = 0
        EL_Days = 0
        EL_100thChanged = 1
        EL_SecondsChanged = 1           ; indicate everything has changed
        EL_MinutesChanged = 1           ; so that 00:00:00 is processed
        EL_HoursChanged = 1
        EL_DaysChanged = 1
    RETURN
    
    
    OverElapsed:
    ENABLE DEBUG
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Yes, I'm an idiot.

    T1CON becomes 00000001 once it is STARTED! D'OH!
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Sheldon,

    It's not perfect, I'm missing some pulses on the Saleae probe, but it's sticking to a 2.5ms tick as you describe in your Elapsed include comment.

    Name:  Saleae 64MHz 2-5ms.PNG
Views: 1552
Size:  27.7 KB

    EDIT: I just noticed that the skipped pulse happens at every 4 ticks.

    And that skipped pulse is between 20-21uS.


    EDIT SOME MORE: Got it, it's that 2nd LED toggle. I commented it out and now I have a regular 2.5ms pulse.

    Robert
    Last edited by Demon; - 23rd December 2014 at 05:43.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

Similar Threads

  1. I2CRead & I2CWrite not working as expected
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 30
    Last Post: - 27th October 2021, 19:36
  2. PORTB.3 Input not working as expected.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th March 2013, 10:58
  3. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 18:39
  4. SPWM and Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 8th May 2008, 04:16
  5. DT Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th March 2008, 00:17

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