# PIC Timer1 Calculations

Discussion in 'Embedded Systems and Microcontrollers' started by sairfan1, Mar 7, 2013.

1. ### sairfan1 Thread Starter Member

May 24, 2012
46
1
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

2. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

• ###### Timer1.GIF
File size:
12.8 KB
Views:
1,091
absf and sairfan1 like this.
3. ### takao21203 Distinguished Member

Apr 28, 2012
3,578
463
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.

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.

sairfan1 likes this.
4. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

sairfan1 likes this.
5. ### takao21203 Distinguished Member

Apr 28, 2012
3,578
463
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.

6. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
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.

absf, sairfan1 and takao21203 like this.
7. ### takao21203 Distinguished Member

Apr 28, 2012
3,578
463
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).

8. ### sairfan1 Thread Starter Member

May 24, 2012
46
1
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.

9. ### JohnInTX Moderator

Jun 26, 2012
2,393
1,051
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.

• ###### PIC_Timer2.zip
File size:
8.5 KB
Views:
148
sairfan1 likes this.
10. ### sairfan1 Thread Starter Member

May 24, 2012
46
1
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: Mar 11, 2013
11. ### masosi Member

Dec 23, 2007
11
1
sairfan1 likes this.
12. ### Brownout Well-Known Member

Jan 10, 2012
2,375
998
Haven't you divided by 4 twice on the first line? You started with 10MHz/4 = 2.5MHz, then divided by 4 again?????