The basic structure of the 8051 series MCU includes: 32 I/O ports (4 groups of 8-bit ports); two 16-bit timer counters; full-duplex serial communication; 5 interrupt sources (2 external interrupts, 2 timer/counter interrupts, 1 serial port input/output interrupt), two-level interrupt priority; 128 bytes of built-in RAM; independent 64K bytes of addressable data and code areas. After an interrupt occurs, the MCU goes to one of the 5 interrupt entry points and then executes the corresponding interrupt service
handler. The entry address of the interrupt program is placed in the interrupt vector by the compiler. The interrupt vector is located at the lowest address of the program code segment. Note that the serial port input/output interrupts here share an interrupt vector. The interrupt vector table of 8051 is as follows:
Interrupt source Interrupt vector
---------------------------
Power-on reset 0000H
External interrupt 0 0003H
Timer 0 overflow 000BH
External interrupt 1 0013H
Timer 1 overflow 001BH
Serial port interrupt 0023H
Timer 2 overflow 002BH
Both interrupt and using are keywords of C51. The C51 interrupt process is implemented by using the interrupt keyword and the interrupt number (0 to 31). The interrupt number specifies the entry address of the compiler interrupt routine. The interrupt number corresponds to the enable bit in the 8051 interrupt enable register IE. The corresponding relationship is as follows:
IE register C51 8051
enable bit Interrupt number Interrupt source
--------------------------------
IE.0 0 External interrupt 0
IE.1 1 Timer 0 overflow
IE.2 2 External interrupt 1
IE.3 3 Timer 1 overflow
IE.4 4 Serial port interrupt
IE.5 5 Timer 2 overflow
With this declaration, the compiler does not need to care about the use of register bank parameters and the protection of accumulator A, status register, register B, data pointer and default registers. As long as they are used in the interrupt routine, the compiler will push them onto the stack and pop them off the stack at the end of the interrupt routine. C51 supports all five 8051 standard interrupts from 0 to 4 and up to 27 interrupt sources in the 8051 series (enhanced). The
using keyword is used to specify the register bank used by the interrupt service routine. The usage is: using followed by a number from 0 to 3, corresponding to 4 groups of working registers. Once the working register group is specified, the default working register group will not be pushed onto the stack, which will save 32 processing cycles , because both stacking and popping require 2 processing cycles. The disadvantage of this approach is that all processes that call interrupts must use the same specified register group, otherwise parameter passing will be incorrect. Therefore, for using, you need to be flexible in your use.
About using:
In the text, you said that "the disadvantage of this approach is that all processes that call interrupts must use the same specified register group." Is this what you mean?
For example:
define a function
void func(unsigned char i) {
...
if(++i==0x12) {
...
}
...
}
and there is an interrupt function
void int_0(void) interrupt 0 using 1 {
....
}
In the default state, func uses register group 0 (BANK0). So when int_0 calls func, will there be a parameter passing error when passing parameters?
Thank you!
If registers are used in an interrupt service function (ISR), the use of using must be handled properly:
1. The interrupt service function uses using to specify a register bank different from the main function (the main function generally uses Register bank 0).
2. ISRs with the same interrupt priority can use using to specify the same register bank, but ISRs with different priorities must use different register banks. The function called in the ISR must also use using to specify the same register bank as the interrupt function.
3. If using is not used, at the entry of the ISR, C51 selects register bank 0 by default, which is equivalent to the entry of the interrupt service program first executing the instruction:
MOV PSW #0.
This ensures that the high-priority interrupt is not specified by using. The low-priority interrupt can use different register banks.
4. Use the using keyword to specify the register bank for the interrupt, so that the register bank can be switched directly without a large number of PUSH and POP operations, which can save RAM space and speed up the MCU execution time. In general, the switching of register banks is prone to errors. You must have a clear understanding of the memory usage, and its correctness must be guaranteed by yourself. Especially when there is direct address access in the program, be careful! As for "when to use register bank switching", one situation is: when you try to run two (or more) jobs at the same time, and their scenes need some isolation, it will be used. Registers are very useful in ISR or using real-time operating system RTOS.
Principles of register bank use: 1.
The lowest 32 bytes of 8051 are divided into 4 groups of 8 registers. They are registers R0 to R7. The register bank is selected by the lower two bits of PSW. In ISR, MCU can switch to a different register bank. Access to register banks is not bit addressable. The C51 compiler stipulates that functions that use using or disable interrupts (#pragma d ISA ble) cannot return bit type values.
2. The main program (main function) uses one group, such as bank 0; all interrupts with low interrupt priority use the second group, such as bank 1; all interrupts with high interrupt priority use another group, such as bank 2. Obviously, there is no problem for interrupts of the same level to use the same set of registers, because interrupt nesting will not occur; high-priority interrupts must use a different set from low-priority interrupts, because it is possible for a high-priority interrupt to occur during a low-priority interrupt. The compiler automatically determines when absolute register access can be used.
3. When calling other functions in ISR, the same register bank as the interrupt must be used. When the NOAREGS command is not used to make an explicit statement, the compiler will use absolute register addressing to access the register bank selected by the function (that is, specified by using or REGISTERBANK). When the register bank assumed by the function is different from the register bank actually selected, unpredictable results will occur, which may result in parameter passing errors and return values may be in the wrong register bank.
For example: when the same function needs to be called in and out of an interrupt, assuming that according to the program flow control, there will be no recursive call of the function, will such a call cause problems? If it is determined that reentrancy will not occur, there are two situations:
1. If the ISR and the main program use the same register bank (the main program uses BANK 0 by default, and if the ISR does not use using to specify a register area for it, it also uses BANK 0 by default), no other settings are required.
2. If the ISR and the main program use different register banks (the main program uses BANK 0 by default, and the ISR specifies other BANKs using using), the called function must be placed in:
#pragma NOAREGS
#pragma AREGS
control parameter pair to specify that the compiler should not use absolute register addressing for the function; or you can also select "Don't use absolute register a CC esses" in Options->C51 to make all codes not use absolute register addressing (this will slightly reduce execution efficiency). In either case, the compiler will give a reentry warning, and you need to manually change the OVERLAY parameter to make a reentry description.
3. There is another way: if the code of the called function is not very long, it is better to copy the function and replace it with a different function name. This situation is suitable for ROM with enough extra space.
Therefore, if you are not sure about the use of the using keyword, it is better not to use it and let the compiler system handle it.
Previous article:C51 learning experience, the relationship between pointers and arrays in programming
Next article:A must-read for beginners learning microcontrollers
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Allwinner V5 helloworld example (test your entire development environment) --- lindeni V5 development board
- The charger has a glitch problem when it is powered on
- Memory alignment processing for ARM processors
- Some useful information: RVB2601 development board program download automatic operation and OLED screen protection
- To the netizens who entered the Pingtouge RVB2601 competition (reference materials, technical support, experience sharing, work submission and selection criteria)
- Summary: RCSN experience W806 Lianshengde 9.9 yuan development board
- Hardware Design: Circuit Design of DSP TMS320C5509A
- VaST's latest modeling tool enables comprehensive software development before tapeout
- What are the methods of remote communication? Which manufacturers have used specific products?
- Regarding "Circuit Design Based on Operational Amplifiers and Analog Integrated Circuits", there is a concept of "Pole" that is not clear