Memory mapping of 51 microcontroller (1)

Publisher:神光骑士Latest update time:2022-12-15 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article mainly talks about the memory mapping of the RAM part of the 51 microcontroller. Its memory space is as shown in the figure below:




There are 256 bytes of RAM space inside the 51 microcontroller. The lower 128 bytes are the working register group area (0x00H-0x1FH), bit addressing area (0x02H-0x2FH), general RAM area (0x30H-0x70H), and SFR register area. (0x80-0xFF) This space defines all control registers and status registers of the 51 microcontroller. Next, we will introduce the functions and access methods of each area one by one in the bottom-up order in the above figure.


  1. working register area

The working register area is R0-R7, each of which is 1 byte, but the 51 microcontroller has a total of 4 sets of working registers, and the microcontroller can only have one set of working registers during operation. The addresses of each working register and the selection of working register groups are shown in the figure below:




The function of the working register is to pass function parameters, assign values ​​to local variables, save the results of function execution, etc. In addition, many instructions also use working registers. We can first think about this question: Can the sum of four numbers be realized using a 51 microcontroller? Let’s take a look at the picture below:




The add function receives three parameters a, b, c but no d. Can the parameter d not be passed? Let’s first take a look at the explanation from the official documentation:

Cx51编译器最多在MCU寄存器中传递三个函数参数。由于不将参数写入内存或从内存读取参数,因此该机制显著提高了系统性能。参数或参数传递可以由REGPARMS和NOREGPARMS指令控制:


It turns out that 51 can only pass up to three parameters, but what should I do if it exceeds three now? Can we only add and subtract three numbers? As shown in the figure below, the parameters passed by the add function are 1, 2, 3, and 4 respectively. The result of our operation is A, which is 10 in decimal. So where does the fourth parameter go?


After simulation, we know that the fourth parameter is pushed onto the stack. (It seems that the stack can be in the working register area, but it is best not to do this. Instead, you should let SP point to after 0x60 in the .S file)

Note: The int in the 51 microcontroller is 2 bytes! The default value of the 51 microcontroller SP when powered on is 0x07, i, j, and t are pushed into the stack 0x07+2*3=0x13. The 51 microcontroller stack is growing positively, so the position of the fourth parameter is where 0x14 and 0x15 are.


2. Bit addressing area

0x20-0x2F has a total of 16 bytes and 128 bits, so the addressing range is 0-0x7F. The memory mapping relationship is as shown in the figure below:




The usage method is the keyword unique to C51, and the assembly instruction corresponding to the sbit keyword is BIT. (Note: SFR registers can also be bit addressable!)




The modified keyword sbit points to the location of P2.1. P2^1 is bit-addressed in the SFR register area. The reason why it is 1 is because it has not been assigned a value, so its default value is 1. The bit addressing area is very useful. For example, bit operations on P2.1 will not affect other I/O ports such as P2.0 and P2.2, so it is a very convenient operation method. In fact, STM32 can also achieve this effect!


3. General RAM area

The general RAM area can run the local variables of the function, because the SP pointer protects the calling function and the interrupt function site. The specific performance is:

1. Protection breakpoint: Push PC onto the stack, and after executing the interrupt function, pop up the previous value of PC and restore to the point where the program was running before the interruption.

2. Protect the scene: protect the value of the register group.


4. SFR register area

The special function register area defines the control registers and status registers that control and monitor the operation of the 51 microcontroller. Timers, I/O ports, serial ports, interrupts and other related registers are all defined here. At the same time, this area is discrete and is reserved for manufacturers. However, the 52 microcontroller can access it, but it requires indirect addressing. For C language programming, there is no need to pay attention to this. The C language itself can convert it. . Then, the stack of the 52 microcontroller has grown by 128 bytes for us to use.


Okay, let’s review and summarize the above content!

The whole article mainly introduces the functions of each module in the RAM area of ​​51 microcontroller through KEIL and programs. The first aspect is the address space occupied by the general register group and the role of this area when the program is running. Secondly, the importance and role of the bit addressing area. If bit addressing is not used, the read-write-modify form can only be used. This is not only inconvenient to use, but also increases the amount of code. The third aspect is the importance of general RAM area and SFR area.



Reference address:Memory mapping of 51 microcontroller (1)

Previous article:Memory mapping of 51 microcontroller (2)
Next article:Summary of 51 microcontroller knowledge points

Latest Microcontroller Articles
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号