In the development and application of microcontrollers, high-level languages have gradually begun to be introduced, and C language is one of them. People who are used to assembly always feel that high-level languages are not as "controllable" as they can be as arbitrary as assembly. The following are some of the author's experiences in C51 programming. I hope it will be helpful to beginners learning C51.
1. Preparation of C51 hot start code
Industrial control computers are often equipped with a watchdog circuit. When the watchdog operates, the computer is reset. This is a hot start. During a warm start, the program is generally not allowed to start from the beginning, because this will reset the measured or calculated values, causing the system to work abnormally. Therefore, the program must determine whether it is a hot start or a cold start. The commonly used method is to set a certain memory unit as a flag bit (such as 0x7f bit and 0x7e bit), and first read the content of the memory unit during startup. If it is equal to a specific value (for example, both memory units are 0xaa) , it is considered a hot start, otherwise it is a cold start. The program executes the initialization part and assigns 0xaa to these two memory units.
Based on the above design ideas, when programming, set a pointer to point to a specific memory unit such as 0x7f, and then determine the cold/warm start based on the value of the specific memory unit in the program. The program is as follows:
void main()
{ char data*HotPoint=(char*)0x7f;
if((*HotPoint==0xaa)&&(*(--Hot
Point)==0xaa))
{ /*Hot start processing*/
}
else
{ HotPoint=0x7e; /*Cold start processing
*HotPoint-0xaa;
*(++HotPoint)=0xaa;
}
/*Normal working code*/
}
During actual debugging, it was found that whether it is a hot start or a cold start, the values of all memory cells are reset to 0 after booting, and the hot start requirements cannot be achieved. Why is this? It turns out that when programming in C language, the code executed at startup does not start from the first statement of the main() function. A section of 'starting code' must be executed before the first statement of the main() function is executed. It is this code that performs the work of clearing memory. The C compiler provides the source program of this start code, named CSTARTUP A51. When you open this file, you can see the following code:
IDATALEN EQU 8011 the length of IDATA memory m bytes
STARTUP1:
IF IDATALEN《》0
MOV R0,#IDATALEN-I
CLR A
IDATALOOP: MOV @R0,A
DJNZ R0,IDATALOOP
ENDIF
It can be seen that before executing the code to determine whether to warm start, the starting code has cleared all memory units. How to solve this problem? Fortunately, the startup code can be changed. The method is: modify the startup.a51 source file, and then use the a51.exe program that comes with the compiler to compile startup.a51 to get the startup.obj file. Use this code to replace the original Start code. The specific steps are (assuming the C source program is named HOTSTART C):
1 Modify the startup.a51 source file (this file is in the C51/LIB directory).
2 Execute the following command:
A51 startup.a51 gets the startup.obj file. Copy this file into the directory where HOTSTART C is located.
3 Compile the compiled C source program with C51 EXE to obtain the target file HOTSTART OBJ.
4 Use the L51 HOTSTART and STARTUP OBJ commands to connect to get the absolute target file HOTSTART.
5 Use OHS51 HOTSTART to obtain the HOTSTART HEX file to complete the modification of the startup code.
Modify startup.a51 according to your own needs. For example, changing 80H in IDATALEN EQU 80H to 70H will prevent the 16-byte memory from 6F to 7F from being cleared.
2. Directly call the solidified program in EPROM
The emulator I use is displayed by a 6-digit digital tube. The display subroutine is stored at DE00H. Just store the displayed number in the display buffer and then call the display subroutine. The assembly instructions are:
LCALL 0DE00H
How to implement this function when programming in C language? The C language has the concept of pointers to functions, which can be used to call functions using function pointers. The definition format of a pointer variable pointing to a function is:
Type identifier(*pointer variable name)();
After defining the pointer, you can assign a value to the pointer variable so that it points to the starting address of a function, and then use (*pointer variable name) () to call the function. The procedure is as follows:
void main(void)
{
void (*DispBuffer)();/*define pointer to function*/
DispBuffer=0xde00; /*assignment*/
for(;;)
{ Key();
DispBuffer();
}
}
3. Convert floating point numbers into character arrays
The author has this requirement when compiling the application program: store the result of the operation (floating point number) in E2PROM. We know that floating point numbers are stored in IEEE format in C language, and a floating point number occupies four bytes. For example, the floating point number 34 526 is stored as 160, 26, 10, and 664 numbers. To store this floating point number in E2PROM, you actually need to store these four numbers. How to get the components of a floating point number in a program?
When floating point numbers are stored, they are stored in consecutive bytes. As long as you try to find the storage location, you can get these numbers. You can define a void pointer, point this pointer to the floating point number that needs to be stored, and then cast this pointer to char type. In this way, you can use the pointer to get the value of each byte that makes up the floating point number. The specific procedures are as follows:
#define uchar unsigned char
#define uint unsigned int
void FtoC(void)
{ float a;
flying I,*px
uchar x[4];/*Define character array and prepare to store four bytes of floating point numbers*/
void *pf;
px=x; /*px pointer points to array x*/
pf=&a;/*void pointer points to the first address of the floating point number*/
a=34.526;
for(I=0;I《4;I++)
{ *(px+I)=*((char *)pf+I);/*Force void pointer to be converted to char type, because void pointer cannot be operated on*/
}
}
If the data has been stored in E2PROM, the method is the same to take it out and merge it. Please refer to the following procedure.
#define uchar unsigned char
#define uint unsigned int
void CtoF(void)
{ float a;
flying I,*px
fly x[4]-{56,180,150,73};
void *pf;
px=x;
pf=&a;
for(I=o;I《4;I++)
{ *((char *)pf+I)=*(px+I)
}
}
The C language used in the above program is FRANKLIN C51 VER 3 2.
Previous article:Design of smoke alarm system based on 51 microcontroller
Next article:Use AT89S51 microcontroller to make an infrared remote control.
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Editor, please correct the author of this article!
- How to get started with CPLD
- Heart rate detection BLE device based on RSL10
- Could you please tell me why this happens to the signal collected by SAR ADC?
- High Frequency Circuit Principle and Analysis
- Can you please tell me how to port MicroPython to my own chip?
- Simple automatic resistance tester
- Thank you + EEWORLD for accompanying me
- Why does the output voltage of the power module become low?
- Make full use of the FLASH storage space of MSP430