52 MCU IO port output light up your LED

52 MCU IO port output lights up your LED [Getting Started\Detailed]

You are welcome! Here are my MCU study notes, I hope my notes can be helpful to you on your way of learning!

Special function register SFR declaration

If we want to use the IO port of the single-chip microcomputer, we must first define its address. We can find the address corresponding to the IO port from the chip data (you can also search directly online)
STC89C52

SFR statement in C language of single chip microcomputer:

sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;//定义P0~P3

After the definition, by controlling P0~P1, we can control their corresponding 8 IO ports at the same time. Of course, we only need to light up one or two LEDs this time, and only one or two IO ports are needed, so we can use the following The method defines an IO port separately.

sbit LED0 = P1 ^ 0;
sbit LED1 = P1 ^ 1;
sbit LED2 = P1 ^ 2;
sbit LED3 = P1 ^ 3;
sbit LED4 = P1 ^ 4;
sbit LED5 = P1 ^ 5;
sbit LED6 = P1 ^ 6;
sbit LED7 = P1 ^ 7;//分别定义P1的8个IO口

Note: define the IO port to uppercase

head File

Earlier we used sfr to define the IO port. It would be very troublesome to input sfr one by one each time. At this time, we can use the header file method to replace the previous method. The header file of the 52 single-chip C language is <reg52.h>.
This header file contains most of the definitions, which is convenient and quick. Then why talk about the sfr statement? In fact, it is to better understand the core content of the header file.

Code reference

Assigning a value of 0 to led0 can make its corresponding IO port send out a low-level signal to light up the led.

#include<reg52.h>

//sfr P1 = 0x90;//定义P1寄存器地址

sbit led0 = P1^0;//led0 第1个led
sbit led7 = P1^7;//led7 第8个led

void main()
{
    
    
//	led0 = 0;//低电平 点亮led0
//	while(1);//使程序停在这(死循环)
	while(1)
	{
    
    
		led0 = 0;
		led7 = 0;
	}
}

The following is the final result picture. The first and eighth LEDs we lighted are in the black frame.Results

Reference content link: https://www.bilibili.com/video/BV1Kt411Q7aV?p=4 .

嘿~我亲爱的伙计!我想慷慨大方的你一定不会吝啬给我这个可怜的博主点赞的。

Guess you like

Origin blog.csdn.net/chuxiqianye2020/article/details/112793602