Home » Let the LED flash

Let the LED flash

The previous article discussed how to light up the first LED. Today, let’s try making this LED blink.

Program Design Thoughts

We haven’t learned about the clock inside the microcontroller yet. How can we roughly determine the duration of each blink? Or in other words, how can we control when it turns on and off?

I can offer a thought process here: as long as we let the program refrain from changing the LED pin’s voltage level for a while, we can control its on and off states. So, what if we first change the voltage level, let the program run something else for a while, and then come back to change the pin’s voltage level again?

In fact, this approach is feasible. We can design an empty loop with for or while, letting it loop millions of times, which would serve the purpose of delaying time. However, this method is not precise because the time taken to run the loop each time is also variable, so we can only roughly achieve the effect of the LED blinking on and off.

Creating the Project

First, we create a project. For details on how to create the project, you can refer to this article: https://global.tylerhong.cn/light-on-the-first-led/

After complete creating the project

Writing the Code

  1. Include the header file
    Import the required header file library “reg52.h”

    include reg52.h

  2. Assigning a variable name to the pin
    On my development board, the P2^0 pin is connected to the first D1 LED, so I’ll define a variable name LED1 to the P2^0 pin.

    LED D1

    Create a name for P2^0

  3. Writing the main function

    main function

  4. Writing the time_delay function

    time_delay

  5. Complete code implementation

    complete

Compiling and Burning

For specific details on how to compile and burn the code, you can refer to this article: https://global.tylerhong.cn/light-on-the-first-led/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top