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/
Writing the Code
Include the header file
Import the required header file library “reg52.h”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.Writing the main function
Writing the time_delay function
Complete code implementation
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/