PIC microcontroller interrupt service routine

The interrupt service routine has a special definition method: void interrupt ISR(void); the function name "ISR" in it can be changed to any legal combination of letters or numbers, but its entry parameter and return parameter type must be "void", That is, there are no entry parameters and return parameters, and there must be a keyword "interrupt" in the middle.
       The interrupt function can be placed anywhere in the original program. Because the keyword "interrupt" has been declared, the PICC will automatically locate it at the 0x0004 interrupt entry when the code is connected at the end to realize the interrupt service response. The compiler will also implement the return instruction "retfie" of the interrupt function.
 
        A simple interrupt service demonstration function is as follows:

void interrupt ISR(void)  //interrupt service routine

if (T0IE && T0IF) //judge TMR0 interrupt

{  

T0IF = 0; //Clear TMR0 interrupt flag //Add TMR0 interrupt service here
  }

if (TMR1IE && TMR1IF) //Determine TMR1 interrupt T

MR1IF = 0; //Clear TMR1 interrupt flag
                 //Add TMR1 interrupt service here
        }
} //End of interrupt and return
 
PICC will automatically add code to protect the interrupted site, and automatically restore the site when the interrupt is over, so programmers do not need to add extra instruction statements for interrupted site protection and recovery like writing assembler programs.
However, if some global variables need to be modified in the interrupt service routine, whether to protect the initial values ​​of these variables will be determined and implemented by the programmer himself.
The principle of high efficiency must be followed when writing an interrupt service program in C language:
1. Keep the code as short as possible, and the interrupt service emphasizes the word "fast".
2. Avoid using function calls within interrupts. Although PICC allows other functions to be called in an interrupt, in order to solve the problem of recursive calls, this function must be exclusively dedicated to the interrupt service. That being the case, it is advisable to write the code originally written in other functions directly in the interrupt service routine.
3. Avoid doing math inside interrupts. Mathematical operations will likely use library functions and many intermediate variables. Even if there is no problem of recursive calls, a lot of overhead will be required to protect and restore these intermediate temporary variables at the entrance and exit of the interrupt, which will seriously affect the efficiency of interrupt services. .
 
There is only one interrupt entry for mid-range PIC microcontrollers, so there can only be one interrupt service function in the entire program.

Guess you like

Origin blog.csdn.net/yangshuodianzi/article/details/9099467