16 bit timer1 formula?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

16 bit timer1 formula?

Post 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
Attachments
tmr1 16 bit adjust attempt.fcf
(14.5 KiB) Downloaded 301 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: 16 bit timer1 formula?

Post 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
Last edited by medelec35 on Sun Jan 13, 2013 3:02 pm, edited 1 time in total.
Reason: Typo with 65563. Should be 65536.
Martin

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: 16 bit timer1 formula?

Post by Enamul »

Hi
is the 65563 ment as 65535?
I think he is trying to write 65536 (2^16) not 65563
Enamul
University of Nottingham
enamul4mm@gmail.com

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: 16 bit timer1 formula?

Post 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 :P ) both ended up wrong :oops:

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 :lol:
Martin

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: 16 bit timer1 formula?

Post 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:
timer1 100Hz.png
(140.25 KiB) Downloaded 4275 times
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 285 times
Martin

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post by brandonb »

martin, i dont know whats wrong, hypotheticly speaking i enter 1+1 in calulator and get three, will try again tommorrow :lol:

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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
tmr1 calculator.fcf
(12.53 KiB) Downloaded 267 times
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
tmr1 test.fcf
(12.5 KiB) Downloaded 259 times
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
tmr1 logic capture.jpg (88.11 KiB) Viewed 8514 times

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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)
  {

  }

}

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: 16 bit timer1 formula?

Post 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

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: 16 bit timer1 formula?

Post 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

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: 16 bit timer1 formula?

Post 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

Post Reply