Reg51.h and Reg52.h Analysis of the head file in MCU with C programming

Source: Internet
Author: User

Reg51.h and Reg52.h Analysis of the head file in MCU with C programming

We are programming in C language is often the first line is Reg51.h or other custom header file, how do we understand it?

1) "file contains" processing.

The first line of the program is a "file contains" processing.

The so-called "file contains" refers to a file that contains the contents of the other file in its entirety. The program contains REG51.h

The purpose of the file is to use P1 (there are many more symbols) this symbol, that is, notify the C compiler, the program is written in P1 refers to the 80c51 MCU P1 port and not other variables. How is this done?

Open the Reg51.h to see something like this:

(This file is generally under C:/keil/c51/inc, the INC folder root directory has a lot of header files, and there are many folders in the company category, which is also the relevant product header files.) If we are going to use our own header files, we can simply copy the corresponding header files to the INC folder when we use them. )

/*--------------------------------------------------------------------------

REG51. H

Header file for generic 80C51 and 80C31 microcontroller.

Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.

All rights reserved.

--------------------------------------------------------------------------*/

#ifndef __reg51_h__

#define __reg51_h__

/* 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;

#endif

Readers familiar with the internal structure of 80c51 are not difficult to see, here are the definition of some symbols, that is, the designation of the name and the ground

The corresponding relationship of the address. Note that there are

SFR P1 = 0x90;

Such a line, that is, the definition of P1 and address 0x90 correspondence, P1 port address is 0x90

(0x90 is the hexadecimal number in C language, equivalent to writing in assembly language 90H).

From here you can also see a frequently occurring word: SFR

SFR is not a standard C-language keyword, but Keil provides a new, direct access to the SFR in 80c51

Key words, the usage is:

SFRT Variable name = address value.

2) symbol P1_0 to represent the P1.0 pin.

In C, if the direct write P1.0,c compiler is not recognized, and P1.0 is not a legitimate C

Language variable name, so give it another name, here is named P1_0, but P1_0 is not P1.0

It? You think so, the C compiler doesn't think so, so they have to be contacted, Keil C is used here.

Keyword Sbit to define, there are three ways to use the Sbit:

First method: Sbit bit variable name = Address value

Second method: Sbit bit variable name =SFR name ^ variable bit address value

Third method: Sbit bit variable name =SFR address value ^ Variable bit address value

If you define the OV in the PSW, you can use the following three ways:

Sbit Ov=0xd2 (1) Description: 0xd2 is the bit address value of OV

Sbit ov=psw^2 (2) Description: Wherein the PSW must first be defined with SFR

Sbit ov=0xd0^2 (3) Description: 0xD0 is the address value of the PSW

So here is the SFR p1_0=p1^0, which is defined by the symbol P1_0 to represent the P1.0 pin, if you want to

From the P10 class name, as long as the following procedures are also changed on the line.

The following is attached reg52.h header file content:

/*--------------------------------------------------------------------------

REG52. H

Header file for generic 80C52 and 80C32 microcontroller.

Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.

All rights reserved.

--------------------------------------------------------------------------*/

#ifndef __reg52_h__

#define __reg52_h__

/* BYTE Registers */

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;

/* 8052 Extensions */

SFR t2con = 0xC8;

SFR rcap2l = 0xCA;

SFR rcap2h = 0xCB;

SFR TL2 = 0xCC;

SFR TH2 = 0xCD;

/* BIT Registers */

/* PSW */

Sbit CY = psw^7;

Sbit AC = psw^6;

Sbit F0 = psw^5;

Sbit RS1 = psw^4;

Sbit RS0 = psw^3;

Sbit OV = psw^2;

Sbit P = psw^0; 8052 only

/* TCON */

Sbit TF1 = tcon^7;

Sbit TR1 = tcon^6;

Sbit TF0 = tcon^5;

Sbit TR0 = tcon^4;

Sbit IE1 = tcon^3;

Sbit IT1 = tcon^2;

Sbit IE0 = tcon^1;

Sbit IT0 = tcon^0;

/* IE */

Sbit EA = ie^7;

Sbit ET2 = ie^5; 8052 only

Sbit ES = ie^4;

Sbit ET1 = ie^3;

Sbit EX1 = ie^2;

Sbit ET0 = ie^1;

Sbit EX0 = ie^0;

/* IP */

Sbit PT2 = ip^5;

Sbit PS = ip^4;

Sbit PT1 = ip^3;

Sbit PX1 = ip^2;

Sbit PT0 = ip^1;

Sbit PX0 = ip^0;

/* P3 */

Sbit RD = p3^7;

Sbit WR = p3^6;

Sbit T1 = p3^5;

Sbit T0 = p3^4;

Sbit INT1 = p3^3;

Sbit INT0 = p3^2;

Sbit TXD = p3^1;

Sbit RXD = p3^0;

/* SCON */

Sbit SM0 = scon^7;

Sbit SM1 = scon^6;

Sbit SM2 = scon^5;

Sbit REN = scon^4;

Sbit TB8 = scon^3;

Sbit RB8 = scon^2;

Sbit TI = scon^1;

Sbit RI = scon^0;

/* P1 */

Sbit T2ex = p1^1; 8052 only

Sbit T2 = p1^0; 8052 only

/* T2con */

Sbit TF2 = t2con^7;

Sbit EXF2 = t2con^6;

Sbit RCLK = t2con^5;

Sbit tclk = t2con^4;

Sbit EXEN2 = t2con^3;

Sbit TR2 = t2con^2;

Sbit c_t2 = t2con^1;

Sbit cp_rl2 = t2con^0;

#endif

Reg51.h and Reg52.h Analysis of the head file in MCU with C programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.