Multiple frequencies using PIC timers

Discussion in 'Embedded Systems and Microcontrollers' started by RG23, Mar 2, 2012.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    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?
  2. MrChips


    Oct 2, 2009
    If you wish stable frequencies for a PIC you should use a frequency crystal connected the the OSC1 and OSC2 pins.
  3. panic mode

    Senior Member

    Oct 10, 2011
    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.
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    Check this web page;

    It describes very simple code to make any frequency you want from any timer and any XTAL frequency. It can also be repeated in code to make a number of simultaneous and independent frequencies, all from the same timer.
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    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.
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    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. :)