| View previous topic :: View next topic |
| Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Tue Nov 27, 2012 2:11 pm |
|
|
May be one in 256 times, because the timer0 is 8bit. I didn't think about that.
Thanks! |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Nov 27, 2012 3:45 pm |
|
|
| Quote: | Because you set the timer after clearing the interrupt, once in 65536 times, the timer can wrap on the instruction between the interrupt being cleared, and the timer being reset. This will cause the original problem to come back...
|
I don't understand.
I agree it's better to change the instruction sequence.
In this code, the timer1 ISR occurs at regular intervals, so the problem should occur every time (almost) or not at all.
If the period between timer1 interrupts was random or variable, I can see the problem leading to complete loss of hair.
Mike |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Tue Nov 27, 2012 4:06 pm |
|
|
If the two timers were free running, I'd agree. However remember he is resetting timer0, in the timer1 ISR, and they are using different divisors. If you test this, setting a time, clears the prescaler, and unless the setting instruction happens to be exactly on the divisor multiple, the two timers can 'creep' relative to one another by how much the amount in the prescaler 'differs' at the moment of reset. It is very unlikely to happen, but better to avoid the chance.
Best Wishes |
|
 |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Nov 28, 2012 3:55 am |
|
|
| May be 1 in 256 times becouse timer0 is 8bit. I didn't think about that. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Wed Nov 28, 2012 4:24 am |
|
|
I'm used to the PIC18's most of the time now, where Timer0, is 16bit.
However it can be less frequent than that. Since it is 'prescaler creep', it'll shift by possibly as little as one instruction time, in the product of the counter, and the prescaler.
Best Wishes |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Nov 28, 2012 6:11 am |
|
|
| Quote: | | However remember he is resetting timer0, in the timer1 ISR, and they are using different divisors. If you test this, setting a time, clears the prescaler, and unless the setting instruction happens to be exactly on the divisor multiple, the two timers can 'creep' relative to one another by how much the amount in the prescaler 'differs' at the moment of reset. |
But because he's resetting timer0 inside timer1 ISR I think he's effectively synchronising the two timers (and their prescalers), thus eliminating creep.
As I understand it:-
Timer1 advances after timer1 pre-scaler overflows.
Timer1 ISR is initiated shortly after timer1 overflows.
He's resetting TImer0 inside timer1 ISR, presumably at a constant interval after the timer1 overflow.
So timer1 and timer1 prescaler should be more (or less) exactly the same on each occasion that timer0 (and its prescaler) is reset.
I do agree, the inadvertent ISR is best avoided.
Mike |
|
 |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Nov 28, 2012 7:15 am |
|
|
| Of course it aso depends on the prescaler. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Nov 28, 2012 3:33 pm |
|
|
My point is, it does not depend on prescaler.
Mike |
|
 |
|