Several issues that should be paid attention to when using the interrupt function of C51, Author: guyepiaoyu date
16:27:00 Reprinted from: http://blog.21ic.com/user1/531/archives/2006/16909.html recently in the Shrimp Pond around, found some small shrimp on the C51 interrupt function some do not understand, today's weekend, take the time to send a technical post, hope to be helpful to small shrimp, if you have any mistakes, please correct them!
The format of the interrupt function of C51 is:
Void funcir (void) interrupt X [using Y]
Some analyses of sleepwalking are as follows:
1. the interrupt function is a special function with no parameters or return values. But does the program allow the use of return? The answer is yes, but only "return;" can be used, but not "Return (z);"; used in some places that require quick return. The corresponding assembly has multiple RET statements, relatively high efficiency.
Ii. Using can modify any function. However, we recommend that you modify the interrupt function only. In short, "using" specifies the working register group, interrupt functions are usually urgent, and sometimes a statement is overconcerned, so using to switch register groups can save some stack pressure actions, because 51 has only two levels of interruption, the same-level interrupt cannot be interrupted. Therefore, we can set the same-level interrupt to the same register group. In a sense, there is a set of registers that are redundant. In addition, we recommend that you use the using keyword for the interrupt function.
3. Call a function in an interrupt. First, we need to discuss the necessity of calling a function in an interrupt function. I have discussed this issue with others on the Forum the day before yesterday. Now I am still using this idea: in some cases, it is necessary to call a function during interruption. In this case, it is similar to a common function. First, if this function is called multiple times, it is even more necessary to include some parameters or something. Someone called me the day before yesterday, saying that if only one call is required and no parameter is returned, the function should be written directly, because if a function is used, at least two call and RET statements will be added. I don't agree. I actually found that when your program is complicated, you pull the part separately to form a function, the code and time may be better.
4. It is recommended that you do not call a function that is called during an interruption by other functions that are not interrupted, because a "repeated call" warning may occur, and sometimes such a call is fatal, some people say that this function can be modified using reentrant. Yes, it can be solved in this way, but I do not recommend this. This may reduce the stack space for you, in addition, the optimization of the entire program is much worse. In this case, I suggest writing this function twice and dividing it into two functions for separate calls.
5. some inexplicable problems may occur when the function is interrupted and some data is incorrect. In fact, it is generally caused by the use of absolute registers in the Assembly. Some people say that the interrupt function uses the register group and which register group is used for the function called by the interrupt function. I think this is not good, this will increase the additional consumption. Using using will add the following statement:
Push psw
MoV psw, # xx
....
Pop psw
More importantly, the using function cannot return values, which is fatal.
Two recommended methods are available:
1. Use "# pragma noaregs" to disable absolute registers.
2. Use "# pragme Rb (x)" to specify the working register group of this file
6. In general, interrupt functions are required to be as short as possible, but there are also special circumstances. In some front/back-end systems, it will put a lot of relatively important things into the scheduled interrupt (this scheduled interrupt is similar to the clock cycle in the real-time operating system), and the program is very long. I just want to tell you that the interrupt function is also a function. As long as the system is necessary, you can do seemingly unreasonable things, just like a GOTO statement.