Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Why does C5 1 language need to include header files?
Why does C5 1 language need to include header files?
Let me answer your question. A few days ago, I had a deep understanding of this aspect and wrote a lot of notes.

Although different chips have different header files when C is programmed, they are all the same.

As long as you learn to write your own header files, you can cope with all kinds of single-chip computers. Even if you use AT89C2052, AT89C5 1, STC 12C, etc. , you can use a header file reg5 1.h, but you need to fix it accordingly.

The following is my personal opinion on reg5 1.h: (Very useful for you) The following is the header file used when writing C5 1, and a detailed explanation of its internal functions and macro definitions.

For the following information, please send an email to amwjie72 @163.com.

Firstly, the memory structure of C5 1 is deeply analyzed.

Second, reg5 1. Header file analysis

Thirdly, about the types and ranges of variables.

Iv. C5 1 Common header files

Fifth, talk about interruption.

Limitations of C5 1 compiler

Seven, indicator C5 1 pointer

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Reg5 1。 Header file analysis

When we usually write MCU applications, most of the header files we use are reg5 1.h or reg52.h Anyone who can write C5 1 will use it, but not many people really know the definition in its header file.

The following is a detailed description of its interior, which is convenient for readers to further understand and can use various types of single chip microcomputer. Because the enhanced functions of enhanced models are controlled by special function registers.

Open the reg52.h header file, and you will find that it is composed of a large number of sfr, sbit statements, and even sfr 16. In fact, this statement is related to the internal function register (special function register) of the single chip microcomputer, which is explained in detail below.

Sfr: declare variables

SFR declares a variable, and its declaration is basically the same as other C variables. The only difference is that SFR also specifies a special function register as the storage address, while the compiler automatically allocates storage space for integers, characters, etc., which is different from that declared by C variable.

As in the reg52.h header file, the first sentence is sfr P0 = 0x80.

Here, the variable P0 is declared, and its storage address is designated as the special function register 0x80 after adding the reg52.h header file. When writing an application, you can directly use P0 without definition. The operation of P0 is to read and write internal special function registers (ports 0x80 to P0 of the application MCU).

If the first sentence is changed to sfr K0 = 0x80, then if you want to reduce all P0 ports of the single chip microcomputer, you should save it instead of writing P0 = 0x00, and then write K0 = 0x00 in the application. Otherwise, the compiler will prompt "P0 is an undefined identifier".

How to use:

Sfr [variable] = [address] // Assign a special function register to the variable.

To the right of 1 equal sign, only decimal and hexadecimal integer data constants are allowed, and expressions with operators are not allowed.

The SFR addresses supported by the classic 805 1 kernel range from 0x80H~0xFF, Philips 80C5 1MX series 0x 180h to 0x 1ff.

2 SFR cannot be declared inside any function, including the main function. Can only be declared outside a function.

3 After declaring variables with SFR, you cannot use the address operator &; Get its address, compilation fails, and the compiler will prompt illegal operation.

It is important to note that 5 1 core 0x80~0xff is the address range of special function registers, but not all addresses are defined. If there is no address defined on the MCU chip you are using, when using sfr to define variables, do not assign the address of variables to undefined special function registers. Although it can pass when compiling, there seems to be no problem when simulating with KEIL, just downloading it to the chip. For example, if you read an undefined special function register, what you read is an unknown number. Readers can test by themselves, first open serial communication, and then do a simple human-computer interaction. Read a number, then send it to the computer, and check it with serial debugging assistant or serial monitoring. This method is very useful in simulation. Therefore, for specific special function registers, it is necessary to check the chip manual you use.

If you encounter an enhanced microcontroller, you only need to know the address of its extended special function register and set it with SFR.

Convenient programming.

Sbit: declare variables

Sbit is also used to declare a variable, similar to SFR, but it is used to declare a bit variable, because in the application of 5 1 series, it is very necessary to access a single bit of SFR, and it has a bit addressing function through the bit data type.

For example, in reg52.h, there are the following statements.

sfr IE = 0xA8

sbit ea = ie^7;

sbit et2 = ie^5; //Only 8052

sbit es = ie^4;

sbit et 1 = ie^3;

sbit ex 1 = ie^2;

sbit et0 = ie^ 1;

sbit ex0 = ie^0;

Therefore, the operation of EA is the operation of the highest bit of IE.

However, if you want to make special function registers such as SPD PL DPHPPCON TMOCTL0TL1TH1SBUF have addresses, you can't adopt the definition of IE as mentioned above. Although there will be no error when compiling after modification, as long as you use your own defined bit variable name, there will be an error. The reason is that only the address of the special function register is a multiple of 8 (hexadecimal ends with 0 or 8) can be used for bit addressing.

Open the reg52.h header file, and you can see that the addresses of all special function registers declared by sbit end with 0 or 8.

If you want to meet the above requirements, you can use macro definitions with parameters to complete it. I won't elaborate here (it's of little significance).

The following is a detailed introduction to the use of sbit:

In the application of 805 1, it is very necessary to access a single bit of a special function register. C5 1 compiler provides bit operations for special function registers through the s bit data type.

The following are three application forms of sbit:

1.sbitname = SFR-name bit position;

sfr PSW = 0xD0

sfr IE = 0xA8

sbit ov = psw^2;

sbit cy=psw^7;

sbit ea = ie^7;

Second, sbitname = SFT- address bit-location;

Sbit over = 0xd0 2

sbit cy =0xd0^7;

sbit ea =0xa8^7;

Third, sbitname = sbit-address;

sbit OV = 0x D2;

sbit CY = 0xD7

sbit EA = 0xAF

Now we will explain the above three forms of statement.

The first form sbit name = SFR-name bit-position;; Such as sbitov = PSW 2;; This special function register must be defined by sfr before, otherwise the compilation will be wrong.

The range of bit positions is from 0 to 7;

The second form sbitname = SFT- address bit-position, such as sbitov = 0xd02; The difference from the first form is that the address of PSW is directly used here. The first form must first define PSW.

The third form. Sbit name= sbit-address, such as sbit OV =0xD2, which is the address of direct OV.

The address of OV is calculated by adding the bit of OV to the register address where OV is located.

note:

Not all sfrs are bit addressable. Only when the address of the special function register is a multiple of 8 (hexadecimal ends with 0 or 8) can this bit be addressed. The variable names declared by sbit can be arbitrary, but it is best not to start with an underscore, because all names that start with an underscore are reserved for the header file of C5 1.

Sfr 16: Declare variables

Many 805 1 derivative microcontrollers use two special function registers with continuous addresses to store the value of 16 bits. For example, the 8052 uses 0xCC and 0xCD to store the high and low bytes of the timing/counting register 2. The compiler provides data type sfr 16 to store two bytes of data. Virtually create a 16 bit register.

As follows:

sfr 16 T2 = 0xCC

The storage mode is small-end storage, with low bytes before and high bytes after. When defined, only the low byte address is written. As above, T2 is defined as a special function register with 16 bits. T2L= 0CCh,T2H= 0CDh

How to use:

Sfr[ variable] = [low address]

On the right side of the equal sign of 1, only the low-order addresses of two special function registers are written, and they can only be integer data constants in decimal and hexadecimal, and expressions with operators are not allowed.

2 SFR cannot be declared inside any function, including the main function. Can only be declared outside a function.

3 After declaring variables with SFR, you cannot use the address operator &; Get its address, compilation fails, and the compiler will prompt illegal operation.

4 When writing data to an sfr 16, the code generated by KEIL CX5 1 compiler is to write the high byte first and then the low byte (which can be viewed through the assembly window). In some cases, this is not the order of operations we want. When using it, be sure to pay attention.

5 When it is very important to write the data of sfr 16, whether you write the high byte or the low byte first, you can only use the keyword sfr to define it, and only save one byte at any time, so that the operation can ensure the correct writing.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Common header files of C5 1

In KEIL, besides reg5 1 reg52, there are some header files with similar functions downloaded from various chip manufacturers in official website, which need to be further understood. In addition, you need to know the general header files that are quite useful for all types of single-chip computers. Because the functions and macro definitions contained in it can greatly facilitate us to write applications.

1 character function ctype.h

1 external bit isalpha (char);

Function: Check whether the parameter characters are English letters, and if so, return 1.

2 external isalnum(char)

Function: Check whether the character is an English letter or a numeric character, and if it is, return 1.

3 External bit iscntrl (character)

Function: Check whether the parameter value is between 0x00 and 0x 1f or equal to 0x7f, and if so, return 1.

4 isdigit (character) of external part

Function: Check whether the parameter is a numeric character, and if it is, return 1.

5 External bits are graphics (characters)

Function: Check whether the parameter value is a printable character. If it is, return 1, and the printable character is 0x2 1~0x7e.

6 external bit isprint(char)

Function: Like isgraph, it also accepts the space character 0x20.

7 external bit ispact (char)

Function: No introduction.

8 Low external bits (characters)

Function: Check whether the value of the parameter character is lowercase English letters, and if so, return 1.

9 external bit isupper(char)

Function: Check whether the value of the parameter character is uppercase English letters, and if so, return 1.

10 external bit isspace(char)

Function: Check whether the character is one of the following characters: space, tab, carriage return, line feed, vertical tab and paper feed. If true, 1 is returned.

1 1 The external bit is xdigit(char).

Function: Check whether the parameter character is 16 hexadecimal numeric character, and return 1 if it is.

12 external character input (character)

Function: convert ASCII characters 0~9 a~f (case-insensitive) into corresponding 16 hexadecimal numbers.

Return value 00H~0FH

13 external characters to lower characters

Function: Convert uppercase characters to lowercase characters. If the character variable is not between a and z, the character is returned directly without conversion.

14 external char toupper(char)

Function: Convert lowercase characters to uppercase. If the character variable is not between a and z, the character is returned directly without conversion.

15 defines toascii(c) ((c) and amp0x7f).

Function: This macro reduces any integer value to the valid ASCII range, and it combines variables with 0x7f to delete all digits above the seventh place.

16 # defines tolower(c) (c-'A'+'a')

Function: This macro bitwise OR characters with constant 0x20.

17 # defines toupper(c) ((c)-'a'+'A')

Function: This macro bitwise adds characters and constant 0xdf.

2 mathematical function math.h

extern int ABS(int val);

External character cab (character value);

extern long labs(long val);

External floating wafer factory;

Function: Returns the absolute value. The above four functions have different parameters and return values.

Other functions are exactly the same.

External floatexp (floatval);

External floating-point log (floating-point value);

External floating point log 10 (floating point value);

Function: exp returns eval.

Log returns the natural logarithm of val.

Log 10 returns the logarithm of val based on 10.

External floatsqrt (floatval);

Function: Returns the positive square root of val.

extern int rand();

extern void srand(int n);

Function: rand returns a pseudo-random number between 0 and 32767, and srand is used to initialize the random number generator to a known (expected) value.

There is no such function in the library of Keil uVision3.

External floating-point sin (floating-point val);

External floating-point cos (floating-point value);

External floating tan (floating value);

Function: Returns the sine, cosine and tangent of val. Val is radian fabs (var) < =65535.

External floatasin (floatval);

External floating-point acos (floating-point val);

External floating-point atan (floating-point val);

External floating point atan2 (floating point y, floating point x);

Function: asin returns the arcsine value of val. Acos returns the arccosine of val.

Atan returns the arc tangent of val.

The range of values of asintanacos is -π/2~+π/2.

Atan2 returns the arctangent value of x/y, with the range of-π ~+π.

External floatsinh (floatval);

External floatcosh (floatval);

External floating-point tanh (floating-point val);

Function: cosh returns the hyperbolic cosine of var, sinh returns the hyperbolic sine of var,

Tanh returns the hyperbolic tangent of var.

Upper external floating limit (floating value);

Function: Rounds up and returns the smallest integer greater than val.

External floating floor (floating valve);

Function: Rounds down and returns the largest integer less than val.

External floating-point power (floating-point x, floating-point y);

Function: Calculate the value of xy. When (x = 0, y

External void fpsave(struct FPBUF *p)

External void fprestore(struct FPBUF *p)

Function: fpsave saves the state of floating-point program, and fprestore restores the original state of floating-point subroutine. These two functions are very useful when floating-point operations need to be performed in interrupted programs.

Note: This function is not included in the math.h library in Keil Vision 3.

3 absolute address access to absacc.h

#define CBYTE ((unsigned char volatile code *) 0)

# define DBYTE((unsigned char volatile data *)0)

# define PBYTE((unsigned char volatile pdata *)0)

# define XBYTE((unsigned char volatile xdata *)0)

Function: CBYTE address code area

Data byte addressing data area

PBYTE address XDATA (low 256) area

X-byte addressing XDATA area

Example: The following instruction accesses an external memory area with address 0x 1000.

xvar = XBYTE[0x 1000];

x byte[0x 1000]= 20;

# define CWORD((unsigned int volatile code *)0)

# define DWORD((unsigned int volatile data *)0)

# define PWORD((unsigned int volatile pdata *)0)

# define XWORD((unsigned int volatile xdata *)0)

Functions: Similar to the previous macros, except that the data type they specify is unsigned int.

By using different data types flexibly, all 805 1 address spaces are accessible.

such as

DWORD[0x 0004]= 0x 12 F8;

That is, (0x08) = 0x12 in the internal data memory; (0x09)=0xF8

4 internal function intrins.h

extern unsigned char _ cror _(unsigned char var,unsigned char n);

extern unsigned int _ iror _(unsigned int var,unsigned char n);

extern unsigned long _ lror _(unsigned long var,unsigned char n);

Function: shift the variable var loop to the right by n bits.

The difference between the above three functions lies in the different types of parameters and return values.

extern unsigned char _ crol _(unsigned char var,unsigned char n);

extern unsigned int _ irol _(unsigned int var,unsigned char n);

extern unsigned long _ lrol _(unsigned long var,unsigned char n);

Function: shift the variable var loop left by n bits.

The difference between the above three functions lies in the different types of parameters and return values.

For example:

# include & ltintrins.h & gt

void main()

{

Unsigned int y;;

y = 0x0ff0

y=_irol_(y,4); //y=0xff00

y=_iror_(y,4); //y=0x0ff0

}

void _ nop _(void);

Function: _NOP_ generates the NOP instruction of 805 1 single chip microcomputer, and C5 1 compiler directly generates the NOP instruction where the program calls the _nop_ function.

In addition, there are group-buying products on the stationmaster group, which are cheap and guaranteed.