Matters needing attention in the development and writing of interrupt programs for single-chip microcomputers in Chengdu

2020-12-28 18:30:09 admin

Matters needing attention in the development and writing of interrupt programs for single-chip microcomputers in Chengdu


There are many interrupt resources in the microcontroller, such as external interrupts, timer interrupts, serial port interrupts, etc. These interrupt resources should be used to handle corresponding emergency events, rather than regular events that are executed sequentially, so you should pay attention when writing interrupt subroutines .


Specific transaction processing 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 as in the main function when writing interrupt subroutines, it will take too much time in this interrupt, if there are other low-priority interrupt signals coming. It may cause the interrupt to be lost and the logic error of the program occurs. Therefore, the instruction statement in the interrupt subroutine should be the shortest.


The statements in the interrupt subroutine should not use the delay function, and do not use the instructions with the wait delay function such as while, do-while, do-until. Using 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 the execution process, and it may also cause the interrupt to be lost when other interrupt signals come. .


So what should be done?


When writing the interrupt subroutine, you should use the global state variable cleverly. In the interrupt subroutine, only the value of the state variable is changed. In the main function, the value of the state variable is judged inside the while(1) program segment, and then based on the value of the state variable. Execute the corresponding transaction processing statements respectively. It's like you receive a call asking you to do something, and you can't keep the phone call to finish it. You need to write down the matter, hang up the phone and arrange time to deal with it.


For example, a serial port touch screen needs to perform corresponding operations according to the keys on the touch screen, and it needs to use the serial port interrupt. Assuming that the touch screen will send data A when button 1 is touched, and data B when button 2 is touched, then we can define a global state variable command:


unsigned char command;


In the serial port interrupt subroutine, change the value of command according to the received different serial port data:


void serial port 1 interrupt function ()


{


switch (received serial port data)


{case A: //If the action of button 1 is received, command is assigned x1


{command=x1;


break;


}


case B: //If the action of button 2 is received, command is assigned x2


{command=x2;


break;


}



default: break;


}


}


In the above interrupt subroutine of serial port 1, switch is a conditional selection command. When the serial port receives data A, then assign x1 to command, and when the serial port receives data B, then assign x2 to command. Of course, the above program segment is just an illustration, it cannot be copied and compiled directly to run in the single-chip microcomputer. In the actual project, it is necessary to develop according to the writing rules of different single-chip microcomputer programs.


Then execute different transaction processing statements according to the value of command in the main function:


main function()


{



while(1)


{


Switch (command)


{casex1: //If you receive the action of button 1, execute the program of transaction 1


{Transaction Processing 1;


break;


}


casex2: //If the action of button 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 from the touch screen, it changes the value of the global state variable command in the serial port interrupt subroutine. This statement is few and the execution time is short. In the main function, the time-consuming specific transaction processing program is executed according to the different values of command. This kind of program distribution will make the MCU run efficiently and reasonably.