Page 1 of 1
16 bit timer1 formula?
Posted: Sun Jan 13, 2013 8:21 am
by brandonb
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
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 2:11 pm
by medelec35
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
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 2:22 pm
by brandonb
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
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 2:35 pm
by brandonb
So If you want to calculate Preloaded Value then:
Preloadedvalue = 65563 - Fosc / (4 x InterruptFrequency x Prescaler)
is the 65563 ment as 65535?
can you break the math down, im not getting the right answers?
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 2:45 pm
by Enamul
Hi
is the 65563 ment as 65535?
I think he is trying to write 65536 (2^16) not 65563
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 2:57 pm
by medelec35
brandonb wrote:is the 65563 ment as 65535?
can you break the math down, im not getting the right answers?
Sorry its a typo. Yes your correct its wrong.
It should be 65536 (and not 65563 or 65535)
Since used copy and paste (The lazy way

) both ended up wrong
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

Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 3:07 pm
by brandonb
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
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 10:37 pm
by medelec35
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
Re: 16 bit timer1 formula?
Posted: Sun Jan 13, 2013 11:47 pm
by brandonb
martin, i dont know whats wrong, hypotheticly speaking i enter 1+1 in calulator and get three, will try again tommorrow

Re: 16 bit timer1 formula?
Posted: Mon Mar 25, 2013 3:30 am
by brandonb
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
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
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
Re: 16 bit timer1 formula?
Posted: Mon Mar 25, 2013 4:24 am
by brandonb
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)
{
}
}
Re: 16 bit timer1 formula?
Posted: Wed Mar 27, 2013 6:28 pm
by Gary Freegard
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
Re: 16 bit timer1 formula?
Posted: Thu Mar 28, 2013 3:27 am
by brandonb
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
Re: 16 bit timer1 formula?
Posted: Thu Mar 28, 2013 10:43 am
by Gary Freegard
Hi Brandon
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
When 23 is added to the last three the output is what would be expected.
Gary