51 microcontroller, lights up the LED light and flashes

 1. The difference between the keywords sfr and sbit

        sfr is the byte address that defines the register, and sbit is the bit address that defines the bit addressability.

        For example: sfr P1 =0x90;

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

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

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

3.Light up an LED light

        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 pin

    while(1)
    {
    }

     return 0;
}

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

 4.LED light flashes

        The LED lights are controlled to flicker by outputting high and low levels. However, due to the afterglow effect of the human eye, the flickering cannot be observed, so a delay is required to achieve the effect. The procedure is as follows

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

void Delay(int time)
{     while(time--) //The while loop takes 10us     {     } }



int main()
{

    while(1)
    {         P2 = 0xFF;         Delay(50000); //The delay is about 450ms         P2 = 0xFE;         Delay(50000); //The delay is about 450ms      }




     return 0;
}

The experimental phenomena are as follows

Guess you like

Origin blog.csdn.net/weixin_52300845/article/details/124317595
Recommended