About C51 interrupt programming

Publisher:温暖的微风Latest update time:2015-02-04 Source: laoguKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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; 6 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 interrupt here shares an interrupt vector. The interrupt vector table of 8051 is as follows:
Interrupt SourceInterrupt 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 procedure is implemented by using the interrupt keyword and the interrupt number (0 to 31). The interrupt number indicates 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 to the stack and pop them 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 interrupt calling processes must use the same specified register group, otherwise parameter passing will fail. Therefore, you need to be flexible in choosing using.

About using:
You said in the article that "the disadvantage of this approach is that all interrupt calling processes must use the same specified register bank." 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 bank 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 the 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 to specify, at the entrance of the ISR, C51 selects register bank 0 by default, which is equivalent to the entrance 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 using different register banks can be interrupted.
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 more 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 using using or disabling interrupts (#pragma disable) 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; while high-priority interrupts must use a different set from low-priority interrupts, because it is possible that a high-priority interrupt may occur in a low-priority interrupt. The compiler will automatically determine when absolute register access can be used.
3. When calling other functions in the ISR, the same register set must be used as the interrupt. When the NOAREGS command is not used to make an explicit statement, the compiler will use absolute register addressing to access the register set selected by the function (that is, specified by using or REGISTERBANK). When the register set assumed by the function is different from the one actually selected, unpredictable results will occur, which may result in parameter passing errors and the return value may be in the wrong register set.
For example: when the same function needs to be called inside and outside an interrupt, assuming that according to the program flow control, there will be no recursive call of the function, will there be any problems with such a call? If you are sure that reentry 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 uses using to specify other BANKs), the called function must be placed in:
#pragma NOAREGS
#pragma AREGS
control parameter pair, specifying that the compiler should not use absolute register addressing mode for this function; or you can also select "Don't use absolute register accesses" in Options->C51 to make all codes not use absolute register addressing mode (this will slightly reduce execution efficiency). In either of the above situations, 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, you can copy the function and replace it with a different function name. This 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. For
detailed usage, please refer to the C51.PDF file. The above is for reference.

Keywords:C51 Reference address:About C51 interrupt programming

Previous article:High-speed interface between C8051 and SRAM
Next article:C51 absolute address access

Recommended ReadingLatest update time:2024-11-17 02:59

Design of OLED control system based on C8051F020 and USB
1 Introduction Compared with traditional LCD screens, OLED display modules have the characteristics of high brightness, high contrast, wide viewing angle, fast response speed, and low power consumption. Therefore, with the development of highly integrated electronic products, OLED display modules are wide
[Microcontroller]
Design of CAN bus intelligent node based on C8051F550
The CAN bus was first designed by the German company BOSCH to solve the monitoring and control system of automobiles. This paper briefly introduces the characteristics and composition of the CAN bus . The working process of the master node and the construction method of the slave node hardware interface ci
[Microcontroller]
Design of CAN bus intelligent node based on C8051F550
8051 MCU development environment construction under Ubuntu 16.04
Due to work requirements, I am preparing to learn MCU, and I am also preparing to learn embedded Linux in the future. Recently, I am fed up with the automatic updates of Win10, so I gave up the Windows environment and prepared to start from 51 MCU in Linux environment, learning both MCU and Linux. I am a novice in b
[Microcontroller]
Using AT24Cxx to expand C8051F EEPROM
System functions Use AT24Cxx to expand C8051F EEPROM. hardware design Using AT24Cxx to expand C8051F EEPROM circuit schematic software design The following part is copied from TXT and copied to the web page. There are many spaces missing in the code part, so it is a bit messy. Please understand! For detailed
[Microcontroller]
Using AT24Cxx to expand C8051F EEPROM
Create a new Keil C51 project (3)
1. Download, install and register Keil C51 UV3 version software I won’t go into the details of the steps here. 2. Open the Keil C51 working interface, as shown below. 1. Create a new project Click the "Project"---"New Project" option in the menu bar, and the "Create New Project" dialog box will pop up as sh
[Microcontroller]
Create a new Keil C51 project (3)
About C51 assembly ADD and SUBB instructions
When I was writing about the PID algorithm today, I suddenly realized that the deviation can be positive or negative. So how should it be represented? Of course, it is represented by the eighth bit of a byte. This is a no-brainer. Will the ADD and SUBB instructions recognize them? This is a question. So
[Microcontroller]
C51 microcontroller pointer example
#pragma src #include reg51.H f(){} f1(){} f2(){} main() { { int x; int *px; //The following representations are annoying, but the generated code But it is extremely concise: //Assign xdata type pointer 0x4000 to px px=(int xdata *)0x4000; //It means taking a char from xdata 0x4000 and giving it to x x=*((char xdata *
[Microcontroller]
C51 MCU Learning Notes (Part 3) - Key control of lights, buzzers and other devices
1. The principle of independent buttons A general independent button has four pins. Regardless of whether the button is pressed or not, pins 1 and 2 are always connected, and pins 3 and 4 are also connected. When the button is pressed, pins 1 and 2 are connected to pins 3 and 4. If the button is pressed and held, it w
[Microcontroller]
C51 MCU Learning Notes (Part 3) - Key control of lights, buzzers and other devices
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号