TMR1H = 0x85; // sets Timer1's most significant byte
TMRIL = 0xED; // sets Timer1's least significant byte
Rather small detail...on my posts above i made a typo mistake ... TMRIL should be TMR1L...so don't panic if you're getting any "symbol undefined" errors!! Sorry about that...
Quote:
|
Originally Posted by CronosPrime1
Do you think you could provide more details on interrupt driven timers? Does it use up one of the six interrupts your robot has? Oh, and the 25 ms timing loop, why is it 25 ms? It seems to me that the calculation is:
2 bytes*8 bits/byte = 16 bits
largest possible 2-byte integer = 2^16 - 1 = 65535
And 800 ns*65535 =52.428 ms
You seem to get half of that...
I'm not doubting the correctness of 25 ms, I just wonder as to why it is 25 ms...
|
Now a few comments on the 25 ms loop...:
OK up to that point that's correct... according to the Timers White Paper found here
http://www.ifirobotics.com/docs/time...004-jan-14.pdf) the following formula should give you what you want:
# of ticks = (time elapsed) * 10^7s / (prescale factor)
As you said above, with a 16 bit timer, counting to 65535, we can measure 6.5535 ms (considering the fact that the internal clock is 10MHz). What happens is that this duration is far too small to use for real applications, so then we used a prescale factor. In this case, we're using one of 8. This means that the timer will only increment once every eight ticks, therefore it will overflow after 52.428 ms. So that is where and how you got this value.
OK up to that point i think we can agree on. Now, we use the formula above to find the # of ticks we need to get a nice even interval of 25 ms (= 0.025 s):
# of ticks = (time elapsed) * 10^7s / (prescale factor)
# of ticks = (0.025) * 10^7s /(8)
Using the last equation we find that we need 31250 ticks to do this. Since it is easier to look at an overflow condition in the timer, we will preload the timer with a value that will cause this overflow after 31250 ticks. To get this value we subtract 31250 from the 16-bit timer’s maximum value (65535) and get 34285.
Therefore we get
65535 (timer's max value of ticks) -
31250 (number of ticks we need) =
34285 (preload value).
This is the value we will preload our timer with. In the code written above you will see 34285 represented in hexadecimal as 0x85ED. Which pretty much explains why the following lines are written the way they are:
TMR1H = 0x85; // sets Timer1's most significant byte
TMR1L = 0xED; // sets Timer1's least significant byte
So, if for example you wanted to work a different time interval (50 ms, let's suppose) use the formula to find out the number of ticks you will need, subtract that value from 65535 and convert the result of that into hexadecimal. That will get you your preload value.
Hope this helps...
__________________
1382 Tribotec Team - Brazil
-----------------------------------------------
2005 NJ Regional GM Industrial Design Award
2005 NJ Regional Website Excellence Award
2004 NJ Regional Semifinalists
-----------------------------------------------