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´s calc?????

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



Joined: 02 Jul 2007
Posts: 9

View user's profile Send private message

timer´s calc?????
PostPosted: Mon Jul 02, 2007 3:15 am     Reply with quote

Hi, can you help´me with timer´s interrupt´s?

Let´s take the next example:
Fosc=20Mhz
Timer0 - Internal and Presc=X

There is any formula to calc when the timer0 will interrupt in mSec. according to X?
for example x=128

and if i "charge" timer0(y)
for example y=50
The formula is equal??????

And for timer1????

what i want????

make a delay with timer0:
On a INT_EXT i charge timer0 with a value(the delay) and enable interrupt of timer0 overflow .
On timer0 interrupt, i connect a device, until next INT_EXT, disconnect the device, charge timer0 with a value(the delay) and enable interrupt of timer0 overflow .
On timer0 interrupt i connect a device, and so on......

But i can´t calc the time when timer0 overflow.......


Thnk´s on advance

Note: sorry my english...............
Ttelmah
Guest







PostPosted: Mon Jul 02, 2007 8:43 am     Reply with quote

The timer calculations are pretty simple.
Master clock.
Divided by the internal /4 prescaler (the 'instruction' clock).

_or_ for timers supporting an external clock, it's frequency value at this point.

Divided by the selected prescaler for the timer.
Divided by the size of the timer's main register (256, or 65536, according to whether this is a 16bit or 8bit timer). _Or_ by a defined value on timers supporting hardware 'count' resets.

Now, 'preloading' (what you are calling 'charging'), is not a good way to work in general. The problem is that the timer is running all the time, while instructions are executing. So if (for instance), you have a timer running off the master instruction clock, and an interrupt is called, the timer may already have 'counted' several dozen counts, before you get to the point where you load the timer, and depending on what else the chip was doing, this count may differ on subsequent calls. This results in long term counting errors...

Solution is to use a timer that allows you to automatically define a reset 'point' (timer2), or use a clock that allows your required 'time' to be a nice binary value (so use a 32768Hz crystal, and then with a prescaler of 128, and a count of 256, you get a nice 1 second 'tick' for example).

Look in the MicroChip application notes, where under 'input/output', 'timer', there are a number of notes about using the timer modules.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Jul 02, 2007 4:45 pm     Reply with quote

Like Ttelmah said, if you try to load a timer with a value it will end up giving you timing errors. If you want to have a timer give you certain delays you need to calculate what result you want and then use the different pre/post scalers to figure out the crystal frequency that will give you the result that you want.

Say, you want to have an interrupt happen every 10ms. Using a 16F76, Timer2, if you use a crystal with a frequency of 19.6608MHZ and use:

setup_timer_2(T2_DIV_BY_16,191,16);

this will cause the interrupt to occur 100 times a second which will give you a 10ms interrupt. If you want something to happen every 100ms then you could have a counter incremented, inside of the ISR, and have it count ten times and set a flag.

You could have different counters being incremented that could set flags in increments of 10ms. If this seems a bit confusing, download the spec sheet and take a look at the block diagrams for the counters. I, myself being a visual oriented person, pictures tell the story much better than words.

Ronald
sf_
Guest







PostPosted: Tue Jul 03, 2007 5:28 am     Reply with quote

Thnk´s for your replies.
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