interrupt, xc8, pic18

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

I am coming from AVR. In AVRs, I can have multiple interrupt source in different interrupt function.
Code:
ISR(Pin_Change_Vector_vect)
{
   //do stuff;
}

ISR(ADC_Vector_vect)
{
   //do stuff;
}
ISR(Timer1_Vector_vect)
{
   //do stuff;
}
So in pic18 with xc8 complier, I am under the impression I am only allow one interrupt function.
Code:
void interrupt some_interrupt(void)
{
  if (Timer1_interrupt_flag) {
  //do stuff;
  return;
  }

  if (ADC_interrupt_flag) {
  //do stuff;
  return;
  }

  if (Pin_change_interrupt_flag) {
  //do stuff;
  return;
  }
}
But Can I do something like these?
Code:
void interrupt Pin_Change_interrupt(void)
{
    if (ping_change_flag)
    //do stuff;
}

void interrupt Timer1_interrupt(void)
{
    if (timer1_flag)
    //do stuff;
}

void interrupt ADC_interrupt(void)
{
    if (ADC_flag)
    //do stuff;
}
Thanks a lot guys!!
 

tshuck

Joined Oct 18, 2012
3,534
AVR has a vectored interrupt configuration, where PIC has one or two (depending in the applicability of hardware prioritized interrupts).

In general, there is only one location the PC is loaded with when an interrupt is fired, from there, the processor checks interrupt sources in turn, according to how your ISR is set up.

That is one of the major drawbacks of PICs, in my opinion, though it is rarely much of an issue.

Edit: So your code would likely be of the form:
Code:
void interrupt ISR(void)
{
   if (ping_change_flag)
   {
       //do stuff;
   }
   if (timer1_flag)
   {
     //do stuff;
   }
   if (ADC_flag)
   {
     //do stuff;
   }
}
 

JohnInTX

Joined Jun 26, 2012
4,787
To add to what tshuck noted, PIC18s can be used in a priority interrupt mode which has 2 vectors (0008h and 0018h). To use the priority mode you 1) provide 2 interrupt routines, one for high and one for low priority and 2) again how tshuck shows, combine all of your interrupt flag test-service routines into one or the other prioritized interrupt service routine. The compiler will sort out the context saving but to make it all work, your code 3)must set IPEN (interrupt priority enable - usually in RCON) and assign high or low priority to each active interrupt (in the various IPRx registers).
INTCON works a bit differently in the priority mode - bit 40h is not the global enable for low priority interupts (GIEL) and 80h is the global enable for ALL interrupts (high and low prio).

Other than that, have at it. The general admonition to keep PIC interrupt services short goes double for high prio interrupts. Also, keep them simple to avoid long context switches/code duplication because the PIC doesn't support re-entrancy.

The compiler may advise you that it is handling fast-stack errata issues. Good, let it. Be glad you don't have to.

Have fun!
 
Top