52 microcontroller IO port output lights up your LED

Publisher:心有归属Latest update time:2022-10-27 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Special function register SFR declaration

If we want to use the IO port of the microcontroller, 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)

Microcontroller C language SFR statement:


sfr P0 = 0x80;

sfr P1 = 0x90;

sfr P2 = 0xA0;

sfr P3 = 0xB0; //Define P0~P3


After defining it, by controlling P0~P1, we can control their corresponding 8 IO ports at the same time. Of course, this time we only need to light up one or two LEDs and only need to use one or two IO ports, 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; // Define the 8 IO ports of P1 respectively


Note: The IO port must be defined in capital letters.


head File

Earlier we used sfr to define the IO port. It would be very troublesome if we had to enter sfr one by one every time to define it. At this time, we can use the header file method instead of the previous method. The header file of the 52 microcontroller C language is .

This header file contains most of the definitions, which is convenient and fast. So why do we still need to 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 allows its corresponding IO port to send out a low-level signal, thereby lighting up the LED.


#include


//sfr P1 = 0x90; //Define P1 register address


sbit led0 = P1^0;//led0 1st led

sbit led7 = P1^7;//led7 8th led


void main()

{

// led0 = 0; //Low level lights up led0

// while(1);//Stop the program here (infinite loop)

while(1)

{

led0 = 0;

led7 = 0;

}

}


Below is the final result picture. In the black frame are the first and eighth LEDs we lit.

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

Reference address:52 microcontroller IO port output lights up your LED

Previous article:Four methods of 52 microcontroller to realize running water lamp
Next article:Microcontroller AT89C51--3. Light up the LED light

Recommended ReadingLatest update time:2024-11-16 15:56

LED alternative creative products
Ocean Nautilus LED Chandelier The Nautilus is a precious ancient relic species in the ocean, famous for its beautiful spiral texture. This LED chandelier draws inspiration from it, and treasures the seeds of light in the shape of the chandelier like the Nautilus, giving the wooden chandelier an oceanic feel. It uses
[Analog Electronics]
LED alternative creative products
Electronic load tester testing on LED driver power supply
In the research and development, production line and quality inspection departments of LED driver sources, it is necessary to be able to simulate the DC load of LED. Multi-channel electronic loads can provide professional CR-LED functions, which can perform complete and reliable tests on LED driver sources, greatly im
[Test Measurement]
A lighting solution for LED street lamps
LED street light Light distribution is to project light to every area of ​​the road as much as possible to obtain light distribution that meets the requirements of road lighting. Whether the light distribution design of LED street lights is accurate is one of the key factors in whether it can replace trad
[Power Management]
A lighting solution for LED street lamps
Example analysis of offline LED lighting design
In response to the application requirements of offline LEDs in general lighting, LED drivers must provide electrical isolation, high efficiency, PFC 0.90 and TRIAC dimming functions, as well as good LED current regulation to maintain consistent brightness . At the same time, LED drivers m
[Power Management]
Example analysis of offline LED lighting design
Timer 0 controls the flashing time of the LED; counter 1 controls the display of the digital tube
/* Timer 0 controls the flashing time of the LED; counter 1 controls the display of the digital tube */ #include reg52.h #define uint unsigned int #define uchar unsigned char sbit DULA = P2^6; sbit WELA = P2^7; sbit LED1 = P1^0; void display(uint i); void delay(uint z); uchar code arry = {0x3f,0x
[Microcontroller]
Design of LED display screen control system based on single chip microcomputer
This design uses dual RAM technology to organize the control system data for controlling the rectangular display screen , improves the memory efficiency when the information is displayed vertically , greatly reduces the occupancy rate of the data memory, and does not require a very high refresh ra
[Power Management]
Design of LED display screen control system based on single chip microcomputer
LED lighting and sensor technology analysis
LED lighting lamps switches automatic control sensors As devices for signal acquisition and electromechanical conversion, their electromechanical technology is quite mature. In recent years, the rise of MEMS technology has made great strides in sensor techno
[Power Management]
LED lighting and sensor technology analysis
Analyze the excellent solution of LED intelligent lighting monitoring system
Urban street lighting is an indispensable public facility in people's daily life. It is understood that the current power consumption of street lighting in China accounts for about 15% of the total power consumption. Faced with the tense situation of power supply, manual control and street light inspection have become
[Power Management]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号