I think you're way over-doing this in the first place.
So, what you want is a program to start counting when you hit one button, and stop counting when you hit a second button, and display the time interval between the 2 right?
some generic code (you fill in the crap with real code)
Code:
'Set up all variables, registers, timers, etc... No interrupts
'display start message
'wait for start button to be hit, if it's not hit, don't jump back to the lcdout display command
'takes time to display and might miss a button push in the meanwhile
'if the button is hit, reset tmr1 to zero and start the timer
'start a loop
'display the current count (which will take longer than 1ms, but the next bunch will take care of that, again, the problem with displaying stuff during a tight count is that you will miss the exact time that a button is pushed and the count could possibly be off by 1 or 2 ms)
'if the tmr1 value is above 1000, add 1 to the ms counter and subtract 1000 from tmr1 (3 from tmr1h and 232 from tmr1l, or subtract 4 from tmr1h and add 24 to a subcounter to cover the extra 24us. when that subcounter exceeds 1000, add another one to the ms counter
'check the stop button
'if the stop button is not pushed, jump back to the loop
'if the stop button is pushed, immediately save the tmr1 count, do the 'over 1000' math to keep the ms count and us count, and display the total
No, not real code, an outline as a guide.
Interrupts are nice, and DT interrupts and the Olympic timer are all you need, but apparently it's not enough to have almost 100% pre-written code made just for you.
This code is based on the fact that you keep timer1 BELOW a certain value and keep track of it as it counts UP. That way if you miss a count or two, you can always recheck it (i.e. tmr1 is 3000, you've missed 3ms, no biggie, just subtract 1000 from it 3 times and you're back on track). I built an OBD2 reader that's based off this to drive my virtual odometer, integrates time and speed. In the process of reading the OBD2 message, I miss a count here and there. No problem, just let the counter increase in the background and cover it up later. The intervals are short enough that I get a nice accurate reading (within 1/10 mile over a 263 mile drive 2 weeks ago) and long enough that the timer won't overflow in between readings.
Make any sense yet?
Bookmarks