Reading Timer1 value


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382

    Default Reading Timer1 value

    I have a project where I need read and compare the current Timer1 value to a constant.
    In the datasheet there is a note stating

    "...the user should keep in mind that reading the 16-bit timer in two 8-bit values itself, poses certain problems, since the timer may overflow between the reads"

    I plan to use the following to read the time on the clock into a single word variable. That max. value should not get to the point where the timer would overflow:

    period.lowbyte = TMR1L ; Store the Timer1 value in period variable
    period.highbyte = TMR1H

    Has anyone ever run into a problem reading Timer1 and not getting an accurate/true value? Is it possible to just use TMR1 instead of the above example?

  2. #2
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    DynamoBen,
    Not a direct answer to your question, but food for thought:

    Depending on which PIC your using, Timer1 may have a 16-bit read/write option. Simple put, if enabled, when you read TMR1L, TMR1H is buffered, so that no errors are introduced due to the delay between reading the two registers.

    I have a project where I need read and compare the current Timer1 value to a constant.
    You also might want to check out CCP-compare mode. It sounds like a possible solution for you to try.


    Steve

  3. #3
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    CCP is not an option for what I'm doing. However the info on read/write is interesting. I'm using the 16F88.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hiya Ben,

    This might help too.
    http://www.picbasic.co.uk/forum/show...php?t=3891#VAR
    <br>
    DT

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Thumbs down

    Quote Originally Posted by DynamoBen
    CCP is not an option for what I'm doing. However the info on read/write is interesting. I'm using the 16F88.
    Then the 16-bit read is not an option either. I am pretty sure it is only available on the 18F models. Oh Well.

    Steve

  6. #6
    L_Gaminde's Avatar
    L_Gaminde Guest


    Did you find this post helpful? Yes | No

    Default

    Is there a timer2 in this chip and is it 8 bit then only one read is necessary

  7. #7
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    8bit isn't an option, I need 16bit.

  8. #8
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by DynamoBen
    In the datasheet there is a note stating "...the user should keep in mind that reading the 16-bit timer in two 8-bit values itself, poses certain problems, since the timer may overflow between the reads"

    ... That (DynamoBen's) max. value should not get to the point where the timer would overflow:
    You not only need to worry about the timer overflowing from $FFFF to $0000 but from $xxFF to $x(x+1)00. IF you read the lowbyte and then the lowbyte overflows before you read the high byte, you will get the wrong answer. If you cannot stop the timer in your application, try something like this

    Code:
    X var BYTE ' in addition to your variables
    
    period.highbyte = TMR1H
    period.lowbyte = TMR1L
    X = TMR1H – period.highbyte
    If X = 0 then Continue
    period.highbyte = TMR1H
    period.lowbyte = TMR1L
    Continue:
    to ensure you get the correct time. (If at all an option, I would stop timer before reading and then your issue goes away.) Although you did not mention it, you should stop the timer before you write to it per a recommendation from Microchip.

    Paul Borgmeier
    Salt Lake City, Utah
    USA
    Last edited by paul borgmeier; - 19th June 2006 at 08:29.

  9. #9
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Perfect, that was the info I was looking for. I will try to stop the clock before any read/write.

    Thanks!

Similar Threads

  1. Software PWM using Timer1
    By muqasim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2009, 11:49
  2. Help with TIMER1 + SLEEP
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2009, 15:07
  3. Time Period of Timer1
    By arnol34 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th May 2007, 00:31
  4. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24
  5. Use of Timer1 with 16F819
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2005, 16:25

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