PIC Multiple Timer Interrupt Problem

Thread Starter

saneeshforu

Joined Feb 22, 2014
8
Haii all I have a problem related with Microc PIC IDE (v8.2)
I have 2 timer interrupts(tmr2,tmr1) in my program..like this..
Rich (BB code):
void interrupt(){
   if (PIR1.TMR1IF) {
 PIR1.TMR1IF=0;
  cnt++ ;
  TMR1H = 0x80;
  TMR1L = 0x00;
   }
else if(PIR1.TMR2IF)
  {
   PIR1.TMR2IF=0;
   cnt2++ ;
   TMR2  =   0;
    }     }
But when Timer 2 Enables (PIE1.TMR2IE = 1) ,both of two interrupts(cnt and cnt2) get incremented ...why ?/

MY WHOLE PROGRAM IS THE BELOW ONE

Rich (BB code):
unsigned short cnt;
unsigned short cnt2;


 void interrupt(){
  
   if (PIR1.TMR1IF) {

 PIR1.TMR1IF=0;
  cnt++ ;
  TMR1H = 0x80;
  TMR1L = 0x00;

   }

else if(PIR1.TMR2IF)

  {
   PIR1.TMR2IF=0;
   cnt2++ ;
   TMR2  =   0;

   }

     }

  void main()
{
  ANSEL  = 0;            // Configure AN pins as digital I/O
  ANSELH = 0;
  TRISB = 0xFF;          // set PORTB to be input
   TRISE = 0;
  TRISD = 0;             // set PORTD to be output
  PORTD = 0x00;
  PORTE=0X00;          // initialize PORTD
           

  T1CON = 1;
  T2CON = 0xFF;               // Timer2 settings
  TMR2  =   0;                // Initialize Timer2 register
   TMR1H = 0x80;               // Initialize Timer1 register
   TMR1L = 0x00;
  INTCON = 0xC0;
  PIE1.TMR2IE = 1;
  PIE1.TMR1IE  = 0;
   PIR1.TMR1IF=0;
  cnt=0;
  cnt2 = 0;
   PIR1.TMR2IF=0;

 for(;;)
 {

 if(cnt==61)

   {
     PORTE=~PORTE;
     cnt=0;
     }
     
   else  if (cnt2==160)
   {
      PIE1.TMR2IE = 0;
      PIE1.TMR1IE  = 1;
      cnt2=0;
      cnt=0;

      PORTD=~PORTD;
    }
     
 }
}
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
Please use code tags when posting code:

Rich (BB code):
void interrupt(){
     if (PIR1.TMR1IF) {
        PIR1.TMR1IF=0;
        cnt++ ;
       TMR1H = 0x80;
       TMR1L = 0x00;
    }
  else if(PIR1.TMR2IF)
  {
      PIR1.TMR2IF=0;
      cnt2++ ;
      TMR2 = 0;
  }
}
Set a breakpoint inside your if (PIR1.TMR1IF) block to be certain that code is being reached.


Also try initializing PIR1.TMR1IF to 0 at the start of your code in main() PIR1.TMR1IF=0;
 

ErnieM

Joined Apr 24, 2011
8,377
Hit the HELP button on your compiler and find the section for "Using MPLAB® Simulator"

Follow the directions there so you can watch while the computer traces thru your code.

Additionally, I doubt the "else if" in your interrupt is correct. It should be a straight "if" statement.
 

JohnInTX

Joined Jun 26, 2012
4,787
In the interrupt routine, don't use if-else, the interrupts are not related and need to be processed as individual events, not one excluding the other. EDIT: As ErnieM said, too.

Since interrupts get disabled/enabled in the code, you have to test the interrupt enable, TMRxIE, as well as the TMRxIF in the service routine. The timers continue to run, setting IF even though the interrupt is disabled. Once in the interrupt routine (from the other timer) that stray IF flag will be processed.

Try something like this:
Rich (BB code):
void interrupt(){

    if (PIR1.TMR1IE)        // process TMR1 interrupt when IE && IF
        if (PIR1.TMR1IF) {
            PIR1.TMR1IF=0;
            cnt++ ;
            TMR1H = 0x80;
            TMR1L = 0x00;
        }

     if(PIR1.TMR2IE)
        if(PIR1.TMR2IF){    // process TMR2 interrupt when IE && IF
            PIR1.TMR2IF=0;
            cnt2++ ;
      //      TMR2  =   0;  unnecessary. TMR2 resets to 00h when it equals PR2
        }

 }//interrupt
Finally, you need to set PR2 in init since TMR2IF is generated on a match of TMR2 and PR2, scaled by the postscaler. Its a good idea even if you expect to use the default reset value. I had to look it up myself.
 
Last edited:
Top