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
Posted: Tue Jan 27, 2004 12:00 pm
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
Thanks Neutone
Posted: Tue Jan 27, 2004 9:36 pm
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
Re: Thanks Neutone
Posted: Wed Jan 28, 2004 9:49 am
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.
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