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,644


    Did you find this post helpful? Yes | No

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

    you have to toggle your led at the correct place in the loop
    the interrupt runs at 4x the speed required (because of high fosc not chip type ) for timing so the post scaler divides it by 4


    ClockCount:
    @ RELOAD_TIMER ; Reload TIMER1

    T1Post = T1Post + 1
    Robert toggle your pin here eg latd.0=!latd.0 for 2.5mS
    IF T1Post = T1PS THEN
    T1Post = 0
    Ticks = Ticks + 1
    Robert toggle your pin here eg latd.1=!latd.1 for 10mS
    IF Ticks = 100 THEN
    Ticks = 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

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


    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 have to toggle your led at the correct place in the loop
    the interrupt runs at 4x the speed required (because of high fosc not chip type ) for timing so the post scaler divides it by 4
    I know that now because I've spent 3 days on this. LOL

    But ask me in a week and I won't remember, I'll spend another day running around my tail wondering why I get 4 times too many ticks at 64MHz.

    I might eventually remember this long-term, if I do it for weeks. But I have sleep apnea that can't be treated completely, so what I experience during the day doesn't always get stored properly overnight in my sleep. Consider me as obsessive compulsive against my will. I can leave comments about quadruple ticking, but I'll glance over the comment and ignore it until it bites my face for a day or so. LOL

    Shoot, my wife gave me $600 a month ago and I totally forgot.

    Robert

  3. #3
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

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

    Hi Robert,
    Getting to know the timer is fine, but that would be otherwise extreme for button debounce.
    You can do a program timer to lock the button for a delay after being pushed.

    Code:
    dtimer var byte
    dtimer = 0
    
    main:
    
    IF port.1 = 1 && dtimer = 0 then
    dtimer = 200
    gosub fire ‘oh no, he pushed the button, all hell is going to break loose.
    endif
    
    
    IF dtimer > 0 then
    dtimer = dtimer-1
    endif
    
    goto main
    If you needed interrupt driven buttons it would pay to set the timer in the ISR and
    decrement the timer in your main program, and reenable interrupts when the timer reaches zero.

    It’s good practice to keep interrupts for the thing that really needs it… like keeping real time is a good example

    With regard to the speed, you can’t count to 400 in your Ticks byte,
    but you can count to 240 Seconds (instead of 60 Seconds) to delay the time

    Code:
               IF Seconds = 240 THEN
                  Seconds = 0
                  Minutes = Minutes + 1
                  MinutesChanged = 1
               ENDIF
    Now if you have to display the seconds anywhere just:

    Code:
    DisplaySeconds var byte
    
    DisplaySeconds = Seconds / 4 ‘ divide by four
    ‘print Hours, Minutes, DisplaySeconds.
    If you needed a high speed signal find and asm routine to rotate a byte with carry, and output bit.0 out a pin.
    The benefit of that is ensuring it takes a constant period of time to run the check.
    You might find in this code:

    Code:
    IF flag = 0 then
    flag = 1
    else
    flag = 0
    endif
    once it’s decompiled it could take a cycle less to execute depending on the result.
    So if you’re trying to produce a square wave it could be jagged.
    These things are easier to do with HPWM of you have the hardware though.

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

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

    With the thing running exactly 4 times too fast I would have rotated this byte (with carry),
    and read the value of bit0 each time:
    Code:
    %00001111

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 : 2

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