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.
Use Multiple interrupt in PIC?
Re: Use Multiple interrupt in PIC?
Hello joh.nguyen
In your code below, (Which I rearranged for making it more understandable for me)
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
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++;
}
}
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!
Gotta catch them all!
-
- Posts: 30
- Joined: 17 Jul 2013 16:35
Re: Use Multiple interrupt in PIC18F4620?
Hi MaGik,MaGiK wrote:Hello joh.nguyen
In your code below, (Which I rearranged for making it more understandable for me)
You can't put two interrupts and expect them to work without any order.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++; } }
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
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,
Re: Use Multiple interrupt in PIC?
Hi joh.nguyen
I can't help you much in this area.
Best Regards
Sorry, but I'm uncertain of what you mean.joh.nguyen wrote:Please tell me some news if having.
I'm not quite sure if it can be done or not. I've never used those to be honest.joh.nguyen wrote:could I use high priority and low priority in this case?
I can't help you much in this area.
Best Regards
My hobby is collecting MikroElektronika products.
Gotta catch them all!
Gotta catch them all!
Re: Use Multiple interrupt in PIC?
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
- aleksa.jovanovic
- Posts: 526
- Joined: 30 Jun 2015 08:48
Re: Use Multiple interrupt in PIC?
Hi,
What happens when you try to use the both interrupts?
Best regards,
Aleksa
What happens when you try to use the both interrupts?
Best regards,
Aleksa