Elapsed Timer findings


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 41
  1. #1
    Join Date
    Aug 2003
    Posts
    985

    Default Elapsed Timer findings

    Hi Guys,

    I finally figured out how the constant timer values are calculated for various oscillator speeds in DT’s Elapsed Timer.

    Code:
      IF OSC == 4                       ; Constants for 100hz interrupt from Timer1
    TimerConst = 0D8F7h                 ; Executed at compile time only
      EndIF
      If OSC == 8
    TimerConst = 0B1E7h
      EndIF
      If OSC == 10
    TimerConst = 09E5Fh
      EndIF
      If OSC == 20
    TimerConst = 03CB7h
      EndIF
    For the 20MHz example where the constant is 03B7h, it's the reverse of this:

    FFFFh - 03B7h = C348h (49992 decimal),
    49992 + 8 = 50000 (it must take eight instruction cycles to reload the timer)
    50000 / 10000 = 5
    5 x 4 = 20MHz

    I was looking for this:
    Code:
      If OSC == 16
    TimerConst = 063C7h
      EndIF

    Apologies if this info is already out there.
    Cheers, Art.
    Last edited by Art; - 30th March 2015 at 06:36.

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Oh dear! So ... simple...

    Ioannis

  3. #3
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Art,

    Wow, that seems complicated, glad it was you figuring it out.

    I took what you posted and looked at DT's Elapsed.bas.
    I believe the formula that Darrel used to calculate the Timer preload value can be calculated a little easier.

    First a couple of statements.
    1. One thing to point out first is that if OSC = 40Mhz he sets the Timer Prescale to 2.
    Otherwise the Prescale is set to 1.

    2. He is indeed allowing for 8 Instruction Cycles to accommodate setting the Timer value.


    The formula to set the Timer1 Preload value is the following:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)

    Where:
    Tpl = Timer Preload value
    Fosc = OSC Frequency
    It = Target Interrupt Time
    Ps = Prescale value
    Tb = Timer Bits (8 or 16)
    Tric = Timer Reload Instruction Cycles


    Here are the computation for 4,8,10,20 and 40 Mhz

    4Mhz:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)
    Tpl = ((2^16)-1)-(.01/(1/(4,000,000/4)*1)-8) = 55,543 (0xd8f)

    8Mhz:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)
    Tpl = ((2^16)-1)-(.01/(1/(8,000,000/4)*1)-8) = 45,543 (0xb1e7)

    10Mhz:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)
    Tpl = ((2^16)-1)-(.01/(1/(10,000,000/4)*1)-8) = 40,543 (0x9e5f)

    20Mhz:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)
    Tpl = ((2^16)-1)-(.01/(1/(20,000,000/4)*1)-8) = 15,543 (0x3cb7)

    40Mhz:
    Tpl = ((2^Tb)-1)-(It/(1/(Fosc/4)*Ps)-Tric)
    Tpl = ((2^16)-1)-(.01/(1/(40,000,000/4)*2)-8) = 15,543 (0x3cb7)

    Cheers.
    Regards,
    TABSoft

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Of course it can be simpler I didn’t know if Ioannis’ comment is sarcastic or not
    It is very simple once you realise it’s incremented to overflow, so the number itself has no meaning.

    Ok, mine is so old there’s no 40MHz definition in it!

    it doesn’t hurt to see this number: 49992. It’s how many instructions you have after the timer interrupt
    that will be free from further interrupt if you’re doing something on TicksChanged rather than SecondsChanged.
    So I have learned to Read/Write I2C, Write LCD, etc in that time.

    Next time I do any LED clock I’d like to try a centi and millisecond display HH:MM:SS:cc:mm!

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    No offense, but I was a bit sarcastic

    @Tabsoft: Nice, a complete tutorial!

    Ioannis

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    No offence taken, it is rather simple. I have to admit though, I’ve looked at it another time and given up.

    I have an idea, but will probably stray off what this thread is for, so will start my own later.

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Art,

    Regarding your remark...

    Code:
    "Next time I do any LED clock I’d like to try a centi and millisecond display HH:MM:SS:cc:mm!"
    You can certainly do this with very few changes to DT's Elapsed_INT file, as I am positive you already know.

    I have already done this once before for a project that I needed to display sub-second time.

    I had created an ElapedDemo program and modified Darrel's original "Elapsed_INT.bas" file appropriately.

    I also took the time to continue to modify the file to incorporate some of the changes he did for his DT_INT-18.bas file for v1.2.
    He did away with hard coded TimerConst values for the different OSC values and replaced it with a compile-time calculation instead.
    He also did away with the TIMER1 prescaler and implemented a SW postscaler instead for frequencies above 20MHz.

    All very cool ideas. Sure do miss DT and his wisdom.

    Plus, I added a change of convenience that others might find useful to quickly change the interrupt period at compile time.

    I will comment the changes, clean up the code and give it a good test.
    Once I am done, maybe tomorrow or the next day, I will post it all here in a zip file.

    Cheers.
    Regards,
    TABSoft

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Hi,
    That sounds cool! Tweaking, adapting, improving and sharing is great but, and this is my personal opinion, please please please, when you do re-post any of Darrels files which has been changed, for whatever reason, PLEASE make sure to name it so that it's easily differentiated from the original file(s). If you change it, please document the changes and do not re-upload it with its original name, for example Elapsed_INT-18.bas. Doing so will eventually create a nightmare....

    /Henrik.

  9. #9
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    No worries Henrik.

    My upcoming post already has the differentiations you suggested implemented.

    Secondly, since the information is not necessarily Darrel's original work but rather a derivative, I was planning on posting it on the Code Examples forum in a new thread to keep it separate from this thread.
    Regards,
    TABSoft

  10. #10
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I can certainly do what I’m talking about, but still also interested in anything you come up with.
    Timer driven (rather than event driven) pic programs are a rather new fad for me.

    The calculation I posted, the reverse of which is:
    Code:
    $FFFF - (((20 / 4) * 10000) - 8) = $3CB7
    Would blow out a word variable if you tried to calc the value for 40MHz!
    I was doing it with a desktop calculator in scientific mode right up to subtracting from $FFFF to look for any decimal places..
    There will be trouble if you use a crystal that is not an even MHz increment.
    Some crystals with decimal places would be fine, but PBP wouldn’t know if you introduced resolution error into the real time keeping.
    I think seeing decimal places is fine right up until the final value $3CB7 is that correct?
    You would not want to see decimal places int he final value.
    Last edited by Art; - 3rd April 2015 at 05:14.

  11. #11
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I was playing with DT_Analog, and random variable set to 0 when I call GetADC.
    This was happen only with long variable enabled.
    So quick look and here is what happened:
    Code:
    #IF  __LONG__
        DTadAccum    VAR LONG ; local - 32-bit sample accumulator
    #ELSE
        DTadAccum    VAR WORD[2]     ; local - 32-bit sample accumulator
    #ENDIF
    If I use PBPL then assembler use this line
    DTadAccum VAR LONG ; local - 32-bit sample accumulator
    And that is fine... Just declaring LONG var.
    But later in code when Darrel clears accumulator:
    Code:
    DTadAccum = 0 : DTadAccum[1] = 0     ; clear the accumulator
    So where is located DTadAccum[1]?
    - 4 bytes after DTadAccum, and where was located my variables that get cleared after calling GetADC? You guessed...

    Solution:
    Replace
    Code:
    DTadAccum = 0 : DTadAccum[1] = 0    ; clear the accumulator
    with
    Code:
    #IF  __LONG__
         DTadAccum = 0  ; clear the accumulator
    #ELSE
        DTadAccum = 0 : DTadAccum[1] = 0  ; clear the accumulator
    #ENDIF
    And that's all.
    Last edited by pedja089; - 15th April 2015 at 13:29.

  12. #12
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Hi Guys,
    First up, respect to Darrel, the more I look into the timer code, it’s more than I thought it was.
    It’s quite some extra trouble to accommodate us.
    I pm’ed Tabsoft, and was going to wait, but even in the last hour, some results have been uncanny.

    I think now, the fact that this works is a problem:
    Code:
    $FFFF - (((20 / 4) * 10000) - 8) = $3CB7
    No matter if you like the other math, this works.
    What is the significance of the value $FFFF? Nothing.
    So I think the code is correct but the constant value should be $3CB8 for 20MHz,
    and similarly, all original constant values need to be incremented by one,
    and the current error is cumulative, and depends on the crystal frequency you’re using.
    If not, have you clocked the code with a standard, and compared against an equal standard for any extended period?

    I am not able to demonstrate in DT’s code any problem, because I think it’s only a math problem.
    Cheers, Art.
    Last edited by Art; - 16th May 2015 at 13:01.

  13. #13
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    ps. Don’t be worried to speak, if I’m right this is very important to me, and probably also others.
    If I’m wrong I’m not afraid to admit so, but the best thing to do whether he’s here or not, is fix any problem.

  14. #14
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I'm not sure that I understand problem, but I'll try to explain...
    For 16 bit timer
    $FFFF Is value at timer overflows to 0 when incremented by 1. So that value doesn't have anything with clock.
    It does with timer(16 or 8 bit)
    So If you need interrupt every timer tick, you will preload timer with $FFFF.
    For 8 bit timer, value is FF, and if you increment that by 1, result is 0 and overflow.
    8 is number of instruction to stop timer, load bytes to timer, and start timer. so you take it into count, when preloading timer.
    And that is true only when clock for timer is Fosc/4. If timer is clocked from Fosc, then it will be 32 ticks for timer, if you load in 8 instruction.
    But if you clock timer from slow clock eg 31KHz or 32768Hz, and processor is clocked from HS osc, you can preload timer in les than one tick. So you can ignore that...
    (20 / 4) this is timer clock. And that value will depend on clock speed, not $FFFF.

  15. #15
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I know the instruction time is 20/4 (assuming 20MHz clock).
    So if the timer registers were larger it would count 5M timer ticks per second.

    DT’s Elapsed timer works by subtracting whatever value (depending on the clock speed)
    from the timer overflow value to achieve 100 interrupts (and 8 instruction timer reload) per second.
    I understand the 8 instruction cycles are compensated for in the constant value that will be continually reloaded to the timer.

    My position is that one doesn’t subtract the value from $FFFF, but from $0000 which is where the overflow interrupt occurs.

    The way that I synced the 1Hz pulse generated by the Elapsed timer code to a GPS pulse per second output
    is by connecting it to portb.0 interrupt, and only allowing the interrupt to reset the second once.
    The port.0 interrupt does not look at the timer value, only resets ticks so only synchronised within 1/100th of a second.

    It only takes a little over an hour for a Human to notice (looking at two flashing LEDs) for them to desynchronise.
    If the constant value is incremented by one, I’m on the third hour watching it so far.
    Initially I was using a crystal running the Elapsed timer, but now rubidium.

    Code:
    'in the ISR after time variables are incremented
    ‘so don’t look at this yet, execution hasn’t gone here
    '
    if INTCON.4 = 1 then		' check if portb.0 interrupt enabled
    if INTCON.1 = 1	then		' check if portb.0 interrupt occurred
    INTCON.4 = 0			' disable portb.0 interrupt
    INTCON.1 = 0			' clear portb.0 interrupt flag
    Ticks = 0			' reset current second to gps pulse
    endif
    endif
    ‘
    ‘since the timer ISR was called by port.0 interrupt,
    ‘and not by timer overflow, any value could be in the timer.
    ’The Ticks are reset to extend the length of the current second.
    ‘We are only synchronised to within 1/100th of a second now,
    ‘but that’s enough for this purpose.
    
    ‘
    'at the start of main program
    '
    '
    INTCON.4 = 0			' disable portb.0 interrupt
    INTCON.1 = 0			' clear portb.0 interrupt flag
    '
    INCLUDE "ElapsedTimer.bas"
    '
    'reset, and start the timer, etc.
    ‘
    
    ‘
    ‘when the main program has run for some seconds
    ‘the qualify bit can be set with a button, or auto set after some seconds have passed
    ‘
    if qualify = 1 then		' synchronise command was qualified
    OPTION_REG.6 = 1		' interrupt on rising edge of portb.0
    INTCON.1 = 0			' clear portb.0 interrupt flag
    INTCON.4 = 1			' enable portb.0 interrupt
    '
    ‘some audible verification the sync command was qualified
    ‘
    ENDIF
    Last edited by Art; - 16th May 2015 at 14:21.

  16. #16
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    You are right...
    But in some example 7 instruction clock are used to compensate for preload. So it is same as if you substract from 0- or $10000.
    I always use 32768Hz crystal, so I didn't pay to much attention to this.

  17. #17
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    How is it demonstrated to everyone? Not everyone has two standards.
    Best I can think of is logic buffer a 10MHz crystal to drive the pic chip running DT timer,
    run a 1Hz flashing LED with the timer,
    and also fan out the 10MHz clock signal to a cascaded bunch of frequency dividers to achieve
    an independent hardware pulse per second derived from the very same crystal.
    Then accuracy of the crystal is then a non-issue, and the two LEDs stray away from each other relatively quickly,
    when the constant value is incremented by one, they will remain as synchronised as they were in the first place.
    It really doesn’t take long, but is there an easier way?

  18. #18
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    The seven instruction example you don’t mean the possibility of skipping an instruction in the reload section?
    That always ends up 8 instructions no matter what the result of the bit test.
    It takes two instructions to skip one instruction that would otherwise have to be executed.

    Mods, this might end up inappropriate for this thread, which was not meant for pointing out errors.
    I will do what it takes, and when done, it maybe is appropriate to clean up this bit of the thread.
    This is still I think the best tool PBP has going for it.

  19. #19
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I found where i saw 7 instructions...
    In misterE Pic Multicalc, default is 7 instr for reload.
    Check it out, and compare results. As far I can see, it uses $10000.

  20. #20
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    If it’s discovered, I’m real surprised it hasn’t been noted somewhere,
    because original values/calculations are still being used also.
    It would be affecting commercial products out there I’ll bet.
    If you assume 7 instruction cycles for reload, and use the correct overflow value, isn’t that still exactly the same problem?

    Test is easy.. make 10,000 Hz hardware PWM, and 100Hz PWM with Elapsed timer on the same chip.
    10,000Hz is the lowest even freq at 50% duty cycle without resolution error assuming 10 MHz clock.
    So then it only takes one pic and two dividers running from the same crystal.
    Then any old crystal of any value doesn’t matter... run the chip as fast as possible, until the LEDs run away from each other.

  21. #21
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Art,

    Trying to respond to your PM and your inbox is full.....
    Regards,
    TABSoft

  22. #22
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Put it out here.. if it happens I end up the fool, I still appreciate finding out

    ps.. it’s cleared if you must.

  23. #23
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Ok I will just say respectfully same as if he were here, got you Darrel!

    Really, we are all Human and this code is excellent.
    I hope it is agreed that it is important to evaluate and improve things like this (and especially this).

    Mods please, it would be the right time, if possible to make some of this thread disappear.
    I’m sure Tabsoft will take care of it. I think original versions should be preserved, but somehow marked.
    I have agreed to help test if need be (at least can do good tests with 16F and show how the test is done).
    but the solution, is really that easy.
    Cheers, Brek.

  24. #24
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    I'm doing some chores outside.

    One of the mods will move the recent posts to a new thread asap.

    Robert
    (beautiful day here)

  25. #25
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Thanks, no rush.. It just seems inappropriate, to do this, and also right and needed at the same time.
    Tabsoft’s thread might be appropriate if some of this can just be moved. Now I have spoken to him I think it would be ok with him also.
    Thanks Robert.

    Brek
    (I wish I could say the same, it’s been hell. I needed this to be wrong or it would be big problems for me, but all good now,
    it might have been Sunny outside for all I know)

  26. #26
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    No worries about moving this to the Thread I started over in Code Examples.

    Go ahead as it is relevant. With one caveat. The code I posted most likely does not have this issue as Brek and I have discussed and he would be testing.
    Regards,
    TABSoft

  27. #27
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    From what you showed me it looks right.
    It’s bed time now... 5:55am here!
    Later, I will try to explain better what I’m doing. It wouldn’t be good enough to show off hardware,
    anything could still be happening in software. It would be best to come up with something simple rather than fancy.

  28. #28
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Guys, give me a title for the new thread and I'll set it up.

    Robert

  29. #29
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Temporary central repository of Darrel Taylor's works (including Mr E's Multicalc

    Um, if not Tabsoft’s thread, then how about just deleting these bits.
    I’m sure everyone who posted lately in this one would understand.
    Or for another thread something like "Elapsed Timer findings" or something.

  30. #30
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    i am a bit lost on what the outcome of this has been , its quite a long thread

  31. #31
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    If you use Darrel’s original code you want to add 1 to the constant reload value you use (to the table at the top of this thread).
    It appears if you are using Tabsoft’s modified version that calculates the value for you based on crystal frequency,
    it should be correct already (and has been the whole time).
    We are still talking, but also we must be across the world from each other, I only get his emails live if I stay up late.

    I’m only waiting to see what he’d like me to do (if anything) about testing it.
    The best I can tell for all of the crystal values Darrel provided, using the correct constant value should cause the software
    to perfectly keep time indefinitely... of course crystal tolerance makes this not possible..
    but the software should not introduce any of it’s own error.

    Some testing will happen in any case, a six month long project of mine is a GPS equipped atomic clock!

  32. #32
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    so to clarify

    this is the version i know of

    i use it on a 64mhz osc - giving TIPS = 4 from memory for a 2.5ms tick


    Code:
    ; -------------- 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   ; add the 8 cycles to constant 
      
    ; -----------------  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

  33. #33
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    That looks like Tabsoft’s modification to me. He’ll explain the math better shortly that I would.
    Darrel’s original code has a block of code in it like the first post in this new thread...
    effectively a compile time lookup table.

  34. #34
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    Actually, it really looks like DT's "Elapsed_INT-18.bas" file in his T_Elapsed-18 project which uses DT_INTS-18 for 18F PICs.
    That is where I got the inspiration for my version of the 1ms Elapsed Timer Demo for DT_INTS-14 and I posted in the separate thread in the Code Examples forum.
    Regards,
    TABSoft

  35. #35
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    I would like to provide a follow up to the discussion in this thread regarding Darrel Taylor's Elapsed Timer demo application.

    The Elasped Timer demo we are speaking of is a project that Darrel had posted here and on his website.
    The Project was called Elapsed_Demo.bas (code dated Jan 10, 2006) and also had the following include files.
    INCLUDE "DT_INTS-14.bas"
    INCLUDE "ReEnterPBP.bas"
    INCLUDE "Elapsed_INT.bas"

    The issue is within the "Elapsed_INT.bas" include file where Darrel provided hard-coded values for the "TimerConst" variable for Oscillator rates of 4/8/10/16/20 MHz.
    It is these hard-coded values that are incorrect.
    Code:
    Relevant section of Elapsed_INT.bas:
    Asm
      IF OSC == 4                       ; Constants for 100hz interrupt from Timer1
    TimerConst = 0D8F7h                 ; Executed at compile time only
      EndIF
      If OSC == 8
    TimerConst = 0B1E7h
      EndIF
      If OSC == 10
    TimerConst = 09E5Fh
      EndIF
      If OSC == 16
    TimerConst = 063C7h
      EndIF
      If OSC == 20
    TimerConst = 03CB7h
      EndIF
    As Art had pointed out a little earlier in this thread, and we have confirmed through code review and testing, Darrel's hard-coded values he used for the Timer1 Preload values (TimerConst) for a 10ms (100Hz) Interrupt were incorrect.

    The value he used for each of the Oscillator rates is (1) less than it should be.
    The reason is that he calculated the Timer1 counter overflow value as 65535 instead of using 65536.
    Timer1 is a 16bit counter from 0 to 65535.
    It takes 65536 counts to overflow (Wrap back to 0) and trigger the Timer1 interrupt.
    This means that his 10ms interrupt will actually run longer by (1) instruction than it should.

    As an example of the wrong value used, for a 4MHz Oscillator rate the interrupt will fire at 10ms + 1 instruction cycle (1us).
    Darrel's Preload value (TimerConst) was hard-coded to 0D8F7h (55543 dec), which is incorrect and it should be 0DF8h (55544 dec)

    For those interested, here is the math.

    When using a Timer1 Interrupt without any prescale or postscale values, Timer1's counter will increment by 1 for each instruction executed.
    Each instruction takes (1 / (Fosc / 4)) amount of time.
    So for a 4Mhz Oscillator rate, 1 instruction cycle (Tcy) = 1 / (4000000 / 4) = 1 / 1000000 = .000001 seconds (1us).

    Darrel calculated the preload value (number to preset the counter to) this way.
    Tpl = 65535 - (It / (1 / (Fosc / 4)) - Tric)

    Where:
    Tpl = Timer Preload value
    65535 = Timer1 Counter Overflow value (Which is wrong, should be 65536)
    Fosc = OSC Frequency
    It = Target Interrupt Time
    Tric = Timer Reload Instruction Cycles

    So for the 4MHz Oscillator rate example:
    Tpl = 65535 - (.01 / (1 / (4000000 / 4)) - 8)
    Tpl = 65535 - (.01 / (1 / (1000000)) -8)
    Tpl = 65535 - (.01 / (.000001) - 8)
    Tpl = 65535 - (10000 – 8)
    Tpl = 65535 - 9992
    Tpl = 55543 (D8F7h)

    This will load 55543 into Timer1's counter.
    Now how many counts does it take for Timer1 to overflow from here?
    If your target is a 10ms interrupt and the Instruction Cycle time is 1us, then you want it to count exactly 10000 instructions including the instructions it uses when it reloads the Timer preset (which the code does in 8 instructions).
    So starting at 55543 + (10000-8) = 55543 + 9992 = 65535

    Timer1's counter is now at 65535 and it will not overflow until 1 more instruction occurs.

    So this 4MHz example shows that the Timer1 interrupt will fire at 10.001ms not 10ms.

    Even though the error is just 1 instruction cycle, the timing impact could be significant.
    The timing error is inversely proportional to the Oscillator rate.
    The slower the Oscillator rate, the worse the timing will be off.

    And as Art pointed out the error is cumulative.
    Every time the interrupt fires, 1 extra instruction is executed, so if the interrupt fires 10 times the total elapsed time for the 4MHz example would be 10.010ms instead of the correct 10ms.

    I hope this clarifies Art's observations and our subsequent findings.
    Last edited by Tabsoft; - 20th May 2015 at 15:45. Reason: Grammatical Issue
    Regards,
    TABSoft

  36. #36
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Thumbs up Re: Elapsed Timer findings

    Excellent tutorial Tabsoft.
    Explains why I always had to "fine tune" after the fact when setting up a timer.
    Louie

  37. #37
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    Ok I was thinking where you said you made the PWM to measure on a scope,
    so had my best go at that sitting here rather than scab the code
    Probably not the most effective with memory or time, but I think the frequency will be correct.
    and it’s probably also more like asm pseudo code at the moment.

    Code:
    ‘start of code somewhere:
    ‘
    flipflip var byte
    
    
    ‘in ISR straight after Ticks is incremented
    ‘
    @ btfsc flipflop ,00 		; 1/2
    @ goto aaaa			; 2
    @ goto bbbb			; 2
    aaaa:				‘
    @ bsf portb, 07			; 1
    @ goto overpwm			; 2
    bbbb:				‘
    @ nop				; 1
    @ bcf portb, 07			; 1
    overpwm:			‘
    @ comf flipflop 		; 1 - but timing doesn’t matter here
    
    ‘instruction time 6 for flip state, 6 for flop state,
    ‘7 for the code to execute no matter the status
    I like the idea the PWM is almost free if you wanted it,
    and there might be a situation you’d want the electrical frequency of the timer.

    It might not have originally been obvious I was setting this up for a pic.. only works up to 20MHz though:
    Code:
    '$FFFF - (((20 / 4) * 10000) - 8) = $3CB7
    ‘might as well fix the constant val too
    
    constval = $FFFF - (((f/4)*10000-7)'
    Though I have not needed to, it’s pointless unless you are varying the pic’s oscillator frequency,
    and also telling the pic program the new osc frequency, or figuring out some other way what frequency the pic is running at.
    That PWM would indicate resolution error at runtime for different osc frequencies.
    Last edited by Art; - 20th May 2015 at 17:02.

  38. #38
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    Very similar to what I did.
    I used the Elapsed Timer to generate a 1KHz 50% duty cycle signal on PORTA.4.
    I wanted to test the stability of the Timer1 interrupt period using the Elapsed Timer code we have been discussing.

    In the ISR I used a call to the RELOAD_TIMER macro after setting the state of the pin.
    That lets the Elapsed Timer code handle things and I could check the accuracy of the output.

    I was lazy though and did not setup a variable to track the state of the pin.
    I just read the port and flipped the pin.
    This was a test and I really didn't mind if I hit the other pins on PORTA.

    500ms Timer1 ISR:
    Code:
            asm
                COMF PORTA, 0   ; 1 Tcy - Complement PORTA, Store in W
                ANDLW 10h         ; 1 Tcy - Force all PORTA pins to 0 except pin 4 
                MOVWF PORTA    ; 1 Tcy - Set the new value for PORTA
                RELOAD_TIMER    ;  Reload the timer
            endasm
    Last edited by Tabsoft; - 20th May 2015 at 17:20. Reason: Additional Info
    Regards,
    TABSoft

  39. #39
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    There’s some fun to be had. It would be possible to rotate a byte with any value in it
    and look at the value of a bit in that byte to get some more free frequency divisions.
    Or if you had a 100Hz timer and wanted a 1Hz pulse, you could do similar to the timer code,
    load a constant value to a byte, increment it, and watch for one of the more significant bits to be set.

  40. #40
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer findings

    Tabsoft

    Re: Elapsed Timer findings
    Actually, it really looks like DT's "Elapsed_INT-18.bas" file in his T_Elapsed-18 project which uses DT_INTS-18 for 18F PICs.
    That is where I got the inspiration for my version of the 1ms Elapsed Timer Demo for DT_INTS-14 and I posted in the separate thread in the Code Examples forum.

    tabsoft is correct its from DT website at the time , using DT_INTS-18 ,

    and like tabsoft found i also required a test pin ( spare pin) to toggle , where i put a CRO on it to , then tune the main osc to get a 10ms pulse , so to be as accurate as possible ( i am using the internal OSC )


    and from tabsofts excellent break done looks like darrels " elapsed_int_18.bas ", asm is correct as he uses 65536 to caclulate the reload value

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 18:39
  2. SPWM and Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 8th May 2008, 04:16
  3. Darrel Taylor Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th March 2008, 02:22
  4. 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 : 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