C51 programming experience summary sharing

Publisher:创新火花Latest update time:2023-04-06 Source: elecfansKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.


Keywords:C51 Reference address:C51 programming experience summary sharing

Previous article:Design of smoke alarm system based on 51 microcontroller
Next article:Use AT89S51 microcontroller to make an infrared remote control.

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号