Teach you how to learn 51 single chip microcomputer - light up LED

Publisher:爱笑的猫咪Latest update time:2024-08-22 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Learn by doing. If you just read the tutorial without actual operation, your understanding of programming language will be very vague, so I decided to learn C language from microcontrollers.

#include //Included header file.

sbit LED = P0^0; //Here is the bit operation on the P0 register. It is equivalent to giving a new name to the P0^0 bit, LED. In fact, this block can be included in the header file to avoid repeated operations.
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

void main()
{
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;

LED = 0;
while(1);
}

The above program can light up a small LED light on the KingST C52 microcontroller learning board.

Attached is the schematic diagram of the microcontroller


#include

Indicates that it contains the special function registers and bit definitions of the C52 microcontroller.


Detailed explanation of 51 MCU header file reg51.h

When we program in C language, the first line is often the header file. The 51 microcontroller is reg51.h or reg52.h. The 51 microcontroller is relatively simple, and there is not much content in the header file. The header files of microcontrollers such as Freescale and ARM series often have a lot of content. Despite this, for some friends who are new to microcontrollers, the 51 header file is still not very clear. Let's explain it in detail today.


1) "File inclusion" processing concept

The so-called "file inclusion" means that all the contents of another file are included in one file. Because some definitions and commands in the included file are used very frequently and may be used in almost every program, in order to improve programming efficiency and reduce the programmer's work, these definitions and commands are combined into a separate file, such as reg51.h, and then included with #include. This is equivalent to standard parts in industry and can be used directly.

2) Reasons for register address and bit address declaration
reg51.h mainly contains address declarations of some special function registers. For bit-addressable registers, it also includes some bit address declarations, such as sfr P1=0x80; sfr IE=0xA8; sbit EA=0xAF, etc.

sfr P1 = 0x90 means that the address of the special function register P1 corresponding to port P1 in the memory is 0x80, and sbit EA = 0xAF means that the address of the EA bit is 0xAF.
Note that there is a very frequently used sfr and sbit here.

sfr stands for special function register. It is not a keyword in standard C language, but a new keyword provided by Keil for direct access to special function registers in 80C51. Its usage is: sfr special function register name = address value (Note that users can actually modify the "special function register name" in the header file, such as P1=0x80, or A1=0x80, but sfr and address value cannot be changed, otherwise compilation errors will occur.) sbit
stands for bit. It is also a keyword in non-standard C language. When writing a program, if you need to operate a certain bit of a register (only bit-addressable registers can be used), you need to define a bit variable. At this time, you need sbit, such as sbit deng=P1^0, sbit EA = 0xAF; It should be noted that there are some special uses for bit definition:

The first method: sbit bit variable name = register bit address value

The second method: sbit bit variable name = SFR name ^ register bit value (0-7)

The third method: sbit bit variable name = SFR address value ^ register bit value

like:

sbit IT0=0x88 (1) Description: 0x88 is the bit address value of IT0

sbit deng=P1^2 (2) Note: P1 must be defined using sfr first

sbit EA=0xA8^7 (3) Explanation: 0xA8 is the address value of the IE register
. It should be noted that IT0 deng EA can be defined by the user at will, but it must meet the definition rules of the C language for variable names. Except for some, the others must be written in the above format, such as "name^variable bit address value" in "^", which is specified by the keil software and cannot be written in other ways. Only in this way can the compilation pass.
The above is an explanation of the definition and declaration of register addresses and bit addresses. Everyone needs to remember: only after declaring the address of the register and the related bits, can we assign related values ​​to them, and the keil software can be compiled. As for why, it may not be clear in one or two sentences.
3) Popular explanation of memory, SFR, bits, addresses, etc.

As mentioned above, the purpose of declaring register addresses and bit addresses (assuming bit addressing) is to tell the C compiler the addresses of the corresponding registers and their bits in memory, so that the variables and values ​​assigned to the registers and some bits can be correctly saved, and then can be correctly called by the CPU to complete the corresponding functions. The
above text mentions registers (SFR), bits, addresses, memory, etc., and the terms ROM, RAM, etc. will also appear in the process of learning single-chip microcomputers. You may find it difficult to understand. Here is a popular explanation, as shown in the following three figures.
We compare memory to a hotel. ROM, RAM, and SFR are equivalent to three different functional floors in the hotel (the specific number of floors in the hotel, that is, the number of ROM, RAM, and SFR, varies from hotel to hotel or from microcontroller to microcontroller). 8 rooms on each floor are equivalent to 8 bits. Each room is occupied by either men or women, which is equivalent to either putting a number 1 or a number 0 in each bit. The Keil compiler is equivalent to the hotel staff. When a traveler goes to stay in a hotel, it is equivalent to the process of writing a program. The hotel staff must tell the staff in advance which floor and room you are in (that is, declare the register address and bit address), and the hotel staff can take you to your room (here it is assumed that the hotel can be decided by the traveler himself). That is: only after declaring the address of the register and the related bit, can we assign relevant values ​​to it, and the Keil software can be compiled.

4) Original text and explanation of REG51.H header file
Open reg51.h and you can see some content like this (this file is usually under C:KEILC51INC. There are many header files in the root directory of the INC folder, and there are also many folders classified by company, which are also header files of related products. If we want to use the header file written by ourselves, we only need to copy the corresponding header file to the INC folder when using it.)
The original text of the header file is attached below, and the comment file is attached together.


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

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; //Three-state bidirectional IO port P0 This sentence means: the address of the special function register P0 is 0x80, which can be bit-addressed, the same below

//Low 8-bit address bus/data bus (usually not used but only used as a normal I/O port. Note that when used as an I/O port, a resistor must be connected to the hardware)

sfr P1 = 0x90; //quasi-bidirectional IO port P1


sfr P2 = 0xA0; //quasi-bidirectional IO port P2

//High 8-bit address bus, generally used as ordinary I/O

sfr P3 = 0xB0; //Dual function

//1. Quasi-bidirectional IO port P3 port

//2. P30 RXD serial data reception

// P31 TXD serial data transmission

// P32 external interrupt 0 signal application

// P33 external interrupt 1 signal application

// P34 Timer/Counter T0 External Count Pulse Input

// P35 Timer/Counter T1 External Count Pulse Input

// P36 WR External RAM write pulse signal input

// P37 RD external RAM read pulse signal input

sfr PSW = 0xD0; // Bit addressable (this register can be ignored when programming in C language)

//Program Status Register Program Status WORD (Program Status Information)

//psw.7(CY) carry flag

//psw.6(AC) auxiliary carry flag AC=1 when the lower four bits carry or borrow to the upper four bits

// Mainly used for decimal adjustment

//psw.5(F0) user-defined program flag

//psw.4(RS1)

//psw.3(RS0)

//Working register selection bit

//Only one set of registers is working at any one time

//0 0 0 zone 00H~07H

//0 1 1 zone 08H~0fH

//1 0 2 zone 10H~17H

//1 1 3 zone 18H~1FH

//psw.2(OV) overflow flag

//psw.1() is reserved and cannot be used

//psw.0(P) parity bit

sfr ACC = 0xE0; //Accumulator A Special function register Bit addressable

sfr B = 0xF0; //Register B is mainly used for multiplication and division operations

sfr SP = 0x81; //The stack pointer register SP stores the top address of the stack.

sfr DPL = 0x82; //

sfr DPH = 0x83; //Data pointer register DPTR, //Address pointer for accessing external RAM and extended IO

sfr PCON = 0x87; //Power control register, not bit addressable

// Manage the power supply of the microcontroller, including power-on reset, power-down mode, idle mode, etc.

//When the MCU is reset, PCON is cleared to 0. When programming, the SMOD bit is generally used, and the others are generally not used.

//D7 SMOD This bit is related to the serial communication baud rate

//SMOD=0 Serial port mode 1 2 3 Baud rate normal

//SMOD=1 Serial port mode 1 2 3 baud rate doubled

sfr TCON = 0x88; //Timer/Counter Control Register Bit Addressable
//D7 TF1 Timer 1 Overflow Flag

[1] [2]
Reference address:Teach you how to learn 51 single chip microcomputer - light up LED

Previous article:CS5532 C51 Driver
Next article:C language basics and running lights implementation

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号