PIC Timer1 Calculations

Thread Starter

sairfan1

Joined May 24, 2012
103
hi,

i'm learning timer for PIC MCU 16F877A with 10MHz osc.

i want know formula to calculate max delay possible with timer1,
how can i calculate initial values so that i get desired one second delay
 

ErnieM

Joined Apr 24, 2011
8,377
Start by looking at the data sheet:



(((clock/4)/PreScale)/Count)

(((2.5MHz/4)/8)/65,536) = 4.768... Hz, or 0.210... times a sec.

The rate is the clock rate divided by 4, or 10MHz /4 = 2.5MHz

You can slow this down with the divide by 2, 4, or 8 prescaler. Using the /8 divider yields a 2.5MHz/8 = 312,500 KHz clock.

A 16 bit counter will wrap around every 2^16 = 65,536 counts.

This clock will overflow the counter at a rate of 312,500 KHz / 65,536 counts or 4.768... times a second, or every 1/4.768 = .210... seconds.

That's the max. delay you can get, and it's not an even number.

Timer2 is a much better choice for an even tick rate.
 

Attachments

takao21203

Joined Apr 28, 2012
3,702
Use a 12.288 MHz or 9.830.500 Hz crystal- these result in integer results with suitable divider value.

12.228 MHz runs best with timer 0 interrupt, and 1:16 prescaler. /4096, results in 750 ticks/second.

I have a reel of these here, only figured out about this after 3 years.

They are perfect to generate 1 seconds timebase.

Sometimes you'd use an external 32 KHz watch crystal, especially when you want to save battery power. Otherwise these 12.288 Mhz crystals are fine, or also 9.830.400 Hz.

There is no reason to employ complicate correction algorithms, which are needed for instance when using 8 MHz crystals.
 

ErnieM

Joined Apr 24, 2011
8,377
There is no reason to employ complicate correction algorithms, which are needed for instance when using 8 MHz crystals.
I use XX.000 MHz crystals all the time and never have the need for any "complicate correction algorithms" by my use of Timer2 to set the rate.

Using an odd frequency crystal is fine and well if your design can tolerate such. As mine is running a USB bus I'm constrained as to the crystal value used.
 

takao21203

Joined Apr 28, 2012
3,702
I use XX.000 MHz crystals all the time and never have the need for any "complicate correction algorithms" by my use of Timer2 to set the rate.

Using an odd frequency crystal is fine and well if your design can tolerate such. As mine is running a USB bus I'm constrained as to the crystal value used.
It is complicated at least in assembler. A little easier in C.

Yes of course with USB you need a 4MHz or 12 MHz crystal.

I had these crystals around for a long time actually, was unaware they can be used for timebase. Normally I use 32 KHz crystals then. I wrote correction algorithm sometimes, but recently, not anymore. It is just not required.
 

ErnieM

Joined Apr 24, 2011
8,377
The language doesn't matter as the hardware inside Timer2 allows you to choose the numeric divisor so you're not stuck to a power of 2. I typically set it to 250, with pre and post scalers my 12MHz clock triggers an interrupt exactly every 1mS with hardware only, no code beyond clearing the ISR flag.
 

takao21203

Joined Apr 28, 2012
3,702
Hmm interesting I have not yet examined the Timer2 very much.

Anyway, having these 12.28 crystals + using Timer0 gives me a suitable timebase for many purposes (such as display refresh, testing keys).
 

Thread Starter

sairfan1

Joined May 24, 2012
103
ErnieM i'm using PIC16F877A and i go through datasheet but could not find any help regarding calculating initial values for Timer2

I know how to calculate initial values for Timer0 but as Timer2 has two scalars (pre/post), kindly advise how can i achieve 1ms delay.

thanking you in advanced.
 

JohnInTX

Joined Jun 26, 2012
4,787
Attached is an Excel sheet that computes TMR2 for midrange. Its not real spiffy but I find it sufficient. Over the years I've done sheets for all the timers and more but this will get you started. I would post them as well but have to strip out a lot of proprietary stuff. But, figure out how these things work ONCE then make a tool.

No fair commenting on the Excel itself. I do the min to get the job done..

Good luck.
 

Attachments

Thread Starter

sairfan1

Joined May 24, 2012
103
im working with PIC16F877A with 10MHz osc, i configured timer1 as below for 100mS delay, and it looks like working fine.

Prescale: 1:8
TMR1L : EE
TMR1H : 85 (34,286)

being beginner need your feedback if i'm wrong, what are the other things i should keep in mind.
 
Last edited:

Brownout

Joined Jan 10, 2012
2,390
(((clock/4)/PreScale)/Count)

(((2.5MHz/4)/8)/65,536) = 4.768... Hz, or 0.210... times a sec.

The rate is the clock rate divided by 4, or 10MHz /4 = 2.5MHz
Haven't you divided by 4 twice on the first line? You started with 10MHz/4 = 2.5MHz, then divided by 4 again?????
 
Top