CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Timer 0 interrupt doesn't work correct.
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 2:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 3:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 4:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 28, 2012 3:55 am     Reply with quote

May be 1 in 256 times becouse timer0 is 8bit. I didn't think about that.
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Wed Nov 28, 2012 4:24 am     Reply with quote

I'm used to the PIC18's most of the time now, where Timer0, is 16bit. Smile

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

View user's profile Send private message

PostPosted: Wed Nov 28, 2012 6:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 28, 2012 7:15 am     Reply with quote

Of course it aso depends on the prescaler.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 28, 2012 3:33 pm     Reply with quote

My point is, it does not depend on prescaler.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group