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 support@ccsinfo.com

Problem with Timer2 and Timer0 interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mutevaggil



Joined: 29 May 2008
Posts: 8

View user's profile Send private message

Problem with Timer2 and Timer0 interrupt
PostPosted: Sat Jul 26, 2008 4:24 am     Reply with quote

hi everyone. i got a problem.

here is my timer adjustments of timer0:


Code:
 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8|RTCC_8_bit);
 set_timer0(131);
 enable_interrupts(INT_TIMER0);


My system OSC=40 Mhz so osc. frequency is 10 Mhz and so tosc=0,1microsecond.

interrupt is supposed to work every 100 microsecond. i have counter in timer0 func.

Code:
#INT_timer0
void count_it()
{
counter_val++;
set_timer0(131)
}



and counter_val is shown in screen. but this apprx. 50 second results in just a minute. it must show 60 on screen, but not.

is my compute wrong? i dont think so. so what is my problem?

(i tried it with timer2 too, it also failed.)

PS: i use 18f4431


Last edited by mutevaggil on Tue Jul 29, 2008 4:50 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Sat Jul 26, 2008 9:02 am     Reply with quote

Do some searches here.
The problem is fundamentally, that interrupts take _time_. With a counter advancing every 8 instruction times, the timer will probably already have advanced by several counts, before you get to your instruction setting the count. When you then set the timer, this advance is 'lost', so the timer is then seen to run slow.
Preferably, either use a timer that supports resetting at a count in hardware (timer2), or adjust your code so you don't have to reset the timer value. As a 'worst case' solution, _add_ your required offset to the value read from the timer (this can still give errors if the timer increments between the read and the write).
There have been a huge number of threads here about this, and different methods of avoiding problems.

Best Wishes
mutevaggil



Joined: 29 May 2008
Posts: 8

View user's profile Send private message

its done
PostPosted: Tue Jul 29, 2008 4:48 am     Reply with quote

thanks ttelmah,

i listened your advice and did some searches here. you really answered hundreds of time for the same problem. and of them worked for me.

i just kept PR2 short. i mean:

Instead of TIMER2(DIV_BY_1, 124, 8) //100us,

TIMER2(DIV_BY_4,24, 10) //100 us worked very well.

i am writing this here because this may be help any other people who face to same problem.


thanks.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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