51 MCU register group settings

Publisher:csw520Latest update time:2017-01-05 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.







    













    

Reference address:51 MCU register group settings

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

A simple oscilloscope design based on 51 single chip microcomputer
With the development of electronic communication and teaching, the application of oscilloscope is becoming more and more extensive, and its role in teaching is becoming more and more important. Oscilloscope can measure the amplitude, frequency and waveform of the signal, but the high-precision oscilloscope is very exp
[Test Measurement]
A simple oscilloscope design based on 51 single chip microcomputer
Zero basics: learn 51 microcontroller timer
  When we learn microcontrollers, the first thing we learn is LED flashing, which is done with a delay program. Looking back now, this is not very appropriate. Why? Our main program makes the lights flash, so we can't do other things. Well, can the microcontroller only work like this? Of course not, we can use a tim
[Microcontroller]
Zero basics: learn 51 microcontroller timer
51 MCU Design Scheme TOP10 (Part 9)
Design of optical power meter based on 51 single chip microcomputer    Abstract: The photoelectric sensor converts the light signal to be measured into an analog signal, and the analog signal is processed and analyzed by AD to obtain the parameter characteristics of the light signal and output it through serial port
[Analog Electronics]
51 MCU Design Scheme TOP10 (Part 9)
51 single chip microcomputer (AT89C52) one key multi-function (short press, long press)
Short press (press for less than 3s) once, the digital tube display will increase by 1, and the running light count will increase by 1 at the same time. When long pressing (pressing duration is greater than 3s), the digital tube display increases by 1 every 500ms, and the running light counts by 1 every 500ms. #in
[Microcontroller]
51 MCU Development Series 5_Matrix Key Scanning
In embedded systems, the most commonly used input device is the key. The user's application requirements can be transmitted to the system software through the corresponding key, and the software in turn completes the user's request to achieve simple human-computer interaction. Here, the author gives a brief introducti
[Microcontroller]
51 MCU Development Series 5_Matrix Key Scanning
51 microcontroller answering device design
mission details A company is responsible for the design of a answering machine system for a answering competition. The requirements are as follows: ① Three players can participate in the competition at the same time. Their numbers are 1, 2, and 3. Each player uses a answering button. The number of the button is the sa
[Microcontroller]
51 microcontroller answering device design
Compile HEX microcontroller files using c51 command line
The command line process is to compile the source file into OBJ, then link the OBJ, and then convert it to HEX file. That's it, very simple.  C51.exe compiles the c file, BL51.EXE links the OBJ, and OH51.EXE converts the file into HEX. Note: Don't forget to copy the header file to the C51INC library directory for e
[Microcontroller]
3-8-way buzzer based on 51 single chip microcomputer
Specific implementation functions Specific functions: (1) The host resets the game and displays the scores of the eight contestants in sequence. The host can press the start button only after the scores of the eight contestants have been displayed. (2) The host presses the answer start button, and the answerer can
[Microcontroller]
3-8-way buzzer based on 51 single chip microcomputer
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号