16 bit timer1 formula?
Moderator: Benj
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
16 bit timer1 formula?
i was playing with tmr1 frequencies and was wondering is there a math formula to arrive at a desired freq? like in avr where with their 16 bit timer can say osc/prescaler/desired frequency = ocr1a
file below cycles 30hz to khz
file below cycles 30hz to khz
- Attachments
-
- tmr1 16 bit adjust attempt.fcf
- (14.5 KiB) Downloaded 332 times
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: 16 bit timer1 formula?
Hi Brandon,
The basic fromula for timer 1 Interrupt Frequency is:
InterruptFrequency = Fosc/(4 x Prescaler x (65536-PreloadedValue))
So If you want to calculate Preloaded Value then:
Preloadedvalue = 65536 - Fosc / (4 x InterruptFrequency x Prescaler)
Martin
The basic fromula for timer 1 Interrupt Frequency is:
InterruptFrequency = Fosc/(4 x Prescaler x (65536-PreloadedValue))
So If you want to calculate Preloaded Value then:
Preloadedvalue = 65536 - Fosc / (4 x InterruptFrequency x Prescaler)
Martin
Last edited by medelec35 on Sun Jan 13, 2013 3:02 pm, edited 1 time in total.
Reason: Typo with 65563. Should be 65536.
Reason: Typo with 65563. Should be 65536.
Martin
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
thanks martin, im amazed at how easy you come up with equasions, that doesnt come natural for me
on this flowchart i was looking at the datasheet and seen those tmr1h and l registers so i gave it a go, is this the best way of doing this

-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
is the 65563 ment as 65535?So If you want to calculate Preloaded Value then:
Preloadedvalue = 65563 - Fosc / (4 x InterruptFrequency x Prescaler)
can you break the math down, im not getting the right answers?
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: 16 bit timer1 formula?
Hi
I think he is trying to write 65536 (2^16) not 65563is the 65563 ment as 65535?
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: 16 bit timer1 formula?
Sorry its a typo. Yes your correct its wrong.brandonb wrote:is the 65563 ment as 65535?
can you break the math down, im not getting the right answers?
It should be 65536 (and not 65563 or 65535)
Since used copy and paste (The lazy way


I have edited my post.
An example:
Fosc = 4MHz
Deisered Interrpupt frequency = 12.57Hz
Prescaler = 2
Preloadedvalue = 65536 - Fosc / (4 x 12.57 x 2)
= 65536 - 4000000/100.56
= 65536 - 39777.24741
=25759 (rounding up)
Edit: Enamul is correct. In my mind was 36, but my fingers had a diffent idea

Martin
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
ok, i wanted to make sure, i get a slightly different frequency, it might be the way i did the flowchart, i havent slept since yesterday, so i wanted to make sure
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: 16 bit timer1 formula?
I just altered your flowchart to work on 16F877A with Clock Speed set to 19660800Hz
I'm aiming for 100Hz so trying with prescaler of 2
So
Preloaded value= 65536-19660800/(4 * 100 *2)
=40960
So count was set to 40960 then compiled to chip.
If all is working then frequency measured should be 1/2 the actual frequency since o/p state is toggled on each interrupt.
Result using E-blocks test pod: As you can see frequency measured = 49.950Hz
x 2 = 99.9Hz
Which is very close indeed!
Martin
I'm aiming for 100Hz so trying with prescaler of 2
So
Preloaded value= 65536-19660800/(4 * 100 *2)
=40960
So count was set to 40960 then compiled to chip.
If all is working then frequency measured should be 1/2 the actual frequency since o/p state is toggled on each interrupt.
Result using E-blocks test pod: As you can see frequency measured = 49.950Hz
x 2 = 99.9Hz
Which is very close indeed!
Martin
- Attachments
-
- tmr1 16 bit adjust attempt.fcf
- (14.5 KiB) Downloaded 341 times
Martin
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
martin, are there speed limitations on the timer1 or should i change the code... here is a calculator that i created from your equasion, from top to bottom prescaler 1,2,4,8 http://eng-serve.com/pic/pic_timer.html which is does
here is flowchart that should cycle 50Khz here is the logic scope shot where it only does 44.5Khz , is this a limit of tmr1 or do i code it differently
running th calculations for 50Khz i come up with the numbers on the top line when simulate, to conferm this is correct i checked on this site as well for match here is flowchart that should cycle 50Khz here is the logic scope shot where it only does 44.5Khz , is this a limit of tmr1 or do i code it differently
- Attachments
-
- tmr1 logic capture.jpg (88.11 KiB) Viewed 9394 times
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
i rewrote the code in another compiler just for kicks and its the same

Code: Select all
unsigned int i;
void interrupt()
{
if(pir1.TMR1IF && pie1.TMR1IE)
{
tmr1h=255;
tmr1l=96;
lata|=1<<5;
for(i=0;i<2;i++){}
lata&=~(1<<5);
pir1. TMR1IF=0;
}
}
void main()
{
osccon=0xf0;//32Mhz
ansela=anselb=0;
trisb=trisc=255;
trisa=0;
lata=0;
intcon.PEIE=1;
t1con.T1OSCEN=1;
t1con.TMR1ON=1;
t1con.TMR1CS1=0;
t1con = t1con & 0x0F;
intcon.GIE=1;
pie1. TMR1IE=1;
while(1)
{
}
}
-
- Posts: 45
- Joined: Mon Nov 07, 2011 6:36 pm
- Has thanked: 1 time
- Been thanked: 30 times
Re: 16 bit timer1 formula?
Hi Brandon
I too have had similar problems in the past. All I end up doing is adjusting the timer value unto I get what I want.
I use this program http://pictimer.picbingo.com/.
I ran a version of your program on a 18F2455, using the program above I set tmr1h to 255 and tmr1l to 16 and the output was 45.6 KHz.
I then adjusted the timer1 values in the program(above), so that the interrupt frequency was 45.6KHz , the new value for timer1 was 23 lower.
I then added that onto tmr1l to make it 39, and the result is 50.0KHz output.
With high frequency timer interrupts I know you have to take the interrupt latency into account which is 3/4 cycles. Plus it takes 2
cycles to write to the timer. This explains a change of 6 for the timer but not the additional 17
I also tried a low frequency (200Hz), tmr1h = 21 and tmr1l = 183 and the output was 100.00Hz
Hope this helps
Gary
I too have had similar problems in the past. All I end up doing is adjusting the timer value unto I get what I want.
I use this program http://pictimer.picbingo.com/.
I ran a version of your program on a 18F2455, using the program above I set tmr1h to 255 and tmr1l to 16 and the output was 45.6 KHz.
I then adjusted the timer1 values in the program(above), so that the interrupt frequency was 45.6KHz , the new value for timer1 was 23 lower.
I then added that onto tmr1l to make it 39, and the result is 50.0KHz output.
With high frequency timer interrupts I know you have to take the interrupt latency into account which is 3/4 cycles. Plus it takes 2
cycles to write to the timer. This explains a change of 6 for the timer but not the additional 17

I also tried a low frequency (200Hz), tmr1h = 21 and tmr1l = 183 and the output was 100.00Hz
Hope this helps
Gary
-
- Posts: 438
- Joined: Mon Aug 29, 2011 12:26 am
- Location: arizona
- Has thanked: 175 times
- Been thanked: 173 times
Re: 16 bit timer1 formula?
gary, thanks for conferming this is normal... i thought i was missing something because it was so far off, figured the whole point of a timer interrupt was that it was solid, the 8 bit versions are
-
- Posts: 45
- Joined: Mon Nov 07, 2011 6:36 pm
- Has thanked: 1 time
- Been thanked: 30 times
Re: 16 bit timer1 formula?
Hi Brandon
Glad to be of help to you.
Just carried out some other tests, Actual Freq should be half of desired:
Gary
Glad to be of help to you.
Just carried out some other tests, Actual Freq should be half of desired:
- tmr1h___tmr1l____Desired Freq____Actual Freq
255_____244_____1MHz__________No output
255_____232_____500KHz ________127.7KHz
255_____208_____250KHz_________84.5KHz
255_____16______50KHz _________22.8Khz
253_____179_____ 20KHz _________9.79Khz
Gary