Several Issues on Application Programming of C51 Language

Publisher:VolareLatest update time:2012-07-04 Source: 21ic Keywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When developing single-chip microcomputer application systems, assembly language is a commonly used software tool. It can directly operate hardware and has fast execution speed of instructions. However, the inherent format of its instruction system is greatly restricted by the hardware structure, and it is difficult to write and debug, and its portability is also poor. With the improvement of single-chip microcomputer hardware performance, its working speed is getting faster and faster. Therefore, when writing single-chip microcomputer application system programs, more emphasis is placed on the writing efficiency of the program itself. The Franklin C51 cross compiler is an efficient C language compiler designed specifically for 80C51 series single-chip microcomputers. Using it can shorten the development cycle and reduce development costs. The developed system is easy to maintain, has high reliability and good portability. Even in terms of code usage efficiency, it can be compared with assembly language. Therefore, it has become a popular tool for developing 80C51 series single-chip microcomputers.

1 Basic skills of C51 language programming

C language is a high-level programming language that provides a very complete standardized process control structure. Therefore, when using C51 language to design single-chip microcomputer application system programs, the structured programming method should be adopted as much as possible, so that the entire application system program structure can be clear and easy to debug and maintain. For a larger program, the entire program can be divided into several modules according to function, and different modules perform different functions. For different functional modules, the corresponding entry parameters and exit parameters are specified respectively, and some frequently used programs are preferably compiled into functions, which will not cause confusion in the management of the entire program, but also enhance readability and portability.

In the process of program design, make full use of the preprocessing commands of the C51 language. For some commonly used constants, such as TRUE, FALSE, PI and various special function registers, or some important constants in the program that are variable according to external conditions, you can use macro definitions "#define" or concentrate them in a header file for definition, and then use the file include command "#include" to add them to the program. In this way, when you need to modify a certain parameter, you only need to modify the corresponding include file or macro definition, and you don't have to modify each program file that uses them, which is conducive to file maintenance and updating. The following examples are given:

Example 1 For different single-chip crystal oscillators, the program takes different delay times, and the length of the delay time can be modified according to changes in external conditions. For such a program, macro definitions and conditional compilation can be used to implement it. The program is as follows:

#define flag 1
#ifdef flag==1
#define fosc 6M
delay=10;
#elif flag = = 0
#define fosc 8M
delay=12;
#else
#define fosc 12M
delay=20;
#endif
main()
{
for(I=0;I }

In this way, the source program can be applied to microcontroller systems with different clock frequencies without any modification, and different delay values ​​can be taken according to different situations to achieve different purposes.

2 Mixed programming of C51 language and assembly language programs

The C51 compiler can compile C language source programs efficiently and generate efficient and concise codes. In most cases, C language programming can achieve the expected purpose. However, sometimes, for intuitive programming or the processing of some special addresses, certain assembly language programming must be used. In other cases, for some purposes, assembly language can also call C language. In this mixed programming, the key is the parameter transfer and the return value of the function. They must have complete agreements, otherwise the data exchange may go wrong. The following uses Liyuan's 10-bit serial A/D converter TLC1549 as an example to illustrate the calling of C language programs and assembly language programs. [page]
Click here to view the image in a new window

The pin diagram and timing diagram of 1549 are shown in Figure 1 and Figure 2 respectively. Assume that DATA OUT is connected to P1.0, CS is connected to P1.1, and CLOCK is connected to P1.2.
Please refer to the relevant information for the specific characteristics of 1549.
Click here to view the image in a new window

Figure 2 TLC1549 timing diagram

Example 2 Calling C language program and assembly language program, the subroutine is as follows:

PUBLIC AD; Entry address
SEG_AD SEGMENT CODE; Program segment
RSEG SEG_AD
USING 0
AD: MOV R6,#00
MOV R7,#00
SETB P1.1
ACALL DELAY
CLR P1.1
ACALL DELAY
MOV R0,#10
RR0: SETB P1.2
NOP
CLR P1.2
DJNZ R0,RR0
ACALL DELAY
MOV 30H,R6; A/D conversion high
; two bits are stored in R6
ACALL CIR
MOV R6,30H
SETB P1.2
NOP
CLR P1.2
MOV 30H,R6
ACALL CIR
MOV R6,30H
MOV R0,#8; A/D conversion low
; 8 bits are stored in R7
RR2: SETB P1.2
NOP
CLR P1.2
MOV 30H,R7
ACALL CIR
MOV R7,30H
DJNZ R0,RR2
RET
CIR: CLR C
MOV C,P1.0
MOV A,30H
RLC A
MOV 30H,A
RET
END [page]

In the above program, the return value of the function is an unsigned integer. According to the calling rules, the high bit of the return value must be in R6 and the low bit in R7, so as to ensure that the data transmission is error-free. In addition, during the calling process, attention must be paid to the stacking of registers. In this way, when A/D conversion is used in the future, the assembly language subroutine AD() can be called in C language.

3 C51 interrupt processing process

The C51 compiler supports the development of interrupt procedures directly in the C source program, thus reducing the tedious work of using assembly language and improving development efficiency. The complete syntax of the interrupt service function is as follows:

void function name (void) [mode]
[reentry] interrupt n [using r]

where n (0~31) represents the interrupt number. The C51 compiler allows 32 interrupts, and the specific interrupt to be used is determined by the 80C51 series chip. r (0-3) represents the rth group of registers. When calling an interrupt function, the register group used by the function called by the interrupt procedure must be the same as that of the interrupt function. "Reentry" is used to indicate whether the interrupt handling function has the "reentry" capability. The C51 compiler and its extension of the C language allow programmers to control all aspects of interrupts and use register groups. This support enables programmers to create efficient interrupt service programs. Users only need to care about interrupts and necessary register group switching operations in the C language.

Example 3 Assume that the FOSC of the microcontroller is 12MHz, and it is required to program using T0 mode 1 to output a square wave with a period of 2ms at the P1.0 pin.

The interrupt service program written in C language is as follows:

#include
sbit P1_0=P1^0;
void timer0(void)interrupt 1 using 1 {
/*T0 interrupt service program entry*/
P1_0=!P1_0;
TH0=-(1000/256); /*Reload the initial count value*/
TL0=-(1000%256);
}
void main(void)
{
TMOD=0x01; /*T0 works in timer mode 1*/
P1_0=0;
TH0=-(1000/256); /*Preset the initial count value*/
TL0=-(1000%256);
EA=1; /*CPU turns on interrupt*/
ET0=1; /*T0 turns on interrupt*/
TR0=1; /*Start T0*/
do{}while(1);
}

When writing interrupt service routines, it is important to note that parameters cannot be passed and there cannot be return values.

4 Conclusion

C51 compiler can not only shorten the development cycle of single-chip control systems, but also facilitate debugging and maintenance. In addition, C51 language has many powerful functions, such as providing rich library functions for users to call directly, complete compilation control instructions to provide necessary symbol information for program debugging, etc. In short, C51 language is a powerful tool for a large number of single-chip developers.

Keywords:C51 Reference address:Several Issues on Application Programming of C51 Language

Previous article:Using 51 single chip microcomputer to realize the conversion between Gregorian calendar and lunar calendar
Next article:Several useful modules for single-chip C51 programming

Recommended ReadingLatest update time:2024-11-16 19:36

C51 Functions of ADC0809 Analog-to-Digital Converter
/*********************ADC0809 function****************/ //Start A/D conversion function: StartADC() void  StartADC(uchar Address) { PinC = (bit) (Address & 0x04); //C highest bit PinB = (bit) (Address & 0x02); PinA = (bit) (Address & 0x01); //The above 3 sentences output address CBA Pin
[Microcontroller]
What is the definition format of C51 bit variables?
What is the definition format of C51 bit variables? How to define a bit variable of bdata type byte variable? Answer: bit Bit variable name 1 ] sbit bit variable name = bdata type variable name ^ bit number constant
[Microcontroller]
Three operations of C51 running light
Three operations of C51 running light 1-Project Directory Main function #include reg52.h #include"led.h" #include"delay.h" int main() { liushuianwei(); //bitwise delay_xms(1000); liushuixunhuan(); //Circular shift delay_xms(1000); liushuiyiwei();//Shift delay_xms(1000); while(1); re
[Microcontroller]
Three operations of C51 running light
The startup process of C program in keil c51
The assembly starts from org 0000h, so how does Keil C51 start the main() function? Keil C51 has a startup program startup.a51, which is always compiled and linked with the C program. Let's see how it is compiled with the main() function; //The main function is as follows; void main(void) {     while (1) This is an un
[Microcontroller]
C51 single chip microcomputer uses external interrupt to realize digital clock, and has time adjustment function
Simulation diagram: Code snippet: /*Overall function: Use eight-bit common anode digital tubes as electronic clock display, and use key0 to control the selection of hours, minutes and seconds. When pressed once, the hour is selected, twice the minute is selected, three times the second is selected, and the fourth
[Microcontroller]
C51 single chip microcomputer uses external interrupt to realize digital clock, and has time adjustment function
3*3 matrix keyboard program for C51 microcontroller
#include   #include dis.h /**Delay 1**/ //Used for debounce void delay1(void) {uchar i;  for(i=0;i 200;i++);  } /****Delay 2**********/  void delay2(void) {   uchar i,j,s;   for(i=250;i 0;i--)   for(j=250;j 0;j--)   for(s=5;s 0;s--); } /****Serial port initialization****/ void chuankou_init(void) {
[Microcontroller]
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
Introduction: Let me first explain that the chip driver of the LCD1602 I am going to talk about below is HD44780. If your LCD1602 driver chip is not HD44780, then the following content is not applicable. This time I will share my LCD1602 programming experience: Let me first explain that the chip driver of the LCD160
[Microcontroller]
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
Proteus C51 simulation learning board 6——ADC
Before we talk about ADC, let's talk about its cause and effect. Since computers widely use digital electronic technology in automatic control, detection and other fields, and the signals of nature are all analog signals, how to save natural signals through computers? Then we need ADC (Analog-Digital Converter) to con
[Microcontroller]
Proteus C51 simulation learning board 6——ADC
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号