Multiple frequencies using PIC timers

Thread Starter

RG23

Joined Dec 6, 2010
304
I am using pic18f87J90 which has four timers

is there any way I can get stable frequency from any?

in the ISR I use the foloowing instructions to go to the timer subroutines

btfsc INTCON,2
goto TMR0L_int

btfsc PIR1,0
goto TMR1_int ; yes
btfsc PIR1,1
goto TMR2_int
btfsc PIR2,1
goto TMR3_int

But the frequency just does not remain steady?

Is this the only way to access the timers?

What is the use of multiple timers in PIC if we cannot create steady
frequencies from each timer?

The time spent in executing one timer subroutine affects the other timer subroutines and hence no steady frequency.

is there any way out?
 

MrChips

Joined Oct 2, 2009
30,769
If you wish stable frequencies for a PIC you should use a frequency crystal connected the the OSC1 and OSC2 pins.
 

panic mode

Joined Oct 10, 2011
2,731
What is the use of multiple timers in PIC if we cannot create steady frequencies from each timer?
timers are simply counters. they count number of pulses. if the clock frequency is not stable, neither delay obtained through timers can be stable. PIC comes with variety of oscillator options. internal clock is cheap as it does not require external components but it is not nearly as stable as crystal oscillator. as MrChips already stated, if you need repeatability and precision, use crystal.
 

John P

Joined Oct 14, 2008
2,026
If the issue is this:
"The time spent in executing one timer subroutine affects the other timer subroutines and hence no steady frequency"

I'm afraid that no, there is no way out of this, if you need to execute code at several fixed frequencies. You must see that if the timers are all independent, you risk a situation where it is the right time to execute code for more than one of them at once, and the processor cannot do that.

What you could do is designate one timer as the most important one, and it would always have a regular execution time. The others wouldn't be as accurate. Or could you set up a PIC to control external clocks which would each be independent? But then all you could do would be to drive logic outputs, not run code in the PIC.
 

THE_RB

Joined Feb 11, 2008
5,438
Sorry JohnP, but I disagree. The page I linked to has simple code examples that will generate any freq from within the interrupt.

The code can be repeated multiple times, to generate multiple *exact* frequencies simultaneously, all from the one simple interrupt. I once coded an 8voice polyphonic synth using one TMR0 interrupt using the same principle. :)
 
Top