#include "main.h"
#include <STC12C5A60S2.h>
//#define MAIN_Fosc 24000000L //定义主时钟
#define WHITE 0xFFFFFF // 白色
#define BLACK 0x000000 // 黑色
#define RED 0x00ff00 // 红色
#define GREEN 0xff0000 // 绿色
#define BLUE 0x0000ff // 蓝色
#define CHENG 0xff7f00 // 橙色
#define DIAN 0xff00ff //
#define ZI 0x00ffff
int LED_N = 5; //LED的数量
sbit LED=P1^1;
//extern unsigned long WsDat[];
unsigned long WsDat[];
void jieshu(unsigned int js) //延时
{ LED_N=js;
ColorToColor(0X000000,0XFF0000);
//ColorToColor(0XFF0000,0xFFFF00);
//ColorToColor(0xFFFF00,0Xffffff);
//ColorToColor(0Xffffff,0X00ffff);
//ColorToColor(0X00ffff,0X0000ff);
//ColorToColor(0X0000ff,0X000000);
//ColorToColor(0X000000,0X00FF00);
//ColorToColor(0X00FF00,0x0FFFF0);
//ColorToColor(0x0FFFF0,0Xffffff);
//ColorToColor(0Xffffff,0XFF00ff);
//ColorToColor(0XFF00ff,0XF0000f);
//ColorToColor(0XF0000f,0X000000);
}
void qm() //延时
{ int i,color;
unsigned char Red1, Green1, Blue1;
for(i=0; i<1; i++)
{
Red1 = 0;
Green1 = 0;
Blue1 = 0;
color = (((unsigned long)Green1<<16)&0XFF0000) | ((Red1<<8)&0XFF00) | Blue1&0XFF; // 合成 绿红蓝
// WsDat[i] = color;
DisP_ALLColor(color) ;
//DisP_Color(color);
delay(1) ; // 渐变速度 原始值15
}
}
void main ()//主函数
{
int bh=1;
reset();
//DisP_R_G_B(20);
while(1)
{
jieshu(bh);
bh++;
if(bh==7)
{
qm();
while(1)
{
bh--;
jieshu(bh);
qm();
if(bh==0)
{
break;
}
}
}
}
}
void delay(unsigned int time) //延时
{ unsigned int i,j,k;
for(j=0;j<time;j++)
for(i=0;i<time;i++)
for(k=0;k<time;k++)
Delay();
}
void Delay() //@24.000MHz
{
_nop_();
}
void reset(void)
{
int i;
LED=0;
for(i=0;i<50;i++)
{Delay();
}
}
void T0(void)
{
LED=1;
LED=0;
Delay();
}
void T1(void)
{
LED=1;
Delay();
LED=0;
}
void DisP_Color(unsigned long dat) //控制一个灯
{ unsigned int i;
for(i=0;i<24;i++)
{ if(0x800000 == (dat & 0x800000)) T1();
else T0();
dat<<=1; //左移一位
}
}
void DisP_ALLColor(unsigned long dat) //控制所有灯
{int j;
for(j=0;j<LED_N;j++) DisP_Color(dat);
reset();
}
void DisP_R_G_B(unsigned int j) //RGB渐变
{
unsigned long i=0;
while(i<0xffffff)
{
if(i<0xff) DisP_ALLColor(i++);
else if(i<0xffff) { i=i+0x100;DisP_ALLColor(i); }
else if(i<0xffffff) {i=i+0x10000;DisP_ALLColor(i); }
delay(j);
}
while(i>0)
{
if(i>0xffff00) DisP_ALLColor(i--);
else if(i>0xff0000) {i=i-0x100; DisP_ALLColor(i);}
else if(i>0) {i=i-0x10000;DisP_ALLColor(i);}
delay(j);
}
reset();
}
unsigned char abs0(int num)
{
if(num>0) return num;
num = -num;
return (unsigned char) num;
}
unsigned long ColorToColor(unsigned long color0, unsigned long color1)
{
unsigned char Red0, Green0, Blue0; // 起始三原色
unsigned char Red1, Green1, Blue1; // 结果三原色
int RedMinus, GreenMinus, BlueMinus; // 颜色差(color1 - color0)
unsigned char NStep; // 需要几步
float RedStep, GreenStep, BlueStep; // 各色步进值
unsigned long color; // 结果色
unsigned char i;
// 绿 红 蓝 三原色分解
Red0 = color0>>8;
Green0 = color0>>16;
Blue0 = color0;
Red1 = color1>>8;
Green1 = color1>>16;
Blue1 = color1;
// 计算需要多少步(取差值的最大值)
RedMinus = Red1 - Red0;
GreenMinus = Green1 - Green0;
BlueMinus = Blue1 - Blue0;
NStep = ( abs0(RedMinus) > abs0(GreenMinus) ) ? abs0(RedMinus):abs0(GreenMinus);
NStep = ( NStep > abs0(BlueMinus) ) ? NStep:abs0(BlueMinus);
// 计算出各色步进值
RedStep = (float)RedMinus / NStep;
GreenStep = (float)GreenMinus / NStep;
BlueStep = (float)BlueMinus / NStep;
// 渐变开始
for(i=0; i<NStep; i++)
{
Red1 = 255; //R
Blue1 = 255; //G
Green1 = 255; //B
color = (((unsigned long)Green1<<16)&0XFF0000) | ((Red1<<8)&0XFF00) | Blue1&0XFF; // 合成 绿红蓝
// WsDat[i] = color;
DisP_ALLColor(color) ;
//DisP_Color(color);
delay(5) ; // 渐变速度 原始值15
}
return color;
}
ws2811.zip_STC12C5A60S2灯_WS2811 51_s2 afast_thirdzk4_ws2811 stc
版权申诉
121 浏览量
2022-07-14
19:08:31
上传
评论
收藏 39KB ZIP 举报

林当时
- 粉丝: 77
- 资源: 1万+
最新资源
- sm2.js,前端加密算法,主要方法sm2EncryptPwd
- 人工智能-项目实践-jira-Python Jira library. Development chat
- Python俄罗斯方块Tetris源文件下载
- 基于Java 实现的LFU算法,特别适合新手,带有测试case
- 基于Java实现的LRU算法,特别适合新手,带有测试case
- 人工智能-项目实践-数据结构-冒泡排序、选择排序、快速排序、堆排序、插入排序、希尔排序、归并排序.zip
- 基于SpringBoot+Vue实现增删改查和分页查询DEMO(源码+数据库)作业
- C++ OnnxRuntime部署yolov8模型
- 人工智能-项目实践-数据结构-冒泡排序;直接插入排序;希尔排序;快速排序;堆排序;归并排序;基数排序.zip
- 人工智能-项目实践-数据结构-二叉树的层序遍历(左-右).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0