Quoting Richard Prosser : > A couple of comments > 1. It is better to update the timer by adding a correcting value rather > than loadoing it. This way it doesn't matter at all how many clocks have > passed before the update. > 2. With some micros, the latency can vary slightly Granted latency can vary - but keep in mind I'm doing this in MPSIM right now, and I doubt this accounts for latency. Especially since it's EXACTLY one clock pulse off every other interrupt. Same goes for adding it - as far as the simulator is concerned, loading the timer will take the same amount of time no matter what. But I will keep this in mind when trying this out for real. Is adding better because I don't have to wait for X amount of cycles before the timer updates?? (I think this is what you were referring to, but couldn't tell for sure). , so either you need to > ensure that the mico is interrupted from the same point each time (eg a > sleep mode) or you may need to introduce a variable delay to ensure you are > hitting the same point in your ISR at the same clock reading. This is only > really applicable if you are concerned with the exact timing of an output > pulse - so doesn't apply here. > > Overall - Add a value to the Timer, rather than load it each time. > > Richard P > > > > > > Jai Dhar > TERLOO.CA> cc: > Sent by: pic Subject: [PIC]: Timer1 and > Interrupt > microcontroller > discussion list > T.EDU> > > > 19/03/03 14:59 > Please respond to > pic > microcontroller > discussion list > > > > > > > Hi, > > I have been trying to sort out this problem for the last 2 days, and have > done nothing to successfully do so - hopefully you guys will be able to > help > me out. Basically, what I'm building is an alarm clock... simple. I figured > I > would use Timer 1 on an f877 to generate an interrupt every 500mS (couldn't > do > one second since 16 bits couldn't hold it, even with a prescaler). All the > interrupt would be responsible for is incrementing the second register, and > consequently the minute and hour bla bla ... the rest of the functionality > would be done outside of the interrupt (As in outputting the values etc). > Since it interrupts every 500ms, I need a flag to tell whether it's in the > 0 - > 0.5 second range, or 0.5 - 1 second so I can know when to increment the > second > register. I hope I'v explained this much well... so anyway, the check is > very > simple. If it is in the 0.5 - 1 second range (ie: back half of one second), > I > don't bother continuing in the interrupt since a whole second hasn't > elapsed > yet - I just called 'retfie'. If it's on the first half of the second, then > and only then do I increment and so on... my problem (finally) is that the > time between the first half interrupt and second half interrupt aren't > even.. > they vary by one instruction cycle... and this is because of the check for > the > flag. If I remove that check, it interrupts every 500mS as it should (I am > checking all this with the stopwatch in MPSIM Btw). What doesn't make sense > to > me is that I set the timer running BEFORE I do the test, so regardless of > the > test, the time should already be running. It shouldn't matter if I return > from > the interrupt one instruction later, or 20! At least in my understanding... > anyway, that's my long explanation, hopefully you guys can see my obvious > flaw > which I can't. My ISR is below: the main code of my program as of now is > very > simple, it's just looping endlessly. And I know I do'nt have any 'context > saving' implemented yet in my ISR, but it's coming. I just wanted to get > this > working first... > > isr > ;Stop the Timer and reload with value 0BDF (value I have calculated to > work > with 500mS) > T1Stop ;Macro to stop the timer > banksel TMR1H > movlw 0x0B > movwf TMR1H > banksel TMR1L > movlw 0xDF > movwf TMR1L > ;Clear the T1 Interrupt Flag so it doesn't interrupt again when we exit. > T1IFClear > > ;Start Timer. > T1Start > ;Now that Timer 1 has started, we can work out other code. > > ;First test to see if Timer 1 is in the 0.5 - 1 sec range (_SECONDBIT will > be > set), or 0 - 0.5 sec. > ;Toggle the bit first, and then continue if it is set (meaning it was > previously cleared). > ;This will be our mask to toggle the first bit. > movlw b'10000000' > banksel flagreg > xorwf flagreg,f > ;Test _SECONDBIT - skip if it's set, meaning it was clear before. > btfss _SECONDBIT > retfie > ;Increment the seconds register since code is at first half of second. > banksel seconds > movlw 0x01 > addwf seconds,f > > ;I will add the other registers in here etc... once I get this working > > retfie > > > Thank you all again! > > > > ---------------------------------------- > This mail sent through www.mywaterloo.ca > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > ---------------------------------------- This mail sent through www.mywaterloo.ca -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads