Everyone knows that the 51 MCU has four groups of registers R0-R7. In the past year, when I was communicating with novice friends, I found that many friends often had problems using the register group. Although this is not a difficult problem, if an error occurs, it will also cause serious consequences.
First, let me introduce the register group of 51:
the register group can be switched arbitrarily by setting the 3rd and 4th bits of the PSW register. Before entering the interrupt, switching the register group can easily protect the data of the original register group from being destroyed by the statements in the interrupt, which is very convenient.
RS1 RS0 Byte address
0 0 Register 00H~07H
0 1 Register 1 08H~0FH
1 0 Register 2 10H~17H
1 1 Register 3 18H~1FH
RS1=PSW.4 RS0=PSW.3
There are three common errors:
1. The 0th register group is specified for the interrupt function.
C program: void int0() interrupt 0 using 0
The compiled assembly is as follows:
PUSH ACC PUSH
B
PUSH DPH
PUSH DPL
PUSH PSW
MOV PSW,#0x00
. . . . . .
Because the main() function uses the 0th register group, the interrupt program will change the data of the register group. When the main program is running, an interrupt may occur at any time. When the interrupt returns to the main program, the value of register R0-R7 has been changed. This is a very serious error. Moreover, the fault sometimes occurs and sometimes does not, and the error is inexplicable.
2. Interrupt priorities are different, but register group numbers are the same
C program: void int0() interrupt 0 using 1 // low priority interrupt
void T0() interrupt 1 using 1 // high priority interrupt
Because high priority interrupts can interrupt the low-level interrupt being executed and switch to high-level interrupts. This is called interrupt of interrupt. Like the first error, the program of high-level interrupt will change the register being used by the low-level interrupt.
3. Do not write using. Strictly speaking, this is not an error. But it is a very bad habit.
C program: void int0() interrupt 0
The compiled assembly is as follows:
PUSH ACC
PUSH B
PUSH DPH
PUSH DPL
PUSH PSW
MOV PSW,#0x00
PUSH 0x00
PUSH 0x01 PUSH 0x02 PUSH
0x03 PUSH 0x04 PUSH 0x05 PUSH 0x06 PUSH 0x07 . . . . . . Without using to specify the register group, the compiler will allocate the 0th group of registers by default, and then use 8 statements to save R0-R7 of the 0th group to the stack. When exiting the interrupt, 8 pop stacks are required. This "clumsy" writing method occupies 32 bytes of program space and 8 bytes of stack. Perhaps the advanced version of the compiler will be improved. It is better to be more disciplined when working on microcontrollers. Experience summary: 1. When writing interrupt programs, you must use the using statement to specify the register group. Groups 1, 2, and 3 are all OK, but not 0. 2. The interrupt of the 51 microcontroller has two priorities. An interrupt will not interrupt another interrupt of the same priority. In this way, interrupts of the same level can use the same group. For example: low-priority interrupt functions all use using 1, and high-priority interrupts all use using 2. This will not conflict. The following is a normal example: C program: void int0() interrupt 0 using 1 The compiled assembly is as follows: PUSH ACC PUSH B PUSH DPH PUSH DPL PUSH PSW MOV PSW,#0x08 . . . It 's that simple. Although this is not a problem for experienced users, many newbies make this mistake. I have encountered no less than 15 of them. I wrote this post today to remind all newbies. It is better to prevent a problem before it happens than to put out fires everywhere.
Previous article:[51 MCU Study Notes FOUR]----16*16LED dot matrix
Next article:[51 MCU Study Notes FIVE]----Independent Buttons
Recommended ReadingLatest update time:2024-11-16 13:05
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Power supply overcurrent and overvoltage protection
- First make an adapter for the parallel interface of the first-generation development board
- Regarding the issue of modifying the BLUENRG-LP routine to realize the chip collecting internal voltage values and transmitting them to another Bluetooth serial port for printing via Bluetooth
- Op amp specification notes
- Variable frequency power supply components: What is the main function of the variable frequency power supply filter capacitor?
- Analysis of the application of TVS tube in the design of mobile phone surge protection
- ST FAE Answer: Reference circuit for replacing BlueNRG's balun BALF-NRG-02D3 with discrete devices
- Analysis of embedded real-time operating system uCOS II
- About atsaM E54 fuse position problem
- [RISC-V MCU CH32V103 Review] Using EXIT Interrupt