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

Timer interrupt set at 1ms?

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



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

Timer interrupt set at 1ms?
PostPosted: Wed Jan 25, 2006 4:28 am     Reply with quote

Hi

I would like to setup a timer that will interrupt every 1ms.
I am using a 4MHz crystal.

If I do:
setup_counters(RTCC_INTERNAL,RTCC_DIV_4);

this will interrupt every 976us.

Can someone help me with this please?

Also while in the interrupt function- all that time does not contribute to the timer counter (i.e. the time is lost)?

Thank you
a.
Ttelmah
Guest







PostPosted: Wed Jan 25, 2006 8:43 am     Reply with quote

You cannot generate a 1mSec timer with the RTCC, and a 4MHz crystal. The best solution, is to use a timer that supports adjustable 'endpoints', normally Timer2. Alternatives are, to set the timer 'forward' a known amount on each interrupt. This has been discussed recently in the thread, 'PIC26F84A Timer_0 interrupt running slowly'. The other route, is to chose a crystal, that gives the required division. For instance, the 4.096MHz crystal is a standard part, and the 4MHz PICs,are actually specified to run to this (I asked Microchip, a long time ago). This then gives a nice 1mSec interrupt.
No, time in the interrupt is not 'lost' to the timer, however the count _will_ become in error, if you try to 'set it' to a fixed value inside the interrupt, since the counter will have already advanced, and possibly by a variable amount.

Best Wishes
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Thu Jan 26, 2006 3:16 am     Reply with quote

Hi Ttelmah

Thank you for your suggestions. Before I get the 4.096 Xstal, can you please tell me about Timer2, or direct me to where I can find how to use it to set interupt durations?

Regards
a.
Ttelmah
Guest







PostPosted: Thu Jan 26, 2006 5:55 am     Reply with quote

Depends whether your chip has it!. You don't mention what the chip is. Data sheet...
MicroChip, have an application note about the various timers.
The 'point' about timer2, is that it has a hardware circuit to 'reset' it at a specified count. It can also interrupt on every 'n' counts (where 'n' is a 4 bit counter). So if your chip has it:

setup_timer2(T2_DIV_BY_4,249,10);

Will for a 4MHz clock, divide this by 16 (/4, then /4 in the prescaler), count from 0...249, and interrupt every tenth time through the count, giving an interrupt every 10mSec. Rather nice for timing. :-)
Be careful about the counter value. The 'wizard', gets this wrong, as does the help file. the chip counts from zero _to_ the value specified, and resets on the _next_ cycle. Hence for 250 counts, you need 249, yet the wizard insists on using 250. The MicroChip notes have this right As you'd - hopefully - expect!).

Best Wishes
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Thu Jan 26, 2006 7:40 am     Reply with quote

Hi Ttelmah

Thank you very much- that worked 100%!

Regards
a.
Ttelmah
Guest







PostPosted: Thu Jan 26, 2006 9:30 am     Reply with quote

Thanks for 'coming back'. :-)

Best Wishes
brian
Guest







PostPosted: Tue Feb 07, 2006 1:49 pm     Reply with quote

for a 4 MHZ clock

to get 1ms

setup_timer_2 (T2_DIV_BY_4, 25-1, 10);
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