Application of C language in single chip microcomputer development

Publisher:chuyifeiLatest update time:2011-02-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the development and application of single-chip microcomputers, high-level languages ​​have gradually been introduced, and C language is one of them. For those who are used to assembly language, they always feel that high-level languages ​​are not as controllable as assembly language. However, as long as we have a certain knowledge of C language, some things are still easy to do. The following are several problems encountered in the author's actual work, hoping to help beginners of C51 .

1. Preparation of C51 hot start code

Industrial control computers are often equipped with watchdog circuits. When the watchdog is activated, the computer is reset, which is a hot start. During a hot start, it is generally not allowed to start from the beginning, which will cause the existing measured or calculated values ​​to be reset, resulting in abnormal system operation. Therefore, the program must determine whether it is a hot start or a cold start. The common method is: determine a certain memory unit as a flag bit ( such as 0x7f bit and 0x7e bit ) , and read the content of the memory unit first when starting. 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 the two memory units.

According to the above design ideas, when programming, set a pointer to point to a specific memory unit such as 0x7f , and then judge it in the program. The program is as follows:

void main()

{char data *HotPoint=(char *)0x7f;

if((*HotPoint==0xaa)&&(*(--HotPoint)==0xaa))

{/* Hot start processing */

}

else

{ HotPoint=0x7e; /* Cold start process

*HotPoint=0xaa;

*(++HotPoint)=0xaa;

}

}

However, in actual debugging, it is found that no matter it is a hot start or a cold start, the values ​​of all memory cells are reset to 0 after startup , and of course the hot start requirement 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. Before the first statement of the main () function is executed, a section of " startup code " must be executed first . It is this section of code that performs the reset work. The C compiler provides the source program of this startup code, named CSTARTUP.A51. Open this file and you can see the following code:

IDATALEN EQU 80H ; the length of IDATA memory in bytes.

STARTUP1:

IF IDATALEN <> 0

MOV R0,#IDATALEN - 1

CLR A

IDATALOOP: MOV @R0,A

DJNZ R0,IDATALOOP

ENDIF

It can be seen that before executing the code to determine whether to hot start, the startup 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 attached to the compiler to compile startup.a51 to get the startup.obj file, and then use this code to replace the original startup code. The specific steps are (assuming that the C source program is named HOTSTART.C ):

  1. Modify the startup.a51 source file ( this file is in the C51LIB directory ) .
  2. Execute the following command:
  3. A51 startup.a51 gets the startup.obj file. Copy this file to the directory where HOTSTART.C is located

  4. Compile the compiled C source program with C51.EXE to obtain the target file HOTSTART.OBJ .
  5. Use L51 HOTSTART, STARTUP.OBJ command to connect and get the absolute target file HOTSTART .
  6. Use OHS51 HOTSTART to get the HOTSTART.HEX file.

For the modification of startup.a51 , you can make it according to your own needs. For example, if you change 80H in IDATALEN EQU 80H to 70H , the 16- byte memory from 6F to 7F will not be cleared.

2. Directly call the program solidified in EPROM

The simulation machine used by the author has a 6- digit digital tube display. The display subroutine is placed in the memory DE00H . As long as the number to be displayed is placed in the display buffer, and then this subroutine is called, it can be used. The assembly instruction is :

LCALL 0DEOOH

How to implement this function when programming in C language ? In C language, there is a concept of pointer to function, which can be used to implement calling function with function pointer. The definition format of pointer variable pointing to function is:

Type identifier (* pointer variable name ) ();

After defining the pointer, you can assign a value to the pointer variable to point to the starting address of a function, and then use

(* pointer variable name )() to call this function. As shown in the following example:

void main(void)

{

void (*DispBuffer)();

DispBuffer=0xde00;

for(;;)

{Key();

DispBuffer();

}

}

3. Convert floating point numbers into character arrays

When I was compiling an application, I had the following requirement: store the result of the operation (floating point number) in EEPROM . We know that floating point numbers are stored in IEEE format in C language . A floating point number occupies four bytes. For example, the floating point number 34.526 is stored as ( 160 , 26 , 10 , 66 ). To store a floating point number in EEPROM , you actually need to store these four numbers. So how do you get the composition of a floating point number in the program?

When floating point numbers are stored, they are stored in continuous 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 you need to store, and then force this pointer to be converted 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 procedure is as follows:

#define uchar unsigned char #define uint unsigned intvoid FtoC(void)

{float a;

uchar i,*px

uchar x[4]; /*Define a character array to store four bytes of floating point numbers*,

void *pf;

px=x;

pf=&a;

a=34.526;

for(i=0;i<4;i++)

{*(px+i)=*((char *)pf+i);

}

}

If the numbers have been stored in EEPROM and you want to retrieve them and merge them, the method is the same. Please refer to the following program.

#define uchar unsigned char #define uint unsigned int

void CtoF(void)

{float a;

uchar i,*px

uchar x[4]={56,180,150,73};

void *pf;

px=x;

pf=&a;

for(i=0;i<4;i++)

{*((char *)pf+i)=*(px+i);

}

}

The C language used above is FRANKLIN C51 VER 3.2 .

Reference address:Application of C language in single chip microcomputer development

Previous article:Operators and expressions (1)
Next article:Talk about C51 programming standards

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号