Menu

Reading all 16 bits of a timer

Help
2009-03-29
2013-05-30
  • Edward LaBudde

    Edward LaBudde - 2009-03-29

    Hi all, newbie here. 

    I know this may sound like a dumb question, but how do I read all 16 bits of timer 1? 

    I tried TMR1H +TMR1L and it does not seem to work.

    Thanks, Ed.

     
    • Nobody/Anonymous

      For example you can do this way:

      dim timer1_value as word

      timer1_value = TMR1L
      timer1_value_H = TMR1H

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-30

      KENT, SANTIAGO, help me out!  I really need to know how to use all 16 bits of timer 1.  Please, Regards, Ed.

       
    • Santiago

      Santiago - 2009-03-31

      Hi.

      Reading a 16 bit timer is not that easy, because while you are reading one byte the other could have been incremented and the reading can be erroneus.
      The recomended by microchip is something like this:

      '- Read the high byte:

      high_byte = TMR1H

      '- Read low byte

      low_byte = TMR1L

      '- Read the high byte again in other variable:

      high_byte-B = TMR1H

      '- Verify that high byte has not changed:

      if high_byte = high_byte-B then goto reading_done

      '- If high_byte has changed now we have 255 cicles untill the next icrement, then we can read safely:

      high_byte = TMR1H
      low_byte = TMR1L

      reading_done:

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-31

      Santiago, thank you for the response.  That shows me how to read the two bytes under dynamic condtions.  In my application I stop the timer to read the values, then do calculations on the timer output. 

      Now how do I combine them into a single 16 bit word to use in my calculations?   

      I need to use all 16 bits of the timer to get the resolution I need.  Do you use the rotate command?

      Thanks, Ed.

       
    • Santiago

      Santiago - 2009-03-31

      Hi Ed.

      Then the suggested solution works Ok:

      dim timer1_value as word

      timer1_value = TMR1L
      timer1_value_H = TMR1H

      The PIC only has 8 bit registers, then onlu 8 bits variables are allowed. to work with 16 bits variables, GCbasic creates two variables: one with the name you give it, for the low byte, and other adding "_H" to the name, for the high byte.
      When you do this:

      timer1_value = TMR1L

      TMR1L will be loaded to the low byte of timer1_value and the high byte of timer1_value will be cleared. then you do this:

      timer1_value_H = TMR1H

      This is a byte operation for the compiler, then TMR1H is loaded to the high byte of timer1_value, and low byte keeps untouched.

      This will not work:

      timer1_value_H = TMR1H
      timer1_value = TMR1L

      Because the second operation is a word (two bytes) operation for the compiler, then the high byte is cleared and TMR1H is lost in this operation.

      Regards.

       
    • Edward LaBudde

      Edward LaBudde - 2009-03-31

      Santiago, thanks for the response, I will try it out, Regards, Ed.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.