Application of C language in single chip microcomputer development

Publisher:翩翩轻舞Latest update time:2011-10-31 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, they always feel that high-level languages ​​are not as controllable as assembly. 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 by the author in actual work, hoping to help beginners of C51.

1. Compilation of C51 hot start code

For industrial control computers, there is often a watchdog circuit. When the watchdog is activated, the computer is reset, which is a hot start. When hot starting, 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, when the program must determine whether it is a hot start or a cold start, the commonly used method is: determine a certain memory unit as a flag bit (such as 0x7f bit and 0x7e bit), first read the content of the memory unit 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 processing

*HotPoint=0xaa;

*(++HotPoint)=0xaa;

}

/*Normal working code*/

}

However, in actual debugging, it is found that no matter it is hot start or cold start, the values ​​of all memory units 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 "starting code" must be executed first. It is this section of code that performs the clearing work. The C compiler provides the source code 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, 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):


modify the startup.a51 source file (this file is in the C51LIB directory).




Execute the following command:


A51 startup.a51 to get the startup.obj file. Copy this file to the directory where HOTSTART.C is located.


Compile the compiled C source program with C51.EXE to get the target file HOTSTART.OBJ.



Use the L51 HOTSTART, STARTUP.OBJ command to connect and get the absolute target file HOTSTART.



Use OHS51 HOTSTART to get the HOTSTART.HEX file.


Modify startup.a51 according to your own needs. For example, change 80H in IDATALEN EQU 80H to 70H, so that the 16-byte memory from 6F to 7F will not be cleared.

2. Directly call the program that has been fixed in EPROM

The emulator used by the author is displayed by a 6-digit digital tube. The display subroutine is placed at DE00H in the memory. 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
[page]

How to implement this function when programming in C language? In C language, there is the 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) (); /* define pointer to function */

DispBuffer = 0xde00; /* assign value */

for (;;)

{ Key ();

DispBuffer ();

}

}

3. Convert floating point numbers to character arrays

When compiling an application, the author has such a 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 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 you need to store, and then force this pointer to be converted to a char type. In this way, you can use the pointer to get the values ​​of the bytes that make up the floating point number. The specific program 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 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 the void type pointer to be converted to char type, because*/

} /*void type pointer cannot be calculated*/

}

If the number has been stored in EEPROM and you want to take it out and merge it, the method is the same. You can refer to the program below.

#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 F RANKLIN C51 VER 3.2.

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

Previous article:Method of cracking the internal password of single chip microcomputer
Next article:Programming Skills of EH78 Series MCU

Recommended ReadingLatest update time:2024-11-17 00:38

51 MCU (AT89C52) timer
 #include #define uchar unsigned char #define uint unsigned int flying i,temp; void init() { TMOD=0x01; TH0=(65536-46080)/256; TL0=(65536-46080)%256; ET0=1; EA=1; TR0=1; } void main() { i=0; temp=0x01; heat(); while(1); } void timer0() interrupt 1 { TH0=(65536-46080)/256;
[Microcontroller]
51 MCU (AT89C52) timer
"Teach you to learn 51 single-chip microcomputer-C language step by step" Part 1 How to learn single-chip microcomputer
Chapter 1 How to Learn Microcontrollers        Working day and night on the wrong path will not lead to success in the end. Methods and ideas are absolutely the most important. Some students who study technology often skip this kind of chapters directly, because most of these chapters are full of nonsense. However,
[Microcontroller]
C51 global initialization and precise delay program
/********************************************************************************************************* *                                          Initialization Program *                                               QiZhao,2007 *                                           All Rights Reserved * File      : initial.
[Microcontroller]
Learn S3C2440 bare metal development under Keil from 0 - 3 Using external NORFLASH + internal RAM
About S3C2440 startup method: After the program is downloaded to NORFLASH, choose to start from NORFLASH and execute automatically. The code is executed in NORFLASH and variables are allocated in the internal RAM. The address of NORFLASH itself is mapped to 0X0000000, and the internal RAM is used as the memory. After
[Microcontroller]
Learn S3C2440 bare metal development under Keil from 0 - 3 Using external NORFLASH + internal RAM
ZTE's new phone passed 3C certification and supports up to 55W fast charging
Some netizens have discovered that a new ZTE 5G phone has just passed the national 3C quality certification. The model is ZTE A2023H, and it comes with a standard NB-A1160A-C charger, supporting up to 55W fast charging. It is currently unclear which brand or series the phone belongs to.   In addition, the phone has
[Mobile phone portable]
ZTE's new phone passed 3C certification and supports up to 55W fast charging
Design of digital image system for assisting reverse driving based on S3C2410
1. Introduction  According to statistics, traffic accidents caused by blind spots behind vehicles account for about 30% in China and 20% in the United States. The first two generations of reversing assistance products are reversing horns and reversing radars. The former can only remind passers-by to dodge on their o
[Microcontroller]
Design of digital image system for assisting reverse driving based on S3C2410
STC12C60S2 single-chip dual 485 communication development dual 485 communication control program
The circuit diagram is as follows:   Program Description Wiring: MAX485-1 D+ links to MAX485-2 D+; MAX485-1 D- links to MAX485-2 D- Function: Serial port 1 automatically sends a tab of data, serial port 2 receives, and the data is displayed on the LED light of port P0 after receiving.           Serial port 2 autom
[Microcontroller]
STC12C60S2 single-chip dual 485 communication development dual 485 communication control program
C language description and application in teaching digital logic circuits
Abstract: In order to improve the teaching method of digital logic circuits to meet the needs of the rapid development of electronic technology, we explored and practiced a new method of teaching digital logic circuits, which is the classroom teaching and experimental teaching method of digital logic circuits based
[Microcontroller]
C language description and application in teaching digital logic circuits
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号