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

Setting up timers reference?

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



Joined: 08 Sep 2003
Posts: 16

View user's profile Send private message

Setting up timers reference?
PostPosted: Tue Jan 27, 2004 10:03 am     Reply with quote

I am using a 18F242 and need to setup a internal timer to interrupt every 16ms. I am having trouble figuring out how to configure a timer to do a 16ms interrupt. Is there a good reference I can use that will show the math on how to set up the Timers on a PIC? Thanks in advance.
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Tue Jan 27, 2004 12:00 pm     Reply with quote

First you need to understand what a prescaler and post scaler are as well as the overflow value.

There is one instruction cycle for every 4 cycles of the oscilator or clock.

A prescaler value of 1 means that one instruction cycle increments the timer once. (1:1 prescaler)
A prescaler value of 4 means that four instruction cycles increments the timer once. (4:1 prescaler)
A prescaler value of 16 means that sixteen instruction cycles increments the timer once. (16:1 prescaler)

In the CCS manual prescaler is refered to as mode.

A postscaler of 1 means that one overflow will trigger an interupt.

An overflow occurs when incrementing past the period of the counter. Timer 1 has a period of 2^16(65536)
Timer 2 has an adjustable period of from 1 to 255.

To solve your problem of achieving a 16mS interupt you first must know how many instruction cycles occur in 16mS.

This should be enough information for you to setup a spreadsheet using the available options. Once you have seen your options you can simply pick the best one.
wmeade



Joined: 08 Sep 2003
Posts: 16

View user's profile Send private message

Thanks Neutone
PostPosted: Tue Jan 27, 2004 9:36 pm     Reply with quote

I created a spread sheet for timer 1. If I got it right the following will be true:

With 20MHz Clock

setup_timer_2(T2_DIV_BY_16,255,16) will interrupt every 13.056mSec
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Thanks Neutone
PostPosted: Wed Jan 28, 2004 9:49 am     Reply with quote

wmeade wrote:
I created a spread sheet for timer 1. If I got it right the following will be true:

With 20MHz Clock

setup_timer_2(T2_DIV_BY_16,255,16) will interrupt every 13.056mSec


That would be

16 * 256 * 16 = 65536 Instruction cycles

At 20Mhz 1 instruction cycle is 1/20Mhz * 4 = 0.2uS

0.2uS * 65536 = 13.1072 mS

Thats not quite the 16mS you were looking for. Maybe you could have the interupt fire every 8mS and then run the fuction you need every other interupt. At least you know your options.
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