WS2811程序 RGB模块程序 电子积木代码

/***************************************************
WS2811:
“0码” T0H:350ns
T0L:800ns
“1码” T1H:700ns
T1L:600ns
RES:>50us
STC15F104
CLOCK:20MHZ 时钟周期50ns 机器周期200ns
****************************************************/
#include
#include
#define nop _nop_();
#define uchar unsigned char
#define uint unsigned int
#define speed 5 //刷新数据时间变量
#define size 65//数据显示个数变量
sbit DIO=P0^0; //数据输出引脚声明
uint t;
uchar bdata LED_DAT; //可位操作的数据发送暂存变量声明
sbit bit0=LED_DAT^0; //被发送的数据各位定义
sbit bit1=LED_DAT^1;
sbit bit2=LED_DAT^2;
sbit bit3=LED_DAT^3;
sbit bit4=LED_DAT^4;
sbit bit5=LED_DAT^5;
sbit bit6=LED_DAT^6;
sbit bit7=LED_DAT^7;

uchar RR,GG,BB; //RGB灰度值全局变量声明

//1ms延时函数============================================
void delay_1ms(uint z)
{

unsigned char a,b;
while(z--)
{

for(b=118;b>0;b--)
for(a=18;a>0;a--);

}

}

//=============低速模式数码BIT0(高电平时间:350ns 低电平时间:800ns )=============//
void h_dat0()
{

DIO=1;
nop;
DIO=0;
nop; nop; nop;

}
//=============低速模式数码BIT1(高电平时间:700ns 低电平时间:600ns )=============//
void h_dat1()
{

DIO=1;
nop; nop; nop;
DIO=0;
nop; nop;

}
//RGB显示控制程序=============================================
void Reset(void)
{

DIO=0;
delay_1ms(1);

}
//===================发送RGB灰度数据===================
void send_single_data() //数据格式:G7~G0~R7~R0~B7~B0
{

LED_DAT=GG;
if(bit7==1) h_dat1(); else h_dat0();
if(bit6==1) h_dat1(); else h_dat0();
if(bit5==1) h_dat1(); else h_dat0();
if(bit4==1) h_dat1(); else h_dat0();
if(bit3==1) h_dat1(); else h_dat0();
if(bit2==1) h_dat1(); else h_dat0();
if(bit1==1) h_dat1(); else h_dat0();
if(bit0==1) h_dat1(); else h_dat0();
LED_DAT=RR;
if(bit7==1) h_dat1(); else h_dat0();
if(bit6==1) h_dat1(); else h_dat0();
if(bit5==1) h_dat1(); else h_dat0();
if(bit4==1) h_dat1(); else h_dat0();
if(bit3==1) h_dat1(); else h_dat0();
if(bit2==1) h_dat1(); else h_dat0();
if(bit1==1) h_dat1(); else h_dat0();
if(bit0==1) h_dat1(); else h_dat0();
LED_DAT=BB;
if(bit7==1) h_dat1(); else h_dat0();
if(bit6==1) h_dat1(); else h_dat0();
if(bit5==1) h_dat1(); else h_dat0();
if(bit4==1) h_dat1(); else h_dat0();
if(bit3==1) h_dat1(); else h_dat0();
if(bit2==1) h_dat1(); else h_dat0();
if(bit1==1) h_dat1(); else h_dat0();
if(bit0==1) h_dat1(); else h_dat0();

}

void send_string_data()
{

uchar i;
for(i=0;i{

send_single_data();

}
Reset();


}


void RGB_open() //RGB闪烁
{

RR=255; GG=0; BB=0;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

RR=0; GG=255; BB=0;
send_string_data();
for(t=speed;t>0;t--) de

lay_1ms(100);

RR=0; GG=0; BB=255;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

RR=255; GG=255; BB=0;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

RR=255; GG=0; BB=255;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

RR=0; GG=255; BB=255;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

RR=255; GG=255; BB=255;
send_string_data();
for(t=speed;t>0;t--) delay_1ms(100);

}
//=======================RGB呼吸灯=======================//
void RGB_PWM()
{

uint i;
RR=0; GG=0; BB=0;
for(i=0; i<256; i++) //红色渐亮
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
RR++;

}
RR=255; GG=0; BB=0;
for(i=0; i<256; i++) //红色渐灭
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
RR--;

}
RR=0; GG=0; BB=0;
/****************************************************/

for(i=0; i<256; i++) //绿色渐亮
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
GG++;

}
RR=0; GG=255; BB=0;
for(i=0; i<256; i++) //绿色渐灭
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
GG--;

}
RR=0; GG=0; BB=0;
/****************************************************/
for(i=0; i<256; i++) //蓝色渐亮
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
BB++;

}
RR=0; GG=0; BB=255;
for(i=0; i<256; i++) //蓝色渐灭
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
BB--;

}
RR=0; GG=0; BB=0;
/****************************************************/
for(i=0; i<256; i++) //白色渐亮
{

send_string_data(); //发送RGB灰度数据
Reset();
delay_1ms(1);
RR++;
GG++;
BB++;

}
RR=255; GG=255; BB=255;
for(i=0; i<256; i++) //白色渐灭
{

send_string_data(); //发送RGB灰度数据
Reset();
RR--;
GG--;
BB--;

}
RR=0; GG=0; BB=0;

}


void main()
{

delay_1ms(50); //延时100毫秒等待所有MCU复位
while(1)
{

RGB_open();
RGB_PWM();

}


}

相关主题
相关文档
最新文档