+ Reply to Thread
Results 1 to 16 of 16
  1. #1
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Revisiting Timers & Time Stamps

    Richard and other wise masters of PIC processors,
    I have an application where I want to record elapsed time stamps (of any units whatsoever) that accompany sensor data packets and the requirement (for me) is to do this without using interrupts or any code processing to keep the “clock counter”.
    The obvious solution to this is to utilise one of the 16 bit on-chip timers that accompany many PICs.
    So, basing this project on the PIC18F27J53 (utilising the plentiful on-chip flash to store the data), I decided to utilise the available reference clock output (which can postscale the system clock up to 32678 times) and hardwire that pin to the clock pulse input pin of Timer 1 (T1CKI)
    With a 4Mhz system clock, this can provide a 4000000 / 32768 = 122.0703Hz input to timer1 which can be further prescaled up x8 times in Timer1 if necessary (not using that though).

    So, I’ve written a simple program to verify the timing which basically
    1.Sets up everything
    2.resets timer1’s 16bit counter to 0
    3.executes a pause statement for 65000 ms
    4.reads timer1’s counter into a 16bit variable
    5.reports the number within the variable

    So, after 65sec pause, I should be getting a theoretical count of 7934 in Timer1, but I’m reporting 7942 every time I run the test. That equates to a ~60ms discrepancy. Now, to my (albeit ignorant) thinking, there shouldn’t really be much discrepancy at all as both the counter and pause command use the same system clock for timing and the code capturing the count is simply:

    TMR1H = 0
    TMR1L = 0
    Pause 65000
    Timer1_Out.lowbyte = TMR1L
    Timer1_Out.highbyte = TMR1H

    Any ideas what would be causing the discrepancy?

    Notes:
    Timer1 Settings
    T1CON.7,6 = 10 ‘Timer1 clock source is the T1OSC or T1CKI pin
    T1CON.5,4 = 00 ‘1:1 prescaling
    T1CON.3=0 ‘Turn off external crystal driver
    T1CON.2=0 ‘Synchronize external clock input (tried both sync and not sync)
    T1CON.1=0 ‘Enables register read/write of Timer1 in two 8-bit operations
    T1CON.0=1 ‘Enables Timer1

    T1GCON.7 = 0 ‘Timer1 gate enable = off

    ‘REFERENCE OSCILLATOR CONTROL
    REFOCON.7 = 1 ‘Reference oscillator is enabled on REFO pin
    REFOCON.4 = 0 ‘System clock (FOSC) is used as the base clock; the base clock reflects any clock switching of the device
    REFOCON.3-0 = 1111 ‘= Base clock value divided by 32,768

    And yes, this PIC does have RTC functionality, but I need subsecond resolution for the ET stamps

    Thanks for any insight,

    Troy

  2. #2
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    307

    Default Re: Revisiting Timers & Time Stamps

    Just for kicks, make sure Timer 1 is Off, clear TMR1H_L, then turn it on. Pause 65000, turn Timer 1 Off, then read TMR1H_L. See what you get. You may be eating up a few milliseconds (8 ms) doing background stuff.
    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.

  3. #3
    Join Date
    Sep 2009
    Posts
    770

    Default Re: Revisiting Timers & Time Stamps

    Probably inaccuracy of PAUSE command.
    It is error of just 0.9-1uS per mS(60uS/65000). And that is just one instruction time when running on 4MHz(4 000 000MHz/4clock per instruction =1000000 Instruction per second, or 1uS per second).
    Check timing with oscilloscope, or with freq meter.
    If I'm correct you won't see any meaningful difference between your code, and what Mike suggested.

  4. #4

    Default Re: Revisiting Timers & Time Stamps

    Also, you might find it better to use 16-bit read/write mode by setting RD16 (T1CON.1).
    That will help with any possible rollover between the two registers.

    You're already doing this, but in 16-bit mode write TMR1H then TMR1L, and read TMR1L then TMR1H.
    The order is important since TMR1H is latched by accesses to TMR1L.

  5. #5
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Re: Revisiting Timers & Time Stamps

    pedja089 & Mike,
    yes there was no difference with what Mike suggested (still 7942 counts), but what exactly are you suggesting I check with an oscilloscope, or with freq meter? The duration of the pause command? The discrepancy/inaccuracy you describe sounds like at least 1 order of magnitude lower than what I'm getting?

    tumbleweed, that obviously sounds appealing (not convinced it'll make much difference in this instance), but how do I do that re: read and write the 16bits at once to a register pair? The datasheet says that reading and writing to the low register will also update the high buffer if set to 16bit mode, but how do I do that in PicBasic? My attempts so far have been fruitless.

    Thanks,

    Troy

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,190

    Default Re: Revisiting Timers & Time Stamps

    In 16 bit mode, when writing or reading the timer always write or read the timer.low byte first as the timer.high byte is triggered by the low.byte read or write operation.
    Dave Purola,
    N8NTA
    EN82fn

  7. #7
    Join Date
    Sep 2009
    Posts
    770

    Default Re: Revisiting Timers & Time Stamps

    Quote Originally Posted by rocket_troy View Post
    pedja089 & Mike,
    yes there was no difference with what Mike suggested (still 7942 counts), but what exactly are you suggesting I check with an oscilloscope, or with freq meter? The duration of the pause command?
    Yes. Set some pin HI before and low after pause 65000. But you probably need to use something lower, like pause 1000.
    Quote Originally Posted by rocket_troy View Post
    The discrepancy/inaccuracy you describe sounds like at least 1 order of magnitude lower than what I'm getting?
    I think that I described it accurately, but it was typo
    It is error of just 0.9-1uS per mS(60uS/65000).
    Should be
    It is error of just 0.9-1uS per mS(60mS/65000).
    So you have very small error generating 1mS, and when you add up all that 65000 times, it shows as 60mS.
    That is more than enough accurate for PAUSE and how it works.
    I would just relay on math and timer. Not checking how compiler implements stuff...

  8. #8

    Default Re: Revisiting Timers & Time Stamps

    In 16 bit mode, when writing or reading the timer always write or read the timer.low byte first
    No, that's not correct. In 16-bit mode TMRH is buffered. All reads and write go thru the buffer register.

    When you write, you must set TMRH first then set TMRL. A write to TMRL latches the TMRH buffer value into TMRH.
    When you read, the opposite is true. Reading TMRL will load TMRH into the TMRH buffer so that when you read TMRH you get the buffered value.

  9. #9
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Re: Revisiting Timers & Time Stamps

    pedja089,
    thanks that clears that up. So far, I haven't measured the timing externally (yet), but what you suggest makes sense.

    re: 16 bit timer access: so, am I correct in saying that 16 bit read/write mode still requires x2 8 bit reads and writes in the code, but the difference is that the register pair get updated simultaneously upon writing to the TMRL register?

  10. #10

    Default Re: Revisiting Timers & Time Stamps

    Yes. Also, for reads you get a snapshot of both values simultaneously.

    Otherwise, in 8-bit mode you should look at the L and H values to determine if they're invalid due to a rollover right as you read them.

  11. #11
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Re: Revisiting Timers & Time Stamps

    Thanks tumbleweed

  12. #12
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Re: Revisiting Timers & Time Stamps

    pedja089,,
    so I just ran a test comparison between Pause & Pauseus and upped the resolution by reducing the Reference Oscillator Divisor from 32768 to 8 (500KHz @4Mhz system clock) using the same code as I originally used:

    Pause 65 produced 32540 counted pulses
    Pauseus 65000 produced 32500 counted pulses (exactly theoretical).

    Confirmation you were smack-bang on the money!

  13. #13
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    78

    Default Re: Revisiting Timers & Time Stamps

    I said "using the same code as I originally used:"

    I should note that for this latest test I also ran the register access in 16bit mode as per Tumbleweed's suggestion.

  14. #14
    Join Date
    Sep 2009
    Posts
    770

    Default Re: Revisiting Timers & Time Stamps



    16bit mode shouldn't make any difference. Because timer clock is much more slower than processor clock. So even 4 instruction clock needed to read and store 2 register into 2 values, won't make changes.
    16bit is must when you clock timer with Fosc/4 or similar speed. But in this case just make thing little safer(there is really small chance that lower byte would rollover in that 4 instruction)

  15. #15

    Default Re: Revisiting Timers & Time Stamps

    16bit mode shouldn't make any difference... (there is really small chance that lower byte would rollover in that 4 instruction)
    That's true, but you might as well do it the correct way. I hate telling my customer the code "works most of the time".

    If you're doing this in 8-bit mode, one way to deal with a 16-bit timer without stopping it first is to:
    1) read TMRL
    2) read TMRH
    3) re-read TMRH. if not equal to value read in step 2, go to step 1

  16. #16
    Join Date
    Sep 2009
    Posts
    770

    Default Re: Revisiting Timers & Time Stamps

    I agree with you. You are right.

Similar Threads

  1. Timers & Counters
    By chapkagop58 in forum General
    Replies: 2
    Last Post: - 23rd February 2012, 02:18
  2. User Position, Velocity & Time II (D1h)
    By dj.lambert in forum GPS
    Replies: 2
    Last Post: - 31st July 2009, 09:21
  3. Timers
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th November 2008, 20:08
  4. Time & Simulation
    By weirdjim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th August 2007, 18:45
  5. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03

Members who have read this thread : 14

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