爱心流水灯

爱心流水灯

lczhuigz Lv1

前言

今天要介绍的这款项目,是一个基于STC89C52RC单片机设计的多功能爱心形状LED流水灯系统。这个项目不仅能够实现多种绚丽的灯光效果,还可以作为装饰品或电子设计教学的工具。

项目简介

本项目采用了STC89C52RC单片机作为控制核心,精心设计并制作了一个爱心形状的LED显示装置。通过编写程序,我们实现了多种多样的灯光效果,使其既美观又实用。这个装置不仅可以用于装饰,还能作为教学工具,帮助初学者更好地理解单片机编程和电路设计。

功能特点

以下是一些主要的功能特点:

  • 全体LED同时闪烁:所有LED灯同时亮起和熄灭,营造出强烈的视觉冲击。
  • 顺时针流水灯效果:LED灯从左上方向右下方依次亮起,如同流水一般流动。
  • 逆时针流水灯效果:与顺时针效果相反,LED灯从右下方向左上方依次亮起。
  • 两侧同时流水效果:两侧的LED灯同时向中间流动,形成独特的光影效果。
  • 呼吸灯效果:LED灯的亮度逐渐变化,模拟呼吸时的明暗变化。
  • 随机闪烁效果:LED灯随机亮起和熄灭,带来意想不到的惊喜。

除了上述效果,系统还支持多种其他效果,如交叉闪烁、心跳效果、波浪效果、跑马灯效果、渐变闪烁和螺旋效果。这些效果使得装置更加丰富多彩,能够满足不同的使用需求。

项目结构

GitHub仓库/Gitee仓库项目的文件结构如下:

1
2
3
4
5
6
7
8
9
├── Keil_Project/            # 程序源代码
│ ├── main.c # 主程序代码
│ └── Objects/ # 编译生成文件
├── PCB_Project/ # PCB设计文件
│ ├── love-flow-lamp.PrjPcb # PCB项目文件
│ ├── love-flow-lamp.SchDoc # 原理图文件
│ ├── love-flow-lamp.PcbLib # PCB库文件
│ └── love-flow-lamp.PcbDoc # PCB图纸文件
└── 实物图片/ # 成品展示

开发环境

本项目使用了以下开发工具和环境:

  • IDE: Keil uVision 5
  • PCB设计工具: Altium Designer
  • 编程语言: C语言
  • 目标单片机: STC89C52RC

项目演示

以下是项目实物图片:

demo_pic

您可以在B站查看项目的详细演示视频:B站视频链接

项目源码

项目源码请参考GitHub仓库 Gitee仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

#include <REGX52.H>

typedef unsigned char uchar;
typedef unsigned int uint;

void Delay_ms(unsigned int xms);

void clock_wise(uint x);
void anticlock_wise(uint x);
void all_dance(void);
void two_flanks(uint x);
void breath_effect(void);
void cross_flash(void);
void random_flash(void);
void heartbeat(void);
void wave_effect(void);
void marquee(void);
void gradient_blink(void);
void spiral(void);
void rainbow_effect(void);
void flash_center(void);

static uint i;

void main()
{
while(1)
{
Delay_ms(500);
for (i = 60; i > 0; i -= 10)
{
clock_wise(i);
}
for (i = 60; i > 0; i -= 10)
{
anticlock_wise(i);
}
Delay_ms(60);
clock_wise(50);
Delay_ms(60);
anticlock_wise(50);
Delay_ms(60);
two_flanks(50);
Delay_ms(60);
breath_effect(); //呼吸效果
Delay_ms(60);
cross_flash(); //交叉闪烁
Delay_ms(60);
random_flash(); //随机闪烁
Delay_ms(60);
heartbeat(); //心跳效果
wave_effect(); //波浪效果
Delay_ms(60);
marquee(); //跑马灯
Delay_ms(60);
gradient_blink(); //渐变闪烁
Delay_ms(60);
spiral(); //螺旋闪烁
Delay_ms(60);
rainbow_effect(); //彩虹渐变效果
Delay_ms(60);
flash_center();
Delay_ms(60);
all_dance();
Delay_ms(100);
}
}

//12m晶振频率,毫秒级延时
void Delay_ms(uint xms)
{
uchar i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}

//全体led闪烁
void all_dance(void){
P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;
Delay_ms(600);
P0=0x00;P1=0x00;P2=0x00;P3=0x00;
Delay_ms(600);
}
//顺时针流水灯
void clock_wise(uint x){
P1=0x7F;Delay_ms(x);P1=0xBF;Delay_ms(x);P1=0xDF;Delay_ms(x);P1=0xEF;Delay_ms(x);
P1=0xF7;Delay_ms(x);P1=0xFB;Delay_ms(x);P1=0xFD;Delay_ms(x);P1=0xFE;Delay_ms(x);
P1=0xFF;Delay_ms(x);

P0=0xFE;Delay_ms(x);P0=0xFD;Delay_ms(x);P0=0xFB;Delay_ms(x);P0=0xF7;Delay_ms(x);
P0=0xEF;Delay_ms(x);P0=0xDF;Delay_ms(x);P0=0xBF;Delay_ms(x);P0=0x7F;Delay_ms(x);
P0=0xFF;Delay_ms(x);

P2=0x7F;Delay_ms(x);P2=0xBF;Delay_ms(x);P2=0xDF;Delay_ms(x);P2=0xEF;Delay_ms(x);
P2=0xF7;Delay_ms(x);P2=0xFB;Delay_ms(x);P2=0xFD;Delay_ms(x);P2=0xFE;Delay_ms(x);
P2=0xFF;Delay_ms(x);

P3=0x7f;Delay_ms(x);P3=0xBF;Delay_ms(x);P3=0xDF;Delay_ms(x);P3=0xEF;Delay_ms(x);
P3=0xF7;Delay_ms(x);P3=0xFB;Delay_ms(x);P3=0xFD;Delay_ms(x);P3=0xFE;Delay_ms(x);
P3=0xFF;Delay_ms(x);
}
//逆时针流水灯
void anticlock_wise(uint x){
P3=0xFE;Delay_ms(x);P3=0xFD;Delay_ms(x);P3=0xFB;Delay_ms(x);P3=0xF7;Delay_ms(x);
P3=0xEF;Delay_ms(x);P3=0xDF;Delay_ms(x);P3=0xBF;Delay_ms(x);P3=0x7F;Delay_ms(x);
P3=0xFF;Delay_ms(x);

P2=0xFE;Delay_ms(x);P2=0xFD;Delay_ms(x);P2=0xFB;Delay_ms(x);P2=0xF7;Delay_ms(x);
P2=0xEF;Delay_ms(x);P2=0xDF;Delay_ms(x);P2=0xBF;Delay_ms(x);P2=0x7F;Delay_ms(x);
P2=0xFF;Delay_ms(x);

P0=0x7F;Delay_ms(x);P0=0xBF;Delay_ms(x);P0=0xDF;Delay_ms(x);P0=0xEF;Delay_ms(x);
P0=0xF7;Delay_ms(x);P0=0xFB;Delay_ms(x);P0=0xFD;Delay_ms(x);P0=0xFE;Delay_ms(x);
P0=0xFF;Delay_ms(x);

P1=0xFE;Delay_ms(x);P1=0xFD;Delay_ms(x);P1=0xFB;Delay_ms(x);P1=0xF7;Delay_ms(x);
P1=0xEF;Delay_ms(x);P1=0xDF;Delay_ms(x);P1=0xBF;Delay_ms(x);P1=0x7F;Delay_ms(x);
P1=0xFF;Delay_ms(x);
}
//两侧同时流水
void two_flanks(uint x){
P1=0x7F;P3=0xFE;Delay_ms(x);P1=0xBF;P3=0xFD;Delay_ms(x);P1=0xDF;P3=0xFB;Delay_ms(x);P1=0xEF;P3=0xF7;Delay_ms(x);
P1=0xF7;P3=0xEF;Delay_ms(x);P1=0xFB;P3=0xDF;Delay_ms(x);P1=0xFD;P3=0xBF;Delay_ms(x);P1=0xFE;P3=0x7F;Delay_ms(x);
P1=0xFF;P3=0xFF;Delay_ms(x);

P0=0xFD;P2=0xFD;Delay_ms(x);P0=0xFE;P2=0xFE;Delay_ms(x);P0=0xFB;P2=0xFB;Delay_ms(x);P0=0xF7;P2=0xF7;Delay_ms(x);
P0=0xEF;P2=0xEF;Delay_ms(x);P0=0xDF;P2=0xDF;Delay_ms(x);P0=0xBF;P2=0xBF;Delay_ms(x);P0=0x7F;P2=0x7F;Delay_ms(x);
P0=0xFF;P2=0xFF;Delay_ms(x);
}

//呼吸灯效果(所有LED渐变亮度)
void breath_effect(void) {
uchar i;
for(i=0; i<5; i++) {
P0=0xFF<<i; P1=0xFF<<i; P2=0xFF<<i; P3=0xFF<<i;
Delay_ms(80);
}
for(i=5; i>0; i--) {
P0=0xFF<<i; P1=0xFF<<i; P2=0xFF<<i; P3=0xFF<<i;
Delay_ms(80);
}
}

//随机闪烁(随机位置LED闪烁)
void random_flash(void) {
uchar i;
for(i=0; i<8; i++) {
P0 = ~(0x01 << (i%8));
P1 = ~(0x01 << ((i+2)%8));
P2 = ~(0x01 << ((i+4)%8));
P3 = ~(0x01 << ((i+6)%8));
Delay_ms(80);
}
}

//交叉闪烁(左右交替)
void cross_flash(void) {
uchar i;
for(i=0; i<4; i++) {
P0=0xAA; P1=0xAA; P2=0x55; P3=0x55; // 左半亮右半灭
Delay_ms(200);
P0=0x55; P1=0x55; P2=0xAA; P3=0xAA; // 右半亮左半灭
Delay_ms(200);
}
P0=0xFF; P1=0xFF; P2=0xFF; P3=0xFF;
}

//心跳效果(快速闪烁三次)
void heartbeat(void) {
uchar i;
for(i=0; i<3; i++) {
P0=0x00; P1=0x00; P2=0x00; P3=0x00;
Delay_ms(200);
P0=0xFF; P1=0xFF; P2=0xFF; P3=0xFF;
Delay_ms(200);
}
}

//波浪效果(从中心向四周扩散)
void wave_effect(void) {
uchar i;
for(i=0; i<4; i++){
P0 = ~(0x18 >> i); // 00011000 右移
P1 = ~(0x18 >> i);
P2 = ~(0x18 << i); // 00011000 左移
P3 = ~(0x18 << i);
Delay_ms(120);
}
P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;
}

//跑马灯效果(四端口循环追逐)
void marquee(void) {
uchar i;
for(i=0; i<8; i++){
P0 = ~(0x80 >> i);
P1 = ~(0x80 >> (i+2)%8);
P2 = ~(0x80 >> (i+4)%8);
P3 = ~(0x80 >> (i+6)%8);
Delay_ms(150);
}
}

//渐变闪烁(不同频率闪烁)
void gradient_blink(void) {
uchar i;
for(i=0; i<6; i++){
P0=0x00;P1=0xFF;P2=0x00;P3=0xFF;
Delay_ms(100 + i*20);
P0=0xFF;P1=0x00;P2=0xFF;P3=0x00;
Delay_ms(100 + i*20);
}
}

//螺旋效果(内外交替旋转)
void spiral(void) {
uchar patterns[] = {0x7E,0xBD,0xDB,0xE7};
uchar i;
for(i=0; i<4; i++){
P0 = patterns[i];
P1 = patterns[(i+1)%4];
P2 = patterns[(i+2)%4];
P3 = patterns[(i+3)%4];
Delay_ms(200);
}
P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;
}

//彩虹渐变效果(简单的颜色变化)
void rainbow_effect(void) {
uchar patterns[] = {0xFF, 0x0C, 0xF3, 0x7E, 0x9F, 0x3F};
uchar i;
for(i=0; i<6; i++){
P0 = patterns[i];
P1 = patterns[(i+1)%6];
P2 = patterns[(i+2)%6];
P3 = patterns[(i+3)%6];
Delay_ms(150);
}
P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;
}

//闪烁中心点效果(四个端口的中间LED依次闪烁)
void flash_center(void) {
uchar i;
for(i=0; i<3; i++){
P0=0x80; P1=0x00; P2=0x00; P3=0x00;
Delay_ms(200);
P0=0x00; P1=0x80; P2=0x00; P3=0x00;
Delay_ms(200);
P0=0x00; P1=0x00; P2=0x80; P3=0x00;
Delay_ms(200);
P0=0x00; P1=0x00; P2=0x00; P3=0x80;
Delay_ms(200);
}
P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;
}

  • 标题: 爱心流水灯
  • 作者: lczhuigz
  • 创建于 : 2025-08-29 14:12:50
  • 更新于 : 2025-09-05 14:35:28
  • 链接: https://www.lczhuigz.top/2025/08/29/love-flow-lamp/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论