MCU_Pointer reading content analysis

Publisher:cocolangLatest update time:2020-06-27 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The STC microcontroller has its own internal reference voltage, which has been calibrated when the microcontroller is produced and can be read in RAM and ROM respectively.


(1) Read in ROM


When reading in ROM, you need to check the option of adding important test parameters at the end of the program area in the download software when downloading, as shown in the figure below:

After checking Download, the BandGap voltage can be read in mV.


BGV = (int code *)0xeff7; ROM capacity is different, the address is different, such as // STC8A8K60S4A10 is 0xeff7


code refers to the program storage (ROM) address


 


(2) Read from RAM


In RAM, there is no need to check (add important test parameters at the end of the program area), put it in the idata area, and use the following program ① (official routine) when reading:


#include "reg51.h"

int *BGV;

unsigned char hi;

unsigned char Low;

void main()

{

BGV = (int idata *)0xef;

Low = (*BGV);

hi = (*BGV) >> 8;

}

 


Question 1: Why is the pointer type set to int? Can it be set to unsigned int?


????


Question 2: 0xef is an address, why can data at high and low addresses be read?


Observe assembly code in a debug session


     6: void main() 

     7: { 

     8: BGV = (int idata *)0xef; 

     9:          

C:0x002E 7B00 MOV R3,#0x00

C:0x0030 7A00 MOV R2,#0x00

C:0x0032 79EF MOV R1,#0xEF

C:0x0034 8B0A MOV BGV(0x0A),R3

C:0x0036 8A0B MOV 0x0B,R2

C:0x0038 890C MOV 0x0C,R1

    10: Low =(*BGV); 

C:0x003A 120003 LCALL C?ILDPTR(C:0003)

C:0x003D AEF0 MOV R6,B(0xF0)

C:0x003F F508 MOV Low(0x08),A

    11: hi = (*BGV) >> 8; 

    12:          

    13:          

C:0x0041 EE MOV A,R6

C:0x0042 FF MOV R7,A

C:0x0043 8F09 MOV hi(0x09),R7

As above, the function ILDPTR is called. The code for ILDPTR assembly is as follows


                 C?ILDPTR:

C:0x0003 BB010A CJNE R3,#0x01,C:0010

C:0x0006 8982 MOV DPL(0x82),R1

C:0x0008 8A83 MOV DPH(0x83),R2

C:0x000A E0 MOVX A,@DPTR

C:0x000B F5F0 MOV B(0xF0),A

C:0x000D A3 INC DPTR

C:0x000E E0 MOVX A,@DPTR

C:0x000F 22 RET      

C:0x0010 5006 JNC C:0018

C:0x0012 87F0 MOV B(0xF0),@R1

C:0x0014 09 INC R1

C:0x0015 E7 MOV A,@R1

C:0x0016 19 DEC R1

C:0x0017 22 RET      

C:0x0018 BBFE07 CJNE R3,#0xFE,C:0022

C:0x001B E3 MOVX A,@R1

C:0x001C F5F0 MOV B(0xF0),A

C:0x001E 09 INC R1

C:0x001F E3 MOVX A,@R1

C:0x0020 19 DEC R1

C:0x0021 22 RET      

C:0x0022 8982 MOV DPL(0x82),R1

C:0x0024 8A83 MOV DPH(0x83),R2

C:0x0026 E4 CLR A

C:0x0027 93 MOVC A,@A+DPTR

C:0x0028 F5F0 MOV B(0xF0),A

C:0x002A 7401 MOV A,#0x01

C:0x002C 93 MOVC A,@A+DPTR

C:0x002D 22 RET      

This program features:

(1) When the value of R3 is equal to 0x01, execute the following program:


C:0x0006 8982 MOV DPL(0x82),R1

C:0x0008 8A83 MOV DPH(0x83),R2

C:0x000A E0 MOVX A,@DPTR

C:0x000B F5F0 MOV B(0xF0),A

C:0x000D A3 INC DPTR

C:0x000E E0 MOVX A,@DPTR

C:0x000F 22 RET      

Program function: read the data in the extended RAM and assign it to A, the addressing range is 0 to 65535. When the array is defined with xdata,


BGV = (int idata *)0xef;

Will jump here.

(2) When the value of R3 is less than 0x01 or equal to 0x00, execute the following program:


C:0x0010 5006 JNC C:0018

C:0x0012 87F0 MOV B(0xF0),@R1

C:0x0014 09 INC R1

C:0x0015 E7 MOV A,@R1

C:0x0016 19 DEC R1

C:0x0017 22 RET    

The CJNZ operation will affect CY, so JNC (jump if the carry bit is 0)


 CJNE R3,#0x01,C:0010

The above operation CJNE R3, #0x01, C:0010 will affect the borrow bit in PSW. When R3 is less than #0x01, CY is set to 1, otherwise it is set to 0.



Program function: Read the data in the 256-byte RAM inside the microcontroller and assign it to A, with an address range of 0 to 255. When the array is defined with data or idata, it will jump here. For example, when the statement BGV = (int idata *)0xef; is executed, it will jump to itself and read the data in the internal RAM address.   

(3) When the value of R3 is not equal to 0x00 or 0x01, the JNC instruction jumps to C:0x0018 and starts to compare with 0xFE. When the value of R3 is equal to 0xFE, the following program is executed:


C:0x0018 BBFE07 CJNE R3,#0xFE,C:0022

C:0x001B E3 MOVX A,@R1

C:0x001C F5F0 MOV B(0xF0),A

C:0x001E 09 INC R1

C:0x001F E3 MOVX A,@R1

C:0x0020 19 DEC R1

C:0x0021 22 RET 

Program function: read the data in the external RAM of the microcontroller and assign the low bit to B and the high bit to A. The addressing range is 0 to 255. When the array is defined with pdata, it will jump here. Usually, 8051 microcontrollers do not use pdata to define variables or arrays.

(4) When the value of R3 is not equal to 0xFE, that is, when the value of R3 is equal to 0xFF, jump to C:0x0022 and execute the following program:


C:0x0022 8982 MOV DPL(0x82),R1

C:0x0024 8A83 MOV DPH(0x83),R2

C:0x0026 E4 CLR A

C:0x0027 93 MOVC A,@A+DPTR

C:0x0028 F5F0 MOV B(0xF0),A

C:0x002A 7401 MOV A,#0x01

C:0x002C 93 MOVC A,@A+DPTR

C:0x002D 22 RET      

Program function: read the data in the ROM inside the microcontroller and assign the low bit to B and the high bit to A. The addressing range is 0 to 65535. When the array is defined with code, such as BGV = (int code *)0xef;

It can be seen that the role of the sub-function "C?ILDPTR" is to read the data at a certain address using different addressing modes according to the storage space where the data is located. R3 is used to determine the addressing mode. The corresponding relationship between the value of R3 and the corresponding addressing mode is:

1. When the value of R3 is equal to 0x00, the on-chip RAM is indirectly addressed; at this time, the data is defined by data and idata.

2. When the value of R3 is equal to 0x01, the off-chip RAM (extended RAM) is indirectly addressed; at this time, the data is defined by xdata.

3. When the value of R3 is equal to 0xFE, the lower 246 bytes of the off-chip RAM (extended RAM) are indirectly addressed; at this time, the data is defined by pdata

4. When the value of R3 is equal to 0xFF, the addressing is performed from the storage memory (ROM); at this time, the data is defined by code


In the Keil C-51 compilation environment, pointer variables, regardless of their length, occupy 3 bytes. R3 indicates the storage space where they are located, R1 and R2 indicate the address.


To summarize, in the Keil C-51 compilation environment, a pointer is a special variable that occupies 3 bytes. When the compiler compiles a program, it automatically generates a sub-function to determine the addressing mode, and assigns a value to the first byte of the pointer according to the physical storage area where the target data is located. Different addressing methods are performed according to the assigned value; the last 2 bytes of the pointer are used to store the referenced address.


Going back to program ①, we use BGV = (int idata *)0xef; idata is used so it will execute


C:0x0010 5006 JNC C:0018

C:0x0012 87F0 MOV B(0xF0),@R1

C:0x0014 09 INC R1

C:0x0015 E7 MOV A,@R1

C:0x0016 19 DEC R1

C:0x0017 22 RET    

Program function: R1 stores the address 0xEF, MOV B (0xF0), after the @R1 operation, the address of R1 is increased by 1 and becomes 0xF0, so question 2 can read the high and low addresses.


appendix


(1) C?CLDPTR is called when operating a char pointer. It is similar to the assembly principle of ILDPTR (int pointer) and will not be discussed here.


If char *BGV1 is defined; reading the content, the following code can be obtained


                 C?CLDPTR:

C:0x00AF BB0106 CJNE R3,#0x01,C:00B8

C:0x00B2 8982 MOV DPL(0x82),R1

C:0x00B4 8A83 MOV DPH(0x83),R2

C:0x00B6 E0 MOVX A,@DPTR

C:0x00B7 22 RET      

C:0x00B8 5002 JNC C:00BC

C:0x00BA E7 MOV A,@R1

C:0x00BB 22 RET      

C:0x00BC BBFE02 CJNE R3,#0xFE,C:00C1

C:0x00BF E3 MOVX A,@R1

C:0x00C0 22 RET      

C:0x00C1 8982 MOV DPL(0x82),R1

C:0x00C3 8A83 MOV DPH(0x83),R2

C:0x00C5 E4 CLR A

C:0x00C6 93 MOVC A,@A+DPTR

C:0x00C7 22 RET      

(2) In Keil's memory, D:0x0F represents the data in RAM, and C:0x0F represents the data in code.


(For example, the upper 128 bytes of the internal RAM of the microcontroller, direct addressing is SFRS (special function registers), and indirect addressing is the internal data area, the two are physically separated)


  C: Code storage space


  D: Directly address on-chip storage space


  I: Indirect addressing of on-chip storage space


  X: Extended external RAM space


(3) for(;;a++,b++) The for statement can add N ++


(4) unsigned char code tab2[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};


The data in tab2 cannot be modified because it is in the code area and cannot be changed during program execution.

Keywords:MCU Reference address:MCU_Pointer reading content analysis

Previous article:51 MCU | Serial communication experiment (simulated serial communication/multi-machine communication example)
Next article:Teach you microcontroller step by step - serial port

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号