Calculate TMR0 reload value at compile time?


Closed Thread
Results 1 to 4 of 4

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Henrick,

    The "method" looks OK, but I think you'll have some problems with the math due to integer truncation.

        #define nS_per_cycle #v((1000/(OSC/4)))

    That line will work ok IF the OSC freq. is an exact multiple of 4mhz.
    But if OSC is 10mhz then (OSC/4) is 2.5 and the .5 gets dropped. Then nS_per_cycle would be 500, which is wrong for 10mhz.

    Same as with PBP, do the multiplication first to avoid the integer problem.

        #define nS_per_cycle #v(1000*4/OSC)
    Of course, they are constants, so you could just (4000/OSC).

    The same thing happens with ...
        #define MB_Frame_Timeout_ns #v((100000000/(MODBUS_BAUDRATE / 11)) * 35)

    There are no banking issues to deal with in your code.

    hth,
    DT

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Darrel,
    Thank you!
    I can't believe I didn't catch that one. I noticed the truncation, I even have it display the calculated results yet I never noticed the ns per cycle number being way off...

    I don't think the error in the timeout calculation would have ill effect in real life but I changed it to:
    #define MB_Frame_Timeout_ns #v((100000000 * 11 / MODBUS_BAUDRATE) * 35)

    Thanks, as always it does help a lot.

    /Henrik.

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