Matters needing attention in developing and writing MCU interrupt program in Chengdu
Matters needing attention in developing and writing MCU interrupt program in Chengdu
There are many interrupt resources in the single chip microcomputer, such as external interrupt, timer interrupt, serial port interrupt, etc. These interrupt resources should be used to deal with the corresponding emergency events, rather than the routine events executed in sequence, so great attention should be paid to writing the interrupt subroutine.
The specific transaction statements should not be placed in the interrupt function. The statements in the interrupt subroutine should be concise and clear, and there should not be too many statements. If you write a large series of instructions to deal with specific transactions when writing interrupt subroutines, as in the main function, it will take up too much time in this interrupt. If other low priority interrupt signals come, it may also lead to interrupt loss and program logic error. Therefore, the instruction statement in the interrupt subroutine should be the shortest.
The statements in the interrupt subroutine shall not use the delay function, and shall not use instructions with wait delay function such as while, do while, do until, etc. The use of while, do while, do until and other instructions in the interrupt subroutine will also cause the MCU to delay or wait too long at this position during execution. When other interrupt signals come, the interrupt may also be lost.
So what should we do?
When writing the interrupt subroutine, we should skillfully use the global state variable, only change the value of the state variable in the interrupt subroutine, judge the value of the state variable inside the while (1) program section in the main function, and then execute the corresponding transaction processing sentences respectively according to the value of the state variable. It's like when you receive a call asking you to do something, you can't finish it all the time. You need to write it down, hang up and arrange time to deal with it.
For example, a serial port touch screen needs to perform corresponding operations according to the keys in the touch screen, and serial port interrupt needs to be used. Assuming that the touch screen will send data a when touching key 1 and data B when touching key 2, we can define a global status variable command:
unsigned char command;
In the serial port interrupt subroutine, change the command value according to different serial port data received:
Void serial port 1 interrupt function ()
{
Switch (received serial port data)
{case a: / / if the action of pressing key 1 is received, the command is assigned x1
{command=x1;
break;
}
Case B: / / if the action of pressing key 2 is received, command assigns x2
{command=x2;
break;
}
…
default: break;
}
}
In the above interrupt subroutine of serial port 1, switch is a condition selection instruction. When the serial port receives data a, assign X1 to the command, and when the serial port receives data B, assign X2 to the command. Of course, the above program section is only a schematic illustration, which can not be directly copied and compiled into the single chip microcomputer. In the actual project, it needs to be developed according to the writing rules of different single chip microcomputer programs.
Then execute different transaction statements in the main function according to the value of command:
Main function ()
{
…
while(1)
{
Switch (command)
{casex1: / / if the action of pressing key 1 is received, execute the program of transaction 1
{transaction 1;
break;
}
Casex2: / / if the action of pressing key 2 is received, execute the program of transaction 2
{transaction 2;
break;
}
default: break;
}
}
}
According to such a program, when the single chip microcomputer receives the serial port data representing different keys sent by the touch screen, it changes the value command of the global state variable in the serial port interrupt subroutine. This statement is few and the execution time is short. Then go to the main function to execute specific time-consuming transaction handlers according to different values of command. Such program allocation will make the single chip microcomputer run efficiently and reasonably.