Electronic – PIC18 xc8 compiler : how to resolve warning (335) unknown pragma “code”

cinterruptsmicrocontrollerpicxc8

I'm trying to compile this code to blink led with button interrupt, using xc8 compiler and PIC18F4550. I got those warning, so the code lines are ignored and the program doesn't work properly (by clinking on button nothing happens)

newmain.c:45: warning: (335) unknown pragma "code"
newmain.c:46: warning: (335) unknown pragma "interrupt"
newmain.c:65: warning: (335) unknown pragma "code"

program code

#define _XTAL_FREQ 4000000
#include <pic18f4550.h>

// BEGIN CONFIG
#pragma config OSC = HS

static int cpt = 1;

void IntExternal_INT(void) {
    TRISB0 = 1; // PORT B0 as input
    INT0E = 1;
    INTCONbits.PEIE = 1; //enable periphyrical interrupts 
    INTCONbits.GIE = 1;
    INTEDG0 = 0; //: Interrupt Edge Select bit : 1 = Interrupt on rising edge of RB0/INT pin
    //  0 = interrupt on falling edge
    INT0F = 0;
}

void delay() {
    volatile int i, j;
    for (i = 0; i < 2000; i++)
        for (j = 0; j < 10; j++);
}

#pragma code isr = 0x08 // Store the below code at address 0x08
#pragma interrupt isr  // let the compiler know that the function isr() is an interrupt handler

void iscr(void) {
    cpt++;

    if (INT0IF) //If External Edge INT Interrupt
    {
        LATDbits.LATD0 = 1; // RD-0 to High
        LATDbits.LATD1 = 1; // RD-1 to High
        delay();
        LATDbits.LATD0 = 0; // RD-0 to LOW
        LATDbits.LATD1 = 0; // RD-1 to LOW
        delay();
        INT0IF = 0; // clear the interrupt
    }
}    
#pragma code // Return to the default code section

void main(void) {
    IntExternal_INT();
    TRISD = 0xF0; // PORT B Setting: Set all the pins in port D to Output.
    while (1) {
        if (cpt % 2 == 0) {
            delay();
            LATDbits.LATD0 = 1; // RD-0 to High
            LATDbits.LATD1 = 1; // RD-1 to High
            delay();
            LATDbits.LATD0 = 0; // RD-0 to LOW
            LATDbits.LATD1 = 0; // RD-1 to LOW
        }
    }
}

Best Answer

Have you read the XC8 user's guide? Section 5.9 deals with interrupts.

In there it states:

The function qualifier interrupt (or __interrupt) can be applied to a C function definition so that it will be executed once the interrupt occurs. The compiler will process the interrupt function differently to any other functions, generating code to save and restore any registers used and return using a special instruction.

and:

An interrupt function must be declared as type void interrupt and cannot have parameters. This is the only function prototype that makes sense for an interrupt function since they are never directly called in the source code.

It then goes on to give an example:

int tick_count;

void interrupt tc_int(void)
{
    if (TMR0IE && TMR0IF) {
        TMR0IF=0;
        ++tick_count;
        return;
    }
    // process other interrupt sources here, if required
}

The compiler itself handles inserting the code into the right location in the vector table to call the ISR.

By default it uses the high priority interrupt vector. To specify the low priority vector instead, insert the attribute low_priority:

void interrupt low_priority tc_int()