51 MCU Learning - 1 - Light up the first LED

Publisher:清新时光Latest update time:2021-07-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Light up the LED

principle:

LED Schematic

The LED light of this single chip is a light emitting diode, which can only pass in one direction. In the figure, the cathode of the LED is connected to the IO port, and the anode is connected to VCC, that is, the power supply, which is a high level. Under normal circumstances, the P2 port outputs a high level by default, so you only need to make the P2 port output a low level.


Code:

#include


sbit led=P2^0; //Through bit definition, let led represent port P20


void main()

{

  while(1)

  {

  led=0; //Set P20 to low level

  }

}


Summarize:

Knowledge points:


Set pin sbit name = Px^x (IO port)

Make the IO port corresponding to the target LED light output low level


expand:

You can also directly assign values ​​to all IO ports of P2. That is, 1111 1111 from high to low corresponds to 1 to 8 LED lights from left to right. If you want to light it up, set the 1 in that position to 0, and then convert it to hexadecimal. For example, to light up all LEDs, set P2=0x00.


At this time, you may see movement on the digital tube, which is normal because P2^2, 3, 4 correspond to the bit selection of the digital tube.

Reference address:51 MCU Learning - 1 - Light up the first LED

Previous article:51 MCU Learning——2--LED Flashing + Flowing Water
Next article:51 MCU Study Notes [Part 3] - Digital Tube Experiment

Recommended ReadingLatest update time:2024-11-15 10:15

The cost of sky-high priced Micro LED TVs is expected to be halved, will the market explode next year?
High costs have always restricted the development of the Micro LED market. The starting price of the new Micro LED TV released by Leyard recently is 880,000 yuan, which is prohibitive for most people. However, industry insiders recently predicted that the cost of Micro LED will drop by more than 50% next year, and the
[Mobile phone portable]
LED damage causes and circuit protection methods
White light LEDs are voltage-sensitive devices. In actual operation, the upper limit of the current is 20mA. However, the current often increases due to various reasons during use. If no protective measures are taken, the LED will be damaged after the increased current exceeds a certain time and amplitude.   The mai
[Power Management]
A brief discussion on the design and application of edge-lit LED backlight technology
The edge-lit type places LEDs on the upper and lower sides of the backlight. The layout adjustment greatly reduces the use of LEDs, thereby reducing production costs and energy consumption, which is of great benefit to manufacturers and consumers. However, its weakness of not being able to dim locally has led to the b
[Analog Electronics]
A brief discussion on the design and application of edge-lit LED backlight technology
PIC12F508 microcontroller uses buttons to control LED lights C language program
#include #include "main.h" __CONFIG(MCLRDIS&WDTDIS&UNPROTECT&INTRC); //Set configuration bits (MCLR pin reset disabled, watchdog off, no code protection, internal RC oscillator) uint count=0; uchar Key_Flag=0; void Delay_US(uchar delay) {    while(delay--); } void Delay_Ms(uchar delay) {    flying
[Microcontroller]
Set the LED color tone from red to green
  The circuit in Figure 1 can create 32° hue of light using red and green LEDs . A constant current is divided into two parts. One part flows through a red LED and the other part flows through a green LED. The current through the red LED can be changed from 0~%, thereby also changing the complementary green LED curren
[Power Management]
Set the LED color tone from red to green
Section 2: delay() delays the flashing of LED lights
Opening remarks:     In the previous section, Brother Hong listed seven major misunderstandings of beginners. What is the core concern of beginners? That is the program structure running on the bare metal. A good program structure is itself a miniature multi-tasking operating system. What Brother Hong teaches everyone
[Microcontroller]
NXP_LPC1114_LED light program
#include "LPC11XX.h" #include "gpio.h" #include "main.h" void Delay_Sec(u8 Time) { u16 and; while(Time) { for(i=0;i 3000000;i++); Time--; } } void Delay_Ms(u16 Time) { u16 and; while(Time) { for(i=0;i 3000;i++); Time--; } } int main(void) { SystemInit(); //System initialization - mainly setting the mai
[Microcontroller]
How does LED lamp bead packaging affect the quality of LED display?
    For full-color LED display screens, LED lamp beads are the most critical components, and the quality of LED lamp beads plays a very important role in determining the quality of LED display screens.   First of all, LED lamp beads are used in the largest number in the entire full-color LED display screen, with tho
[Power Management]
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号