Elapsed timer not working as expected at 64MHz


Closed Thread
Results 1 to 40 of 56

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

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

    now I'm confused
    Well that toggle from Sheldon confirms DT's is not working perfectly at 64MHz.
    or
    Well that toggle from Sheldon confirms DT's is working perfectly at 64MHz.

    ps
    IF LATD.3 = 0 THEN ' debug
    LATD.3 = 1
    ELSE
    LATD.3 = 0
    ENDIF
    can be replaced with

    latd.3=!latd.3

    lets not have too much unnecessary overhead in the interrupt
    Last edited by richard; - 23rd December 2014 at 05:20.

  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

    Quote Originally Posted by richard View Post
    now I'm confused

    or
    Well that toggle from Sheldon confirms DT's is working perfectly at 64MHz.
    DT's Elapsed include doesn't work perfectly at 64MHz (1 tick = 2.5ms), Sheldon's modified version does (1/100th = 10ms). Sheldon has added a new variable. I'm cleaning up the new include now to put it on the forum, somewhere.

    Robert
    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
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

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

    you may like to re-examine that I just tried it on a pic1845k20 @64 MHz and the int is @2.5mS as expected , sheldons mod just adds 100th capability doesn't it ?

    the seconds inc on every 400th interrupt the ticks are not meant to be 10mS
    Last edited by richard; - 23rd December 2014 at 05:57. Reason: better explanition

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

    From http://web.archive.org/web/201206151...8/elapsed.html

    The time is kept in the variables:
    --------------------------------------------------------------------------------

    Ticks var byte ' 1/100th of a second
    Seconds var byte ' 0-59
    Minutes var byte ' 0-59
    Hours var byte ' 0-23
    Days var word ' 0-65535
    But a tick is not 2.5ms.

    Robert

  5. #5
    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 richard View Post
    you may like to re-examine that I just tried it on a pic1845k20 @64 MHz and the int is 2.5mS as expected , sheldons mod just adds 100th doesn't it

    the ticks only inc on every 4th interrupt

    What happens when you start/stop the timer after 5 ticks using the original include?

    I get this using the new include:
    Name:  Saleae 64MHz 5 ticks.PNG
Views: 1329
Size:  22.6 KB

    Code:
    MainProgram:
    
        if T1CON.0 = 0 then
            PortD.3 = 1
            GOSUB StartTimer
        endif
    
        if OneHundreth = 5 then
            PortD.3 = 0
            GOSUB StopTimer                    ' Stop the Elapsed Timer
            GOSUB ResetTime                     ' Reset Time to  0d-00:00:00.00
        endif
    
        goto MainProgram
    Robert

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

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

    its a clock not a stopwatch , if you really need to measure wee small times like that there are better ways.
    to get the fractional part of the seconds multiply t1post with ticks and / 4

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

    New version of Darrel Taylor's Elapsed include for 64MHz:

    Code:
    '***************************************************************************
    '*  Name    : Elapsed_INT-18.bas                                           *
    '*  Author  : Darrel Taylor                                                *
    '*  Date    : JUL 11, 2006 : 7/11/2010 : Dec 23, 2014                      *
    '*  Version : 1.3                                                          *
    '*  Notes   : Must have DT_INTS-18.bas loaded first                        *
    '*   ver 1.2: Now works at any OSC frequency without using the prescaler   *
    '*   ver 1.3: will work with 64MHz thanks to Sheldon                       *
    '*            New field ONEHUNDRETH is now used instead of TICKS           *
    '***************************************************************************
    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)
    
    Ticks            VAR BYTE   ; Counts timer Overflows
    OneHundreth      VAR BYTE    ; New field for 64MHz
    ;T1Post           VAR BYTE   ; Timer1 postscaler
    Seconds          VAR BYTE
    Minutes          VAR BYTE
    Hours            VAR BYTE
    Days             VAR WORD
    
    OneHundrethChanged     VAR BIT    ; New field for 64MHz
    SecondsChanged   VAR BIT    ; idicates that the value has changed
    MinutesChanged   VAR BIT
    HoursChanged     VAR BIT
    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
    Ticks = Ticks  + 1                      ; each tick is 2.5ms 
    '       IF LATD.3 = 0 THEN                  ' used to debug to confirm 2.5ms
    '          LATD.3 = 1
    '       ELSE 
    '          LATD.3 = 0
    '       ENDIF   
      if ticks // T1PS = 0  THEN      ' if modulas of tips (4) = 0 
    '       IF LATD.3 = 0 THEN                  ' used to debug to confirm 10ms
    '          LATD.3 = 1
    '       ELSE 
    '          LATD.3 = 0
    '       ENDIF   
              OneHundreth = OneHundreth + 1   ; each 1/100th is 10ms
              OneHundrethChanged = 1
              IF OneHundreth  = 100 THEN
                  OneHundreth = 0
    '    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
        Ticks = 0
        OneHundreth = 0
        Seconds = 0
        Minutes = 0
        Hours = 0
        Days = 0
        OneHundrethChanged = 1
        SecondsChanged = 1           ; indicate everything has changed
        MinutesChanged = 1           ; so that 00:00:00 is processed
        HoursChanged = 1
        DaysChanged = 1
    RETURN
    
    
    OverElapsed:
    ENABLE DEBUG
    New field OneHundreth (10ms) replaces Tick (2.5ms at 64MHz).

    Name:  Saleae 64MHz new.PNG
Views: 1331
Size:  22.4 KB

    (Remove .TXT suffix)
    Elapsed_INT-18_64MHz.bas.txt

    Robert
    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, 18:36
  2. PORTB.3 Input not working as expected.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th March 2013, 09:58
  3. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  4. SPWM and Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 8th May 2008, 03:16
  5. DT Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th March 2008, 23: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