C Language
To learn a programming language, the most important thing is to establish an exercise environment where you can learn and practice well. Keil software is currently the most popular software for the development of 80C51 series single-chip microcomputer. Keil provides a complete development solution including C compiler, macro assembly, connector, library management, and a powerful simulation debugger, these parts are combined using an integrated development environment (microvision.
After learning to use assembly language, it is easy to learn C language programming. We will introduce the C language programming method through a series of examples. As shown in Figure 1-1, the circuit diagram uses a single-chip microcomputer as the main chip. This single-chip microcomputer belongs to the 80C51 series. It has an 8 k flash rom, which can be erased repeatedly and has the ISP function. It supports online download, it is very suitable for experiments. P3.2 ~ The P3.4 pin is equipped with four button switches. Our task is to enable the Light Emitting Diode connected to the P1 pin to emit light as required.
1.1 Introduction to a simple C program
Example 1-1: Enable LED light on the P1.0 pin.
**************************************** *********
Single-light Program
**************************************** *********
# Include "reg51.h"
Sbit P1_0 = P1 ^ 0;
Void main ()
{P1_1 = 0;
}
The function of this program is to enable the LED on the P1.0 pin. Next we will analyze what information this C language program contains.
1) "File Inclusion.
The first line of the program is a "file inclusion" processing.
The so-called "File Inclusion" means that a file contains all the content of another file, so although the program here only has four lines, however, the C compiler processes dozens or hundreds of rows. Here, the program contains the REG51.h file to use the P1 symbol, that is, to notify the C compiler that the P1 written in the program refers to the port P1 of the 80C51 microcontroller rather than other variables. How is this done?
Open reg51.h to see the following content:
/* ---------------------------------------------------------------------- REG51.H
Header file for generic 80C51 and 80C31 microcontroller.
Copyright (c) 1988-2001 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
-------------------------------------------------------------------------*/
/* BYTE Register */
Sfr P0 = 0x80;
Sfr P1 = 0x90;
Sfr P2 = 0xA0;
Sfr P3 = 0xB0;
Sfr PSW = 0xD0;
Sfr ACC = 0xE0;
Sfr B = 0xF0;
Sfr SP = 0x81;
Sfr DPL = 0x82;
Sfr DPH = 0x83;
Sfr PCON = 0x87;
Sfr TCON = 0x88;
Sfr TMOD = 0x89;
Sfr TL0 = 0x8A;
Sfr TL1 = 0x8B;
Sfr TH0 = 0x8C;
Sfr TH1 = 0x8D;
Sfr IE = 0xA8;
Sfr IP = 0xB8;
Sfr SCON = 0x98;
Sfr SBUF = 0x99;
/* BIT Register */
/* PSW */
Sbit CY = 0xD7;
Sbit AC = 0xD6;
Sbit F0 = 0xD5;
Sbit RS1 = 0xD4;
Sbit RS0 = 0xD3;
Sbit OV = 0xD2;
Sbit P = 0xD0;
/* TCON */
Sbit TF1 = 0x8F;
Sbit TR1 = 0x8E;
Sbit TF0 = 0x8D;
Sbit TR0 = 0x8C;
Sbit IE1 = 0x8B;
Sbit IT1 = 0x8A;
Sbit IE0 = 0x89;
Sbit IT0 = 0x88;
/* IE */
Sbit EA = 0xAF;
Sbit ES = 0xAC;
Sbit ET1 = 0xAB;
Sbit EX1 = 0xAA;
Sbit ET0 = 0xA9;
Sbit EX0 = 0xA8;
/* IP */
Sbit PS = 0xBC;
Sbit PT1 = 0xBB;
Sbit PX1 = 0xBA;
Sbit PT0 = 0xB9;
Sbit PX0 = 0xB8;
/* P3 */
Sbit RD = 0xB7;
Sbit WR = 0xB6;
Sbit T1 = 0xB5;
Sbit T0 = 0xB4;
Sbit INT1 = 0xB3;
Sbit INT0 = 0xB2;
Sbit TXD = 0xB1;
Sbit RXD = 0xB0;
/* SCON */
Sbit SM0 = 0x9F;
Sbit SM1 = 0x9E;
Sbit SM2 = 0x9D;
Sbit REN = 0x9C;
Sbit TB8 = 0x9B;
Sbit RB8 = 0x9A;
Sbit TI = 0x99;
Sbit RI = 0x98;
Readers familiar with the internal structure of 80C51 can easily see that some symbols are defined here, that is, the correspondence between symbol names and addresses. Note that
Sfr P1 = 0x90;
This line (in the above section, it is represented by a black box), that is, defining the correspondence between P1 and address 0x90. The address of P1 port is 0x90 (0x90 is the hexadecimal number written in C language, equivalent to 90 H in assembly language ).
Here we can also see a frequently-occurring word: sfr
Sfr uses the Standard C language keyword, but Keil provides a new keyword for direct access to SFR in 80C51. Its usage is:
Sfrt variable name = address value.
2) The P1.0 pin is indicated by the P1_0 symbol.
In C language, if P1.0 is directly written, the C compiler cannot recognize it, and P1.0 is not a legal C language variable name, so you have to give it another name. Here, the name is P1_0, but is P1_0 P1.0? In your opinion, the C compiler does not think so, so you must establish a connection with them. Here we use the Keil C keyword sbit for definition. There are three sbit usage methods:
Method 1: sbit bit variable name = address value
Method 2: sbit bit variable name = SFR name ^ variable bit address value
Method 3: sbit bit variable name = SFR address value ^ variable bit address value
For example, you can use the following three methods to define OV in PSW:
Sbit OV = 0xd2 (1) Description: 0xd2 is the OV bit address value.
Sbit OV = PSW ^ 2 (2) Description: PSW must be defined with sfr.
Sbit OV = 0xD0 ^ 2 (3) Description: 0xD0 is the PSW address value.
Therefore, sfr P1_0 = P1 ^ 0 is used here. It is defined to express the P1.0 pin with the symbol P1_0. If you like, you can also name P10, as long as the following program also changes.
3) main is called the "main function ".
Each C language program has only one main function, and the function must be followed by a pair of braces "{}". Other programs are written in braces.
From the above analysis, we have learned some features of the C language. Next we will look at a more complex example.
Example 1-2 enable LED flashing on the P1.0 pin
/*************************************** **********
Single-light flashing program
**************************************** *********/
# Include "reg51.h"
# Define uchar unsigned char
# Define uint unsigned int
Sbit P10 = P1 ^ 0;
/* Latency Program
Delay Time determined by Delay Parameter
*/
Void mDelay (unsigned int Delay)
{Unsigned int I;
For (; Delay> 0; Delay --)
{For (I = 0; I <124; I ++)
{;}
}
}
Void main ()
{(;;)
{P10 =! P10; // reverse P1.0 pin
MDelay (1000 );
}
}
Program analysis: the first line in the main program is not currently viewed, and the second line is "P1_0 =! P1_0; ", there is a symbol"! "before P1_0, Symbol "!" It is an operator in C language, just like "+" and "-" in mathematics. It is an arithmetic operator, meaning "inverse ", returns the value of the variable after the symbol.
Note: The inverse operation is only for the value of the variable and does not automatically change the variable itself. It can be considered that the C compiler is processing "! P1_0 ", the value of P1_0 is given to a temporary variable, and then the temporary variable is reversed instead of the P1_0 directly, therefore, after the reverse operation is completed, you need to use the value pair ("=") to assign the reverse value to P1_0. In this way, if P1.0 is low (LED light ), then, P1.0 is the high level (LED off) after reverse sampling. If P1.0 is the high level, P1.0 is the low level after reverse sampling. This command is executed repeatedly, when connected to P1.0, the light will be constantly "on" and "off ".
The key to executing this command repeatedly lies in the first line of the main program: for (;), which is not described in detail here. for the time being, this line of program, together with a pair of braces "{}", forms an infinite loop statement, and the statements in the braces are repeatedly executed.
The third line of the program is: "mDelay (1000);", the purpose of this line of program is to delay 1 s time, because the MCU executes the command fast, if not delay, immediately after the light is on, the light is on immediately after the light is off. The speed is too fast and the human eyes cannot tell the difference.
Here mDelay (1000) is not a library function provided by Keil C, that is, you cannot write such a program under any circumstances to implement latency. If you write such a line when writing other programs, you will find that the compilation fails. So why is this correct? Note that there is void mDelay (…) in this program (...) In this line, we can see that the word mDelay is our own name, and some program lines are written for this. If your program does not have such a program line, then you cannot use mDelay (1000. Someone has a fast mind and may immediately think about it. Can I copy this program to my other programs, and then I can use mDelay (1000? The answer is yes, of course. Note that the name of mDelay is named by the programmer and can be changed by the programmer. However, once the name is changed, the name of the main () function must be changed accordingly.
There is a parentheses behind mDelay, which contains data (1000). This 1000 is called a "parameter" and can be used to adjust the delay length within a certain range, here, we use 1000 to set the latency to 1000 milliseconds. To do this, we have to write our own mDelay program. The details are further analyzed in the subsequent loop program, I will not introduce it here.
From: http://pangqicheng123.blog.163.com/blog/static/8233547620086124535942/