1. Variable absolute address positioning
1) When defining a variable, use the _at_ keyword plus the address.
unsigned char idata myvar _at_ 0x40;
Define the variable myvar at 0x40 of idata. You can find the following line in the M51 file:
IDATA 0040H 0001H ABSOLUTE ; indicates that a variable is located at the absolute address 0x0040 of idata.
2) Use KeilC compiler to define variables with absolute addresses, the method is to be checked.
2. Function absolute address positioning
1) Write a function myTest in the program
void myTest(void)
{
// Add your code here
}
2) Use KeilC compiler to locate the function of absolute address, open Project -> Options for Target menu, select BL51 Locate tab, enter in Code:?PR?myTest?MAIN(0x4000) to locate the function myTest to 0x4000 in the program area, and compile again.
3) How to locate multiple functions at once
Similarly, write another function myTest1 in the program
void myTest1(void)
{
// Add your code here
}
In the Code: field of the BL51 Locate tab of the Options for Target menu, enter: ?PR?myTest1?MAIN(0x3900), ?PR?myTest?MAIN(0x4000) to locate the function myTest1 at 0x3900 in the program area, define the function myTest at 0x4000 in the program area, and recompile.
The following can be found in the M51 file:
Copy code
3.obj TO Reader RAMSIZE (256) CODE (?PR?MYTEST1?MAIN (0X3900), ?PR?MYTEST?MAIN (0X4000))
3665H 029BH *** GAP ***
CODE 3900H 0014H UNIT?PR?MYTEST1?MAIN
3914H 06ECH *** GAP ***
CODE 4000H 0014H UNIT?PR?MYTEST?MAIN
Copy code
4) Function call
The method of directly calling functions in a program will not be explained here. Here we will focus on the method of using function pointers to call functions at absolute addresses.
(1) Define the prototype of the function to be called typedef void (*CALL_MYTEST)(void); This is the prototype of a callback function, and the parameter is empty.
(2) Define the corresponding function pointer variable CALL_MYTEST myTestCall = NULL;
(3) The function pointer variable is assigned to point to the function at the absolute address we located myTestCall = 0x3900; points to the function myTest1
(4) Function pointer call
if (myTestCall != NULL)
{
myTestCall(); // Call the function myTest1 at the function pointer, set the PC pointer to 0x3900
}
Check the bin file generated by the compilation. You can see the content of myTest1 at 0x3900 and the content of myTest at 0x4000.
(5) Other notes: If there is no content in the program space from 0x3000 to 0x3900, when the address pointer of myTestCall points to 0x3800 (between 0x3000 and 0x3900), execution will start from 0x3900. The method of calling a function in AP in Load is similar to this, but the corresponding parameter passing may require another method.
Definition of Segment
RSEG is a segment selection instruction. To understand its meaning, you need to understand the meaning of segment.
A segment is a storage unit for program code or data objects. Program code is placed in the code segment, and data objects are placed in the data segment. There are two types of segments: absolute segments and relocation segments. Absolute segments are specified in assembly language, and their addresses will not change when connected with L51. They are used to access the i/o of a fixed memory, or to provide the entry address of an interrupt vector. The address of the relocation segment is floating, and its address is determined by L51 when connecting the program module. The segments generated by C51 when compiling the source program are all relocation segments, which have segment names and storage types. Absolute segments do not have segment names.
After saying so much, you may still not understand what a segment means. Don't worry, just keep reading.
For example, you wrote a function void test_fun(void) { ...} in C and saved it in test.c. After compiling it with the compiler, you can see in the .SRC FILE:
?PR?test_fun?TEST SEGMENT CODE //(Put the function in the code segment)
When writing this function body:
RSEG ?PR?test_fun?TEST //Select the located code segment as the current segment test_fun:
……//code
So the expression pattern of the function is: ?PR?function name?file name
The function names are divided into:
1: Function without parameters? PR? Function name? File name
2: Function with parameters?PR?_Function name?File name
3: Re-enter the function?PR?_?function name?file name
For example, you define a global variable
unsigned char data temp1,temp2;
unsigned char xdata temp3;
In the test.c file, the compiler will divide each file into 0 or more global data segments, and global variables of the same type are stored in the same segment. So the above will be compiled as follows:
Copy code
RSEG ? DT ? TEST
. temp1: DS 1
. temp2: DS 1
;
RSEG ?XD? TEST
. temp3: DS 1
Copy code
Copy code
// Below is the representation of the global segment of each type of data
?CO? File name //Constant section
?XD? FILE_NAME //XDATA data segment
?DT? FILE_NAME //DATA data segment
?ID? FILE_NAME //IDATA…..
?BI? FILE_NAME //BIT …..
?BA? FILE_NAME //BDATA….
?PD? FILE_NAME //PDATA…..
Copy code
You should understand the meaning of segment after reading this. You may ask, what is the use of this? It is used when you need to write a part of the program in assembly language. Put the function written in assembly language in this file, rename it to xxx.a51, and write it according to the above rules. Just compile it.
Now that we know the meaning of segment, let's go back to the usage of SEG. There are two types of segment selection instructions in A51: relocation segment selection instruction and absolute segment selection instruction. They are used to select whether the current segment is a relocation segment or an absolute segment. Using different segment selection instructions will locate the program in different address spaces.
1. The selection instruction for relocation segment is: RSEG segment name
It is used to select a previously defined relocation segment as the current segment. The usage is just like the example above, where a function segment is declared first and then the function segment is written.
2. Absolute segment selection instruction
Copy code
CSEG [AT absolute address expression] //absolute code segment
DSEG [AT absolute address expression] // internal absolute data segment
XSEG [AT absolute address expression] //External absolute data segment
ISEG [AT absolute address expression] //Internal indirect addressing absolute data segment
BSEG [AT absolute address expression] // absolute bit addressing segment
Copy code
Here is an example of their usage:
For example, if we write a serial port interrupt program, the starting address is 0x23.
CSEG AT 0X23
LJMP serialISR
RSEG ?PR?serialISR?TEST
. serialISR:
The assembly function uses the variables in the same project C file. For example, if ICFLAG is defined in the C file, the definition in the assembly file is
EXTERN ICFLAG ; define external variables
Define a function, such as
Copy code
CARDATR:
...........
RET
GLOBAL CARDATR
Copy code
To call the CARDATR function in the same project file, you should define the function
extern void CARDATR(void);
C18 specifies the absolute address of data
For example:
#pragma udata overlay RECBUFS =0x190 //200
UINT8 NUMBER;
UINT8 REC_BUF[31];
#pragma udata
Previous article:The _at_ keyword in KEIL C51
Next article:Using _at_ absolute address positioning in keil
Recommended ReadingLatest update time:2024-11-16 15:00
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- A serial screen product with analog video signal usage sharing
- Useful information | Four major development skills for microcontrollers!
- 【TI mmWave Radar Review】+Configuration File Test (I)
- AC/DC voltage and current signal ADC acquisition
- EEWORLD University Hall----Microelectronic Devices and IC Design (Huazhong University of Science and Technology)
- Looking for QFN32/LQFP128 package PCB parts library
- Analysis of film deformation problem in PCB process
- PCB carries 10A current
- EEWORLD University ---- VICOR High Performance Power Conversion Seminar (2019.11)
- The wireless receiving signal brightness DBM is negative