Use Multiple interrupt in PIC?

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
joh.nguyen
Posts: 30
Joined: 17 Jul 2013 16:35

Use Multiple interrupt in PIC?

#1 Post by joh.nguyen » 20 Dec 2013 08:15

Dear all,

I want to implement 2 interrupt sources include Timer 0 and uart Rx. I put them in interrupt function as below

// Interrupt service routine
void Interrupt() {

if (TMR0IF_bit) {
TMR0IF_bit = 0; // clear TMR0IF
TMR0H = 0x67;
TMR0L = 0x5F;
Num++;
if (Num == 1440) { // 90 for crystal 20 Mhz, 18 for crystal 4 Mhz
Num = 0;
asm {
reset
}
}
}

if (RCIF_bit == 1) { // Checks for Receive Interrupt Flag bit
RCIF_bit = 0;
rxarray = UART1_Read(); // Storing read data
i++;
}
}

The program could not be run expectly. However, in case I just used 1 interrupt source at the same time, the program worked well.

Please help me on this.

MaGiK
Posts: 897
Joined: 19 Apr 2013 10:00

Re: Use Multiple interrupt in PIC?

#2 Post by MaGiK » 20 Dec 2013 10:02

Hello joh.nguyen :D

In your code below, (Which I rearranged for making it more understandable for me)

Code: Select all

// Interrupt service routine
void Interrupt() 
{

   if (TMR0IF_bit) 
   {
   TMR0IF_bit = 0;                               // clear TMR0IF
   TMR0H = 0x67;
   TMR0L = 0x5F;
   Num++;
   if (Num == 1440)                              // 90 for crystal 20 Mhz, 18 for crystal 4 Mhz
      {                           
        Num = 0;
        asm 
          {
          reset
          }
      }
   }

   if (RCIF_bit == 1) 
   {                                           // Checks for Receive Interrupt Flag bit
   RCIF_bit = 0;
   rxarray[i] = UART1_Read();           // Storing read data
   i++; 
   }
}
You can't put two interrupts and expect them to work without any order.

Let me put it in example:

Imagine that the program entered your Timer part of the Interrupt Service Routine (ISR), and then it got a UART interrupt before it actually got out of the Timer part.
In that case, your program will neglect the UART interrupt and continue finishing the Timer part of the ISR, which will make your program results neglect that particular UART interrupt results.

Similarly, if your program is executing the UART part of the ISR, and then your Timer overflew, then your program will neglect the timer part of the ISR and finish the UART part, and your program will neglect some results.

I'm not saying that you can't handle two interrupts at a time. It certainly can be done, and I've done it many times, but you must avoid activating the UART interrupt while the program is in your Timer part of the ISR, and vice versa.

I hope you understand your issue. It is quite hard to understand I must say.

Best Regards
My hobby is collecting MikroElektronika products.
Gotta catch them all!

joh.nguyen
Posts: 30
Joined: 17 Jul 2013 16:35

Re: Use Multiple interrupt in PIC18F4620?

#3 Post by joh.nguyen » 23 Dec 2013 11:22

MaGiK wrote:Hello joh.nguyen :D

In your code below, (Which I rearranged for making it more understandable for me)

Code: Select all

// Interrupt service routine
void Interrupt() 
{

   if (TMR0IF_bit) 
   {
   TMR0IF_bit = 0;                               // clear TMR0IF
   TMR0H = 0x67;
   TMR0L = 0x5F;
   Num++;
   if (Num == 1440)                              // 90 for crystal 20 Mhz, 18 for crystal 4 Mhz
      {                           
        Num = 0;
        asm 
          {
          reset
          }
      }
   }

   if (RCIF_bit == 1) 
   {                                           // Checks for Receive Interrupt Flag bit
   RCIF_bit = 0;
   rxarray[i] = UART1_Read();           // Storing read data
   i++; 
   }
}
You can't put two interrupts and expect them to work without any order.

Let me put it in example:

Imagine that the program entered your Timer part of the Interrupt Service Routine (ISR), and then it got a UART interrupt before it actually got out of the Timer part.
In that case, your program will neglect the UART interrupt and continue finishing the Timer part of the ISR, which will make your program results neglect that particular UART interrupt results.

Similarly, if your program is executing the UART part of the ISR, and then your Timer overflew, then your program will neglect the timer part of the ISR and finish the UART part, and your program will neglect some results.

I'm not saying that you can't handle two interrupts at a time. It certainly can be done, and I've done it many times, but you must avoid activating the UART interrupt while the program is in your Timer part of the ISR, and vice versa.

I hope you understand your issue. It is quite hard to understand I must say.

Best Regards
Hi MaGik,

Thanks for your reply. I forgot to mention PIC 18F4620 was used in this case. Please tell me some news if having.

By the way, could I use high priority and low priority in this case?

Regards,

MaGiK
Posts: 897
Joined: 19 Apr 2013 10:00

Re: Use Multiple interrupt in PIC?

#4 Post by MaGiK » 23 Dec 2013 14:43

Hi joh.nguyen :D
joh.nguyen wrote:Please tell me some news if having.
Sorry, but I'm uncertain of what you mean.
joh.nguyen wrote:could I use high priority and low priority in this case?
I'm not quite sure if it can be done or not. I've never used those to be honest.
I can't help you much in this area.

Best Regards
My hobby is collecting MikroElektronika products.
Gotta catch them all!

dhurgham
Posts: 1
Joined: 07 Dec 2014 08:43

Re: Use Multiple interrupt in PIC?

#5 Post by dhurgham » 24 Oct 2015 20:19

plz, anybody can help me ,i have the same problem,i used two interrupts,timer and received uart data ,but the problem is that the program not work ,plzzzzzzz

User avatar
aleksa.jovanovic
Posts: 526
Joined: 30 Jun 2015 08:48

Re: Use Multiple interrupt in PIC?

#6 Post by aleksa.jovanovic » 29 Oct 2015 15:12

Hi,

What happens when you try to use the both interrupts?

Best regards,
Aleksa

Post Reply

Return to “Website & Forums General Discussion”