stc15单片机流水灯代码
时间: 2023-11-22 14:06:14 浏览: 50
以下是STC15单片机流水灯的代码,使用了WS2812灯带:
```c
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED = P1^0;
// 定义WS2812灯带的数据格式
typedef struct {
uchar G;
uchar R;
uchar B;
} Color;
// 发送一个字节的数据到WS2812灯带
void sendByte(uchar dat) {
uchar i;
for (i = 0; i < 8; i++) {
if (dat & 0x80) {
LED = 1;
_nop_();
_nop_();
LED = 0;
} else {
LED = 1;
_nop_();
LED = 0;
_nop_();
}
dat <<= 1;
}
}
// 发送一个颜色数据到WS2812灯带
void sendColor(Color color) {
sendByte(color.G);
sendByte(color.R);
sendByte(color.B);
}
// 发送多个颜色数据到WS2812灯带
void sendColors(Color *colors, uint count) {
uint i;
for (i = 0; i < count; i++) {
sendColor(colors[i]);
}
}
// 流水灯效果
void flowLight(Color *colors, uint count, uint delay) {
uint i, j;
for (i = 0; i < count; i++) {
for (j = 0; j < count; j++) {
colors[j].R = 0;
colors[j].G = 0;
colors[j].B = 0;
}
colors[i].R = 255;
colors[i].G = 255;
colors[i].B = 255;
sendColors(colors, count);
delay_ms(delay);
}
}
void main() {
Color colors[12];
uint i;
for (i = 0; i < 12; i++) {
colors[i].R = 0;
colors[i].G = 0;
colors[i].B = 0;
}
while (1) {
flowLight(colors, 12, 100);
}
}
```