The development board I use is TX-1C of Tianxiang Electronics, and the microcontroller is STC89C52RC.
The following starts with the hardware connection diagram of eight light-emitting diodes and the microcontroller:

To make the light-emitting diode light up, a current needs to pass through it, about 5mA current is enough (the larger the current, the stronger the brightness, but if the current is too large, the diode will be burned out, generally controlled between 3-20mA.).
It can be seen from the above figure: when the Q0~Q7 terminals are low, the light-emitting diode will be lit. The light-emitting diode is connected to the P1 port of the microcontroller through the 74HC573 latch.
Therefore, in order to make the output terminals Q0~Q7 of 74HC573 output low level, the principle of unlocking the memory must be firstly understood. The truth table is as follows:

It can be known from the truth table that when the OE (output enable) terminal is low and the LE (latch enable) terminal is high, the states of the output terminals Q0~Q7 are consistent with the states of D0~D7.
Look at the schematic diagram again: the OE terminal is grounded, the LE terminal is connected to the P2^5 of the microcontroller, and D0~D7 are connected to the microcontroller P1^0~P1^7.
So as long as P2^5 is set to high level and P1^0~P1^7 are set to low level, Q0~Q7 can be obtained as low level. At this time, the light-emitting diode can be lit.
Next, discuss the size of the current through the light-emitting diode:
In Figure 1: Component P2 is a 1KΩ resistor, VCC terminal input +5V voltage, the voltage drop when the light-emitting diode is normally turned on is 1.7V, according to Ohm’s law U=IR can be obtained: 5V-1.7V=1KΩ * I, find I=3.3mA.
We can appropriately reduce the resistance value of the resistor to make the luminous tube brighter.
Let us light up the first LED, the code is as follows:
1 #include //Introduce 52 series MCU header files
2
3 sbit LED1 = P1^0; //Declare the first bit of the P1 port of the microcontroller 4
5 void main() //Main function
6 {
7 while(1) //Loop operation
8 {
9 LED1 = 0; //light up the first light-emitting diode
10}
11}
In the same way, other LEDs can be lit in the same way.
Now we come to discuss how to light up the luminous tube to make it appear the water light effect:
Principle: ①, light up the first luminous tube
②. Wait for a short period of time to turn on the second luminous tube and turn off the first luminous tube at the same time. ③. Similarly, the cycle continues
We have learned how to light up the LED above, so how to let the LED go out after a period of time? We write the following delay function:
1 void delay (uint ms)
2 {
3 uint i;
4 for (; ms》 0; ms –)
5 {
6 for (i = 114; i》 0; i –)
7 {
8 }
9 }
10}
We only need to call this delay function after lighting a light-emitting tube, and when the delay function is executed, we can turn off the light-emitting tube again.
Let’s analyze the situation when each different luminous tube is lit:
1. The first light-emitting tube is lit, and the level status of each bit of the P1 port of the single-chip microcomputer is: 11111110; 2. The second light-emitting tube is lighted, and the level status of each bit of the P1 port of the single-chip microcomputer is 11111101; 3. The third light-emitting tube is lit, and the level state of each bit of the P1 port of the microcontroller is: 11111011; 4. The fourth light-emitting tube is lit, and the level state of each bit of the P1 port of the microcontroller is 11110111 at this time; 5. The fifth light-emitting tube is lit, and the level status of each bit of the P1 port of the single-chip microcomputer is: 11101111; 6. The sixth light-emitting tube is lighted, and the level status of each bit of the P1 port of the single-chip microcomputer is 11011111 at this time; 7. The seventh light-emitting tube is lit, and the level status of each bit of the P1 port of the single-chip microcomputer is: 10111111; 8. The eighth light-emitting tube is lighted, and the level status of each bit of the P1 port of the single-chip microcomputer is: 01111111; The following rule can be obtained from the level state change of the P1 port: the next level value is obtained by shifting the previous level value by one bit to the left.
We can use the loop left shift function _crol_ in the header file intrinsics.h to achieve.
The following is the complete code of the water lamp:
1 #include
2 #include
3 #define uint unsigned int
4 void delay (uint);
5
6 void main()
7 {
8 P1 = 0xfe;
9
10 while(1)
11 {
12 delay(500);
13 P1 = _crol_(P1, 1);
14}
15}
16
17 void delay (uint ms)
18 {
19 uint i;
20
21 for (; ms》 0; ms –)
twenty two {
23 for (i = 114; i》 0; i–)
twenty four {
25
26}
27}
28}