Maker Pro
Maker Pro

PIC Multiple Timer Interrupt Problem

saneeshforu

Feb 23, 2014
5
Joined
Feb 23, 2014
Messages
5
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..

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


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:

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Have you forgotten to set the PR2 register?

What PIC device are you using?
 

kpatz

Feb 24, 2014
334
Joined
Feb 24, 2014
Messages
334
Based on your T2CON and T1CON settings, timer1 is overflowing before timer2, so its interrupt flag is set before your timer2 interrupt fires.

Disabling the interrupt (TMR1IE) only stops the interrupt from occurring, it doesn't stop the TMR1IF interrupt flag from getting set.

Use this to get around it:

Code:
if (PIR1.TMR1IF) {
    PIR1.TMR1IF=0;
    if (PIE1.TMR1IE) {
      cnt++ ;
      TMR1H = 0x80;
      TMR1L = 0x00;
   }
  }
 
Last edited:

saneeshforu

Feb 23, 2014
5
Joined
Feb 23, 2014
Messages
5
thanks for your consideration and time ...actually i forget to use TMR1ON in TCON reg..now it is working
 
Top