6
\$\begingroup\$

I need to add an Interrupt Service Routine (ISR) to existing code in C for a PIC18F4620 and a PIC18F46K22. As you can see in section 9 (of both datasheets), these devices have multiple interrupt vectors: 0x0008 for high priority interrupts, 0x0018 for low priority interrupts.

I'd like to have an answer which is useful in many cases, so the question would be: how can I make a function on a specific program memory address? For the ISR, this address would be 0x0008 or 0x0018.

If it's of any use, I'm using the MPLAB C18 compiler, v3.44.

Related: C coding design - function pointers?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ For the record, there is no generic way in standard C to allocate code at a specific address. You have to use compiler/linker-specific ways, such as #pragmas. \$\endgroup\$
    – Lundin
    Commented Feb 25, 2013 at 15:15

1 Answer 1

9
\$\begingroup\$

You'll find the information you need in the compiler's manual: MPLAB C18 C COMPILER USER'S GUIDE.

From page 37 of the user's guide:

MPLAB C18 does not automatically place an ISR at the interrupt vector. Commonly, a GOTO instruction is placed at the interrupt vector for transferring control to the ISR proper. For example:

void lowPriorityISR(void);

#pragma code low_vector=0x18
void interrupt_at_low_vector(void) { 
    _asm GOTO lowPriorityISR _endasm 
}
#pragma code

#pragma interruptlow lowPriorityISR
void lowPriorityISR() {
}

When implementing a high priority ISR, you cannot use the interrupthigh pragma as you would expect. You'll have to use the interrupt pragma instead, as is described here.

If you can change the compiler: using interrupts is easier in the Microchip XC8 or HI-TECH PICC compilers.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Had to copy the code to my project anyway :-) \$\endgroup\$
    – user17592
    Commented Feb 23, 2013 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.