Customize the reg52.h header file (key knowledge points of microcontroller learning)

Publisher:Qingliu2022Latest update time:2024-08-14 Source: cnblogsKeywords:Custom Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Custom REG52 header file

@auth lei

@date 2017-05

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


#ifndef __REG52_H__

#define __REG52_H__


/* One byte register */

sfr P0 = 0x80; //Bidirectional IO, external output needs to be connected to a pull-up resistor

sfr P1 = 0x90; //quasi-bidirectional IO, as input, it must be set to 1 first

sfr P2 = 0xA0; //quasi-bidirectional IO, as input, it must be set to 1 first

sfr P3 = 0xB0; //quasi-bidirectional IO, as input, it must be set to 1 first, and has a second function

sfr PSW = 0xD0;

sfr ACC   = 0xE0;

sfr B     = 0xF0;

sfr SP    = 0x81;

sfr DPL   = 0x82;

sfr DPH   = 0x83;

sfr PCON  = 0x87;

sfr TCON = 0x88; //Interrupt control register

sfr TMOD = 0x89; //Timer working mode register

sfr TL0 = 0x8A; //Timer/Counter 0 low 8 bits

sfr TL1 = 0x8B; //Timer/Counter 1 low 8 bits

sfr TH0 = 0x8C; //Timer/Counter 0 high 8 bits

sfr TH1 = 0x8D; //Timer/Counter 1 high 8 bits

sfr IE = 0xA8; //Interrupt enable register

sfr IP = 0xB8; //Interrupt priority register

sfr SCON = 0x98;

sfr SBUF  = 0x99;


/* 8052 MCU extended registers */

sfr T2CON = 0xC8;

sfr RCAP2L = 0xCA;

sfr RCAP2H = 0xCB;

sfr TL2    = 0xCC;

sfr TH2    = 0xCD;



/* One bit register */

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


/*------------Detailed explanation of interruption content----------------------------

* Related registers

* 1.IE interrupt enable register

* 2.TCON timing control register

* 3.IP interrupt priority register (not commonly used)

* 4.TMOD timer working mode register (only used for timer/counter interrupt)

*Steps to use interrupts:

* 1. Interrupt initialization function (only need to be called once, do not put it in a loop and call it repeatedly)

* 2. Write interrupt handling function

* 3. Call the interrupt initialization function in the main function

*------------Detailed explanation of interruption content----------------------------/


/* TCON timing control register */

sbit TF1 = TCON^7; // Flag bit in timer/counter:

sbit TR1 = TCON^6; //Timer/counter 1 starts and stops:

sbit TF0 = TCON^5; //0 flag bit in timer/counter:

sbit TR0 = TCON^4; //Timer/Counter Interrupt 0 start, stop:

sbit IE1 = TCON^3; //External interrupt 1 interrupt flag: hardware set to 1 when an interrupt occurs, hardware set to 0 after the interrupt function is processed

sbit IT1 = TCON^2; //External interrupt 1 trigger mode: 0 for level, 1 for falling edge

sbit IE0 = TCON^1; //External interrupt 0 interrupt flag: hardware set to 1 when an interrupt occurs, hardware set to 0 after the interrupt function is processed

sbit IT0 = TCON^0; //External interrupt 0 trigger mode: 0 is level, 1 is falling edge


/* IE interrupt enable register */

sbit EA = IE^7; //interrupt master switch

sbit ET2 = IE^5; // 8052 specific

sbit ES = IE^4;

sbit ET1 = IE^3; //Timer/Counter Interrupt 1 Switch

sbit EX1 = IE^2; //External interrupt 1 switch

sbit ET0 = IE^1; //Timer/Counter Interrupt 0 Switch

sbit EX0 = IE^0; //External interrupt 0 switch


/* IP interrupt priority register */

sbit PT2 = IP^5;

sbit PS = IP^4;

sbit PT1 = IP^3;

sbit PX1 = IP^2;

sbit PT0 = IP^1;

sbit PX0 = IP^0;


/*------------------Detailed explanation of TMOD register-----------------

The 8 bits from high to low are: GATE C/T M1 M0 GATE C/T M1 M0

{----Configure timer 1----} {----Configure timer 0----}

GATE: Gating bit, the external pin (T0, T1) is used as the main switch to start the timer: when GATE=0, T0 and T1 are invalid; when GATE=1, the timer can only be started when T0 or T1 switch is turned on (high level)

C/T: Counting mode selection, when C/T=0, it is used as a timer, when C/T=1, it is used as a counter

M1, M0: working mode selection bit, 00 is working mode 0, 13-bit timer/counter, TH stores the upper 8 bits, TL stores the lower 5 bits

01 is working mode 1, 16-bit timer/counter, TH stores the upper 8 bits, TL stores the lower 8 bits (commonly used)

10 is working mode 2, 8-bit timer/counter with automatic initial value loading (commonly used)

11 is working mode 3, T0 is divided into two 8-bit independent counters, and T1 stops working

*------------------Detailed explanation of TMOD register-----------------/



/* The second function of P3 port */

sbit RD = P3^7; // external memory read strobe signal

sbit WR = P3^6; //External memory write select signal

sbit T1 = P3^5; // Start of external control timer/counter 1 (valid only when bit 7 of TMOD register GATE = 1), 1 means start, 0 means stop

sbit T0 = P3^4; // Start of external control timer/counter 0 (valid only when bit 3 of TMOD register GATE = 1), 1 means start, 0 means stop

sbit INT1 = P3^3; //External interrupt 1 input

sbit INT0 = P3^2; //External interrupt 0 input

sbit TXD = P3^1; //Serial output

sbit RXD = P3^0; //Serial input


/* SCON serial port control register */

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 port second function */

sbit T2EX = P1^1; // 8052 only

sbit T2 = P1^0; // 8052 specific

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


Keywords:Custom Reference address:Customize the reg52.h header file (key knowledge points of microcontroller learning)

Previous article:Homemade 51 MCU package library
Next article:Digital-to-Analog Converter ADC08009 Application

Latest Microcontroller Articles
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号