- Joined
- Jul 4, 2009
- Messages
- 16,489
- Helped
- 5,157
- Reputation
- 10,347
- Reaction score
- 5,207
- Trophy points
- 1,393
- Location
- Aberdyfi, West Wales, UK
- Activity points
- 139,504
I would be writing the code from scratch so I would factor other uses into the program. I often use timers for more than one purpose though, all you do is trigger an ISR at the shortest time interval the program needs and use software counters to create longer delays from it. For example:
This sets bit 0 of a variable called TimerFlags each time the interrupt occurs and if the value you put in 'Counter' has also reached zero it sets bit 1. The code is over-simplified and in reality you would monitor and reset the bits in the main loop of the program. The idea is that if you loaded 'Counter' with say 25 and TMR0 rolled over every 10mS, bit 0 in TimerFlags would set every 10mS and bit 1 would set every 250mS so you have generated two intervals from one timer. The beauty of this method is you can set up many different 'Counter' variables and load them with different values in the program as needed, you can even change them while the program is running.
Brian.
Code:
InteruptRoutine
clrf INTCON,TMR0IF ; clear the interrupt flag
movlw 0x55 ; reload the timer interval
movwf TMR0
bsf TimerFlags,0 ; indicate the short interval has finished
decfsz Counter,f ; count down for the longer interval
retfie
bsf TimerFlags,1 ; indicate the longer interval has finished
retfie
This sets bit 0 of a variable called TimerFlags each time the interrupt occurs and if the value you put in 'Counter' has also reached zero it sets bit 1. The code is over-simplified and in reality you would monitor and reset the bits in the main loop of the program. The idea is that if you loaded 'Counter' with say 25 and TMR0 rolled over every 10mS, bit 0 in TimerFlags would set every 10mS and bit 1 would set every 250mS so you have generated two intervals from one timer. The beauty of this method is you can set up many different 'Counter' variables and load them with different values in the program as needed, you can even change them while the program is running.
Brian.