Not getting this timer thing.


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    Hardware timers will continue running once enabled. It runs on its own, in the background, until you disable it.

    If you're in a loop reading the timer, checking for a value > x, the value you read indicates how long the timer has run.

    When you insert the test loop, this wastes time, which adds to the timer count since it's still running while your code in the test loop executes.

    So - the more code that executes before you read the timer, the larger the count will be. At least until it rolls-over back to 0.

    The timer is also running during your PAUSE time. At 16MHz, with a prescaler of 1:1, Fosc/4 as the timer clock source, timer1 increments by 1 count every 250nS. Since timer1 is a 16-bit timer it will count from 0 to 65,535 then roll-over back to 0 and continue counting.

    Here's a neat example showing how to use a timer to see how long PBP code takes to execute.
    http://www.picbasic.co.uk/forum/showthread.php?t=365

    Here's on using timer2 as a clock
    http://www.picbasic.co.uk/forum/show...0262#post70262
    Last edited by Bruce; - 29th July 2011 at 13:29. Reason: Links
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    taking a stab at this:
    I am not familiar with "cadd code" If you intended to say "copy code" then yes, I did copy some code in an effort to learn how it works.
    I think Steve was saying add code. With that he is pointing out that you have added code overhead but don't use interrupt on the timer, so the added code slows the clock down. Adding the pause is just to illistrate this further.

    And all that Bruce said.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    May 2011
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    So is the 3 seconds the test loop adds an accumulation of the timer1 count value above 40380 when the value is finally checked? I guess I didn't account for the overage if that is the case.

    I did some more playing with timer2 last night and did the same test as I did with timer1. Regardless of what test loops I put in, the time appeared to stay the same. That had me looking for difference between timer 1 and 2. Is it fair to say the timer2 routine would also have an overage and is longer with the test loop but it isn't noticeable when timing with a stop watch?

    Here is the timer2 code:

    Include "modedefs.bas"
    DEFINE OSC 16
    OSCCON=%01111010
    test var byte
    PR2 = 249
    time var word
    msec var word
    msec = 0
    sec var byte
    sec = 0
    mint var byte
    mint = 0
    hour var byte
    hour = 0
    'pir1.1 match flag

    tmr2=0
    pr2 = 1

    high portb.4
    pause 500
    low portb.4

    'T2CON.2=1

    t2con =%01111111
    do
    if pir1.1 = 1 then
    pir1.1 = 0
    msec = msec + 1
    if msec = 1000 then
    msec = 0
    sec = sec +1
    if sec = 60 then
    sec = 0
    mint = mint+1
    endif
    endif
    endif

    'test = 0
    'do until test = 127
    'test = test +1
    'loop
    if mint = 1 then
    mint = 0
    high portb.4
    pause 500
    low portb.4
    endif
    loop

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    YES!!!
    The test loop adds execution time, which causes your time before check to be longer.
    There are prolly some differences in how you can use timer 1 and timer 2, but if they are set up as counters triggered by fosc/4, they will count the same.

    You have a few choices in how to deal with getting accurate times. the first and always pointed at is to use interrupts. The second is to auto adjust the count while the program is running. to do this, once the timer is greater then target number, subtract target number from the CURRENT timer value. when you reload the timer, reload it with your reload value + the difference just calculated. Mind you, that is for a counter counting up. If using PR (as with timer 2) you can just load timer 2 with the difference. then it will count from adjusted number to PR value, instead of from zero to PR.

    Did any of that make sense? Both methods are valid and useful depending on application. (Thanks PBP members for the auto correct idea)
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    Consider dt-ints and this structure.Name:  INTERRUPT.JPG
Views: 416
Size:  70.9 KB
    don
    amgen

  6. #6
    Join Date
    May 2011
    Posts
    42


    Did you find this post helpful? Yes | No

    Default Re: Not getting this timer thing.

    I can attain the results I need through the Timer2 and with the serial comms I need to mix in, timer2 is probably the best way. Thanks to all who set me straight on this. I think I understand enough now to get me to the next item I don't understand.

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