51 single-chip microcomputer (Puzhong HC6800-EM3 V3.0) experimental routine software analysis experiment point lights up the first LED

Table of contents

foreword

1. Schematic diagram and introduction of knowledge points

1.1. Schematic diagram of LED

1.2. Schematic diagram of MCU51

2. Code analysis

Knowledge point 1: #include "reg52.h" //This file defines some special function registers of the microcontroller

Knowledge point 2: Do you know how sfr P0 = 0x80; come from? Why do you assign the value 0x80?

Knowledge point 3: Where are sbit and sfr defined, why can they be used in keil, but not other compilation and development tools, and errors will be reported?


foreword

Because it is the first experiment, we will be a little bit verbose, and regard everyone as having a certain foundation.

How to use a development board to use it.

Of course it's the manual:

We need the following materials: development board schematic diagram, development manual, development tools


1. Schematic diagram and introduction of knowledge points

1.1. Schematic diagram of LED

In this section we study LED, we only need to pay attention to the schematic diagram of the LED part of the development board as shown below:

 J12 in the figure is an 8pin pin header interface, connected to a current limiting resistor through the 74HC245 chip and then connected to the anode of the LED (D11~D18)

analyze:

It can be seen from the figure that the cathodes of the LEDs are all grounded, we only need to control the anodes to control the on and off of the LEDs

  • High level is lit
  • Low level is off

Now just figure out how to use the chip 74HC245 ( the purpose of the 74HC245 chip_ManGo CHEN's blog-CSDN blog ), the following is the definition of the pins:

analyze:

From the combination of schematic diagram and table, it can be analyzed that:

  • A0-A7 is input, B0-B7 is output: OE: must be L (low level), DIR: must be H (high level)
  • The single-chip microcomputer indirectly controls the lighting and extinguishing of the LED by controlling A0-A7

1.2. Schematic diagram of MCU51

 Of course, there is also a part of the schematic diagram that is the MCU as shown below:

 We have nothing to analyze in this part of the schematic diagram. Of course, there are still many intriguing designs after careful consideration. If you are interested, you can refer to the blog post: Schematic Sharing 1 (MCU_CT107D Training Platform Instructions and Circuit Schematic Diagram) _ManGo CHEN's Blog - CSDN Blog

2. Code analysis

Let me introduce the project first:

Let's go directly to the code:

/**************************************************************************************
*		              点亮第一个LED实验												  *
实现现象:下载程序后D11指示灯点亮
注意事项:无																				  
***************************************************************************************/


#include "reg52.h"    //此文件中定义了单片机的一些特殊功能寄存器

sbit led=P0^0;	   //将单片机的P0.0端口定义为led

/*******************************************************************************
* 函 数 名       : main
* 函数功能		 : 主函数
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
void main()
{
	while(1)
	{
		led=1;	//P0.0端口设置为低电平
	}		
}

Here we will analyze some intriguing issues in our work. We will not discuss and analyze some more basic issues. Of course, you can discuss and study together in the comment area or private message me. 

Knowledge point 1: #include "reg52.h" //This file defines some special function registers of the microcontroller

Have you opened this header file? Let's open it together today to see what's in the header file:

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

In the above code, it is true that SFR defines special function registers, and sbit defines the function of a certain bit of these special function registers (bit addressing).

We mainly use the special function register P0 this time.

Knowledge point 2: Do you know how sfr P0 = 0x80; come from? Why do you assign the value 0x80?

Of course, it is obtained by reading the manual. What manual should I read? Of course, it is the MCU manual. From the manual, you can know the addresses of these special function registers:

 Last time I asked a graduate student this question, he could only answer lightly: It is stipulated in the library file. If you can find the root cause, then you have already started learning.

Knowledge point 3: Where are sbit and sfr defined, why can they be used in keil, but not other compilation and development tools, and errors will be reported?

 This problem is related to the development tools. sbit and sfr are not keywords in the C language, but belong to Keil's extended instructions.

  • sfr instruction: used to directly describe the hardware address, which can be understood as "the starting address of a group of IO ports (for example, sfr P0 = 0x80 //P0 is a group of IO ports)" (generally only use the IO ports of P1, P2, and P3 , the specific reason is related to the hardware, and will not be described here)
  • sbit command: corresponds to a bit of the addressable space, which can be understood as the address of "one IO port/pin" (for example, sbit led1=P0^0 //This means to find the first IO port of the group of IO ports in P0, and name it led1)

Don't ask me how I know, just tell you if you ask, check it through keil (details will not be expanded):

The rest of the code is C language, there is nothing to analyze, everyone digests it by themselves. Welcome to discuss together.

 

Guess you like

Origin blog.csdn.net/qq_42700289/article/details/132072542