PDA

View Full Version : Adding a Value to Timer1



SteveB
- 6th June 2006, 16:50
I’ve got a couple of questions about adding a value to a timer (particularly TMR1 on the 18F4620). Looking in the datasheet for examples or info only yielded a case of setting bit 7 of TMR1H while it was running. This was not very helpful in my case. (If I missed something in the datasheets, point me in the right direction!)

Here is the code I am looking at:


bcf T1CON,TMR1ON ; Turn off timer
movlw LOW(TimerConst) ; 1 instruction cycle
addwf TMR1L,F ; 1 instruction cycle
movlw HIGH(TimerConst); 1 instruction cycle
addwfc TMR1H,F ; 1 instruction cycle
bsf T1CON,TMR1ON ; 1 instruction cycle - Turn TIMER1 back on



1) Do I need to really need to stop the timer first?
2) If I do stop the timer, do I need to account for the instructions starting and/or stopping the timer? If so, do I add 5 or 6 to TimerConst?

My guess is yes, I need to stop the timer, or else on execution of the add, I will loose 1 increment of the timer (probably only with the add to TMR1L though). Also, I need to add 5 to TimerConst to account for all the instruction cycles between turning the timer off and on.
I was also thinking I could get away with keeping the timer running, and just add 1 to TimerConst to account for a lost cycle. Not knowing for sure exactly how the the add and timer increment will interact, I just don't know how well this will work.

BTW, this code taken from the elapsed timer demo in Darrel Taylor’s Instant Interrupts (credit where credit is due, thanks Darrel).

Steve