Connect with us

PIC Multiple Timer Interrupt Problem

Discussion in 'Microcontrollers, Programming and IoT' started by saneeshforu, Feb 23, 2014.

  1. saneeshforu

    saneeshforu

    5
    0
    Feb 23, 2014
    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: Feb 23, 2014
  2. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,266
    Nov 28, 2011
    Have you forgotten to set the PR2 register?

    What PIC device are you using?
     
  3. kpatz

    kpatz

    247
    52
    Feb 24, 2014
    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: Feb 24, 2014
  4. saneeshforu

    saneeshforu

    5
    0
    Feb 23, 2014
    thanks for your consideration and time ...actually i forget to use TMR1ON in TCON reg..now it is working
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-