51 single chip microcomputer, light up LED light and flash

Publisher:DataExplorerLatest update time:2022-07-25 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 1. The difference between the keywords sfr and sbit


        sfr is the byte address of the register, and sbit is the bit address of the bit-addressable register.


        For example: sfr P1 = 0x90;


        sbit A = P1^0 sbit B = P1^1;


2. The microcontroller pins are: power pin, crystal pin, reset pin, download pin, GPIO pin


The crystal pin is mainly used to provide an external clock to drive instruction execution. The GPIO pins include four ports: P0, P1, P2, and P3.


3. Light up an LED


From the circuit diagram, we can know that the LED is the P2 port. If we give the P2.0 pin a low voltage, that is, 0V, the first one will light up. The procedure is as follows       


#include"reg52.h" //reference header file


int main()

{

    P2 = 0xFE; //1111 1110 represents the output voltage status of P2.0----P2.7 pins


    while(1)

    {

    }


     return 0;

}


If I output low voltage to all pins of port P2, P2=0x00, then all LEDs will light up, as shown below


4. LED light flashes


The LED light is controlled to flash by outputting high and low levels, but due to the afterglow effect of the human eye, the flashing cannot be seen, so a delay is needed to achieve the effect. The program is as follows


#include"reg52.h" //reference header file


void Delay(int time)

{

    while(time--) //while loop takes 10us

    {

    }

}


int main()

{


    while(1)

    {

        P2 = 0xFF;

        Delay(50000); //Delay about 450ms

        P2 = 0xFE;

        Delay(50000); //Delay about 450ms 

    }


     return 0;

}


The experimental phenomena are as follows

Reference address:51 single chip microcomputer, light up LED light and flash

Previous article:51 single chip microcomputer: LED light water lamp
Next article:51 MCU assembly language: using RET instruction to implement multi-way branching

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号