What should be paid attention to when dealing with the interrupt service routine of PIC microcontroller

The interrupt system in the single-chip microcomputer is the best way for Electronic engineers to solve emergencies and multi-task real-time processing, and mastering the application of interrupt technology is a necessary ability for a qualified electronic engineer.

The interrupt system in the single-chip microcomputer is the best way for electronic engineers to solve emergencies and multi-task real-time processing, and mastering the application of interrupt technology is a necessary ability for a qualified electronic engineer.

A significant difference between PIC and 51 series microcontrollers is that PIC has only one interrupt entry address (04H), while 51 has multiple interrupt sources. In this way, for the PIC, no matter what kind of interrupt occurs, it will automatically transfer to execute the program statement at 04H. In order to distinguish which kind of interrupt occurs, it must be determined and transferred to the corresponding interrupt by judging the interrupt flag bit in the interrupt service routine. in the service program. When an interrupt occurs, the breakpoint address is first pushed into the hardware stack, and some on-site parameters must be saved by the user through software. The registers that must be saved include the W register, the high-order byte PCLATH of the program register, and the status flag register STATUS, etc. Others The registers used in both the interrupt service routine and the main program must also be saved, otherwise unpredictable errors may occur. If there are too many on-site parameters that need to be saved, there will be two disadvantages: one is to waste RAM resources, especially for application systems with tight RAM resources; the other is to increase the running time of the interrupt service routine, which leads to the real-time performance of the interrupt system. If it becomes worse, it even affects the realization of normal functions. For example, using the CCP module in the microcontroller to measure the high-speed pulse width requires that the running time of the interrupt service routine be as short as possible, otherwise it will lose the opportunity to capture and cause errors. Therefore, it is suggested that the registers used in the main program should not be used as much as possible in the interrupt service routine, and the registers in the interrupt should be set as dedicated, which saves the protection and recovery procedures of these registers and reduces the probability of errors.

What should be paid attention to when dealing with the interrupt service routine of PIC microcontroller

Another note is to try to avoid using subroutines with multiple levels of nesting in the interrupt service routine. I have encountered such problems in the actual application process, and I have not been able to find the root cause of the problem for a long time. The hardware stack of the PIC mid-range microcontroller is 8 levels. If a subroutine with 6 levels of nesting is called in the main program, when the main program executes to the innermost subroutine, 6 levels have been used in the 8 stack. , if an interrupt occurs at this time, the breakpoint is pushed into the stack, and seven addresses are saved in the stack at this time, and if there is a second-level nested subroutine in the interrupt service routine, the stack will overflow, This will inevitably cause the program to not return to the original address correctly, thus causing the program to deviate. Because the occurrence of this situation needs to meet certain conditions, that is, the main program is interrupted when it runs to the innermost subroutine, so there is great uncertainty, and there may be no problems at all during the product development and debugging stage. When the product has been used for a long time by the user, it begins to be exposed, but at this time, it has already caused a very bad impact on the image of the product in the user’s mind, and the loss is irreversible. So as a developer you should pay special attention.

Another problem is also worth noting, that is to avoid opening the global interrupt enable bit in the interrupt service routine, because the PIC microcontroller does not support interrupt nesting, otherwise, after executing one interrupt service routine and entering another interrupt service routine, The last interrupted field parameter will be corrupted, causing the program to generate unpredictable errors.

The Links:   LQ121S1LG42 G101UAN020 GETCOMPONENT