【单片机笔记】关于RGB彩灯WS2812B的驱动

前段时间做了个88颗灯珠的原形灯板,分别用stm8s003和n76e003写了底层驱动,

根据手册要求代码如下:

STM8S003

#include "fy_ws2812b.h"

/*

此代码针对stm8s003平台 @16M


灯的性质决定,需要一次性刷新,比如16个灯就必须一次性刷新16个,
而不能一个一个中间有间隔的刷新,这样就只有一地个灯亮,
如果需要花样把不需要亮的灯设置为不亮0x000000即可

*/

/*以下定义如果没有需要自行定义*/
//#define BYTE_0(n)                 ((u8)((n) & (u8)0xFF))        /*!< Returns the low color of the 32-bit value */
//#define BYTE_1(n)                 ((u8)(BYTE_0((n) >> (u8)8)))  /*!< Returns the second color of the 32-bit value */
//#define BYTE_2(n)                 ((u8)(BYTE_0((n) >> (u8)16))) /*!< Returns the third color of the 32-bit value */
//#define BYTE_3(n)                 ((u8)(BYTE_0((n) >> (u8)24)))
//#define enableInterrupts()	EA=1
//#define disableInterrupts()	EA=0

//根据平台定义
#define _nop_() asm("nop")

#define delay1NOP()	_nop_();
#define delay2NOP()	delay1NOP();_nop_();
#define delay3NOP()	delay2NOP();_nop_();
#define	delay4NOP()	delay3NOP();_nop_();
#define	delay5NOP()	delay4NOP();_nop_();
#define	delay6NOP()	delay5NOP();_nop_();
#define	delay7NOP()	delay6NOP();_nop_();
#define	delay8NOP()	delay7NOP();_nop_();
#define	delay9NOP()	delay8NOP();_nop_();
#define	delay10NOP()	delay9NOP();_nop_();
#define	delay11NOP()	delay10NOP();_nop_();
#define	delay12NOP()	delay11NOP();_nop_();
#define	delay13NOP()	delay12NOP();_nop_();
#define	delay14NOP()	delay13NOP();_nop_();
#define	delay15NOP()	delay14NOP();_nop_();

//引脚
#define RGB_PIN_H() GPIOD->ODR |= (uint8_t)GPIO_PIN_5;
#define RGB_PIN_L() GPIOD->ODR &= (uint8_t)(~GPIO_PIN_5)

void Ws2812b_Configuration(void)
{
    GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
    RGB_PIN_L();
}
#if 0
static void Ws2812b_Write0(void)
{
    RGB_PIN_H();
    delay6NOP();
    RGB_PIN_L();
}

static void Ws2812b_Write1(void)
{
    RGB_PIN_H();
    delay12NOP();
    
    RGB_PIN_L();
}

static void Ws2812b_WriteByte(u8 color)
{
	if(color & 0x80)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x40) 
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x20)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x10)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x8)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x4)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x2)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(color & 0x1)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
}
#else
/*程序进出都需要耗时的,用下面的方法替代上面更佳*/
static void Ws2812b_WriteByte(u8 color)
{
	if(color & 0x80){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
    if(color & 0x40){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x20){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x10){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x8) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x4) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x2) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x1) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
}

#endif
void Ws2812b_Write(u32 color)
{
#if 0
    Ws2812b_WriteByte(BYTE_1(color));//green
    Ws2812b_WriteByte(BYTE_2(color));//red
    Ws2812b_WriteByte(BYTE_0(color));//blue
#else
/*同理*/    
    //green
	if(color & 0x8000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
    if(color & 0x4000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x2000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x1000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x800) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x400) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x200) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x100) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }
    //red
	if(color & 0x800000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
    if(color & 0x400000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x200000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x100000){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x80000) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x40000) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x20000) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x10000) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }
    //blue
	if(color & 0x80){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
    if(color & 0x40){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x20){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x10){
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x8) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x4) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x2) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay7NOP();
    }	
	if(color & 0x1) {
        RGB_PIN_H();
        delay12NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay6NOP();
        RGB_PIN_L();
        delay2NOP();
        /*程序进出会有延时*/
        //delay7NOP();
    }    
#endif
}

/*********************************************以上为底层***********************************************/

//设置灯的颜色
//color:颜色(包含亮度)
//num  :总共的led数量
void RGB_Refresh(u32 color,u8 num){
	disableInterrupts();//关闭所有中断避免打扰
	while(num--){
		Ws2812b_Write(color);
	}
	enableInterrupts();
}

//颜色流水灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
void RGB_Cycle(u32 color,u8 num,u16 speed)
{
	u8 i;
	for(i=1;i<=num;i++){		//设置颜色		
		RGB_Refresh(color,i);
		Delay_ms(speed);
	}
	for(i=1;i<=num;i++){		//灭
		RGB_Refresh(0,i);
		Delay_ms(speed);
	}
}

//颜色呼吸灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
void RGB_Wave(u32 color,u8 num,u16 speed)
{
	u32 color_step=0;
	u32 color_temp = color;
	color_step |= (BYTE_2(color)?0x010000:0x000000);
	color_step |= (BYTE_1(color)?0x000100:0x000000);
	color_step |= (BYTE_0(color)?0x000001:0x000000);
    
	while(//先把颜色的亮度见到最小
          BYTE_2(color_temp)>=BYTE_2(color_step) &&
              BYTE_1(color_temp)>=BYTE_1(color_step) &&
                  BYTE_0(color_temp)>=BYTE_0(color_step)
                      ){
                          color_temp-=color_step;
                      }
	
	while(		//缓慢变亮	
          BYTE_2(color_temp)<0xff &&
              BYTE_1(color_temp)<0xff &&
                  BYTE_0(color_temp)<0xff
                      )	{
                          color_temp+=color_step;
                          RGB_Refresh(color_temp,num);
                          Delay_ms(speed);
                      }	
	while(//缓慢变暗
          BYTE_2(color_temp)>=BYTE_2(color_step) &&
              BYTE_1(color_temp)>=BYTE_1(color_step) &&
                  BYTE_0(color_temp)>=BYTE_0(color_step)
                      ){
                          color_temp-=color_step;
                          RGB_Refresh(color_temp,num);
                          Delay_ms(speed);
                      }
	RGB_Refresh(0,num);
}
//颜色流水灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
//cnt  :闪烁次数
void RGB_Flash(u32 color,u8 num,u16 speed,u8 cnt)
{
	while(cnt--){
		RGB_Refresh(color,num);
		Delay_ms(speed);
		RGB_Refresh(0,num);
		Delay_ms(speed);
	}
}

/*********************************************END OF FILE**********************************************/

N76E003:

#include "fy_ws2812b.h"

/*

此代码针对n76e003平台 @16M

1个nop 		占用87ns

while(1)	占用194ns

函数调用		占用570ns

灯的性质决定,需要一次性刷新,比如16个灯就必须一次性刷新16个,
而不能一个一个中间有间隔的刷新,这样就只有一地个灯亮

*/

/*以下定义如果没有需要自行定义*/
//#define BYTE_0(n)                 ((u8)((n) & (u8)0xFF))        /*!< Returns the low byte of the 32-bit value */
//#define BYTE_1(n)                 ((u8)(BYTE_0((n) >> (u8)8)))  /*!< Returns the second byte of the 32-bit value */
//#define BYTE_2(n)                 ((u8)(BYTE_0((n) >> (u8)16))) /*!< Returns the third byte of the 32-bit value */
//#define BYTE_3(n)                 ((u8)(BYTE_0((n) >> (u8)24)))
//#define enableInterrupts()	EA=1
//#define disableInterrupts()	EA=0


#define delay1NOP()	_nop_();
#define delay2NOP()	delay1NOP();_nop_();
#define delay3NOP()	delay2NOP();_nop_();
#define delay4NOP()	delay3NOP();_nop_();
#define delay5NOP()	delay4NOP();_nop_();
#define delay6NOP()	delay5NOP();_nop_();
#define delay7NOP()	delay6NOP();_nop_();
#define delay8NOP()	delay7NOP();_nop_();
#define delay9NOP()	delay8NOP();_nop_();
#define delay10NOP()	delay9NOP();_nop_();
#define delay11NOP()	delay10NOP();_nop_();
#define delay12NOP()	delay11NOP();_nop_();
#define delay13NOP()	delay12NOP();_nop_();
#define delay14NOP()	delay13NOP();_nop_();
#define delay15NOP()	delay14NOP();_nop_();



#define RGB_PIN_H() P06 = 1
#define RGB_PIN_L() P06 = 0

void Ws2812b_Configuration(void)
{
    P0M1 &= ~(1<<6);
    P0M2 |= (1<<6);
    RGB_PIN_L();
}

#if 0
static void Ws2812b_Write0(void)
{
    RGB_PIN_H();
    delay6NOP();
    RGB_PIN_L();
    
}

static void Ws2812b_Write1(void)
{
    RGB_PIN_H();
    delay12NOP();
    
    RGB_PIN_L();
}

static void Ws2812b_WriteByte(u8 byte)
{
	if(byte & 0x80)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x40) 
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x20)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x10)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x8)
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x4)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x2)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
	if(byte & 0x1)         
		Ws2812b_Write1();
	else
		Ws2812b_Write0();
}
#else
static void Ws2812b_WriteByte(u8 byte)
{
	if(byte & 0x80){
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
    if(byte & 0x40){
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x20){
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x10){
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x8) {
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x4) {
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x2) {
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
	if(byte & 0x1) {
        RGB_PIN_H();
        delay10NOP();
        RGB_PIN_L();
    }
	else{      
        RGB_PIN_H();
        delay4NOP();
        RGB_PIN_L();
        delay4NOP();
    }	
}

#endif

void Ws2812b_Write(u32 color)
{
    Ws2812b_WriteByte(BYTE_1(color));//green
    Ws2812b_WriteByte(BYTE_2(color));//red
    Ws2812b_WriteByte(BYTE_0(color));//blue
}

/*********************************************以上为底层***********************************************/

//设置灯的颜色
//color:颜色(包含亮度)
//num  :总共的led数量
void RGB_Refresh(u32 color,u8 num){
	disableInterrupts();
	while(num--){
		Ws2812b_Write(color);
	}
	enableInterrupts();
}

//颜色流水灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
void RGB_Cycle(u32 color,u8 num,u16 speed)
{
	u8 i;
	for(i=1;i<=num;i++){		//设置颜色		
		RGB_Refresh(color,i);
		Delay_ms(speed);
	}
	for(i=1;i<=num;i++){		//灭
		RGB_Refresh(0,i);
		Delay_ms(speed);
	}
}

//颜色呼吸灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
void RGB_Wave(u32 color,u8 num,u16 speed)
{
	u32 color_step=0;
	u32 color_temp = color;
	color_step |= (BYTE_2(color)?0x010000:0x000000);
	color_step |= (BYTE_1(color)?0x000100:0x000000);
	color_step |= (BYTE_0(color)?0x000001:0x000000);

	while(//先把颜色的亮度见到最小
			BYTE_2(color_temp)>=BYTE_2(color_step) &&
			BYTE_1(color_temp)>=BYTE_1(color_step) &&
			BYTE_0(color_temp)>=BYTE_0(color_step)
		){
		color_temp-=color_step;
	}
	
	while(		//缓慢变亮	
			BYTE_2(color_temp)<0xff &&
			BYTE_1(color_temp)<0xff &&
			BYTE_0(color_temp)<0xff
		 )	{
		color_temp+=color_step;
		RGB_Refresh(color_temp,num);
		Delay_ms(speed);
	}	
	while(//缓慢变暗
			BYTE_2(color_temp)>=BYTE_2(color_step) &&
			BYTE_1(color_temp)>=BYTE_1(color_step) &&
			BYTE_0(color_temp)>=BYTE_0(color_step)
		){
		color_temp-=color_step;
		RGB_Refresh(color_temp,num);
		Delay_ms(speed);
	}
	RGB_Refresh(0,num);
}
//颜色流水灯
//color:颜色(包含亮度)
//num  :总共的led数量
//speed:延时(单位ms)
//cnt  :闪烁次数
void RGB_Flash(u32 color,u8 num,u16 speed,u8 cnt)
{
	while(cnt--){
		RGB_Refresh(color,num);
		Delay_ms(speed);
		RGB_Refresh(0,num);
		Delay_ms(speed);
	}
}

/*********************************************END OF FILE**********************************************/

 

头文件:

#ifndef __FY_WS2812B_H
#define __FY_WS2812B_H

#include "fy_includes.h"


/*颜色宏定义*/								/*R G B*/
#define	COLOR_LIGHTPINK						0xFFB6C1	//	浅粉红
#define	COLOR_PINK							0xFFC0CB	//	粉红
#define	COLOR_CRIMSON						0xDC143C	//	猩红
#define	COLOR_LAVENDERBLUSH					0xFFF0F5	//	脸红的淡紫色
#define	COLOR_PALEVIOLETRED					0xDB7093	//	苍白的紫罗兰红色
#define	COLOR_HOTPINK						0xFF69B4	//	热情的粉红
#define	COLOR_DEEPPINK						0xFF1493	//	深粉色
#define	COLOR_MEDIUMVIOLETRED				0xC71585	//	适中的紫罗兰红色
#define	COLOR_ORCHID						0xDA70D6	//	兰花的紫色
#define	COLOR_THISTLE						0xD8BFD8	//	蓟
#define	COLOR_PLUM							0xDDA0DD	//	李子
#define	COLOR_VIOLET						0xEE82EE	//	紫罗兰
#define	COLOR_MAGENTA						0xFF00FF	//	洋红
#define	COLOR_FUCHSIA						0xFF00FF	//	灯笼海棠(紫红色)
#define	COLOR_DARKMAGENTA					0x8B008B	//	深洋红色
#define	COLOR_PURPLE						0x800080	//	紫色
#define	COLOR_MEDIUMORCHID					0xBA55D3	//	适中的兰花紫
#define	COLOR_DARKVOILET					0x9400D3	//	深紫罗兰色
#define	COLOR_DARKORCHID					0x9932CC	//	深兰花紫
#define	COLOR_INDIGO						0x4B0082	//	靛青
#define	COLOR_BLUEVIOLET					0x8A2BE2	//	深紫罗兰的蓝色
#define	COLOR_MEDIUMPURPLE					0x9370DB	//	适中的紫色
#define	COLOR_MEDIUMSLATEBLUE				0x7B68EE	//	适中的板岩暗蓝灰色
#define	COLOR_SLATEBLUE						0x6A5ACD	//	板岩暗蓝灰色
#define	COLOR_DARKSLATEBLUE					0x483D8B	//	深岩暗蓝灰色
#define	COLOR_LAVENDER						0xE6E6FA	//	薰衣草花的淡紫色
#define	COLOR_GHOSTWHITE					0xF8F8FF	//	幽灵的白色
#define	COLOR_BLUE							0x0000FF	//	纯蓝
#define	COLOR_MEDIUMBLUE					0x0000CD	//	适中的蓝色
#define	COLOR_MIDNIGHTBLUE					0x191970	//	午夜的蓝色
#define	COLOR_DARKBLUE						0x00008B	//	深蓝色
#define	COLOR_NAVY							0x000080	//	海军蓝
#define	COLOR_ROYALBLUE						0x4169E1	//	皇家蓝
#define	COLOR_CORNFLOWERBLUE				0x6495ED	//	矢车菊的蓝色
#define	COLOR_LIGHTSTEELBLUE				0xB0C4DE	//	淡钢蓝
#define	COLOR_LIGHTSLATEGRAY				0x778899	//	浅石板灰
#define	COLOR_SLATEGRAY						0x708090	//	石板灰
#define	COLOR_DODERBLUE						0x1E90FF	//	道奇蓝
#define	COLOR_ALICEBLUE						0xF0F8FF	//	爱丽丝蓝
#define	COLOR_STEELBLUE						0x4682B4	//	钢蓝
#define	COLOR_LIGHTSKYBLUE					0x87CEFA	//	淡蓝色
#define	COLOR_SKYBLUE						0x87CEEB	//	天蓝色
#define	COLOR_DEEPSKYBLUE					0x00BFFF	//	深天蓝
#define	COLOR_LIGHTBLUE						0xADD8E6	//	淡蓝
#define	COLOR_POWDERBLUE					0xB0E0E6	//	火药蓝
#define	COLOR_CADETBLUE						0x5F9EA0	//	军校蓝
#define	COLOR_AZURE							0xF0FFFF	//	蔚蓝色
#define	COLOR_LIGHTCYAN						0xE1FFFF	//	淡青色
#define	COLOR_PALETURQUOISE					0xAFEEEE	//	苍白的绿宝石
#define	COLOR_CYAN							0x00FFFF	//	青色
#define	COLOR_AQUA							0x00FFFF	//	水绿色
#define	COLOR_DARKTURQUOISE					0x00CED1	//	深绿宝石
#define	COLOR_DARKSLATEGRAY					0x2F4F4F	//	深石板灰
#define	COLOR_DARKCYAN						0x008B8B	//	深青色
#define	COLOR_TEAL							0x008080	//	水鸭色
#define	COLOR_MEDIUMTURQUOISE				0x48D1CC	//	适中的绿宝石
#define	COLOR_LIGHTSEAGREEN					0x20B2AA	//	浅海洋绿
#define	COLOR_TURQUOISE						0x40E0D0	//	绿宝石
#define	COLOR_AUQAMARIN						0x7FFFAA	//	绿玉\碧绿色
#define	COLOR_MEDIUMAQUAMARINE				0x00FA9A	//	适中的碧绿色
#define	COLOR_MEDIUMSPRINGGREEN				0x00FF7F	//	适中的春天的绿色
#define	COLOR_MINTCREAM						0xF5FFFA	//	薄荷奶油
#define	COLOR_SPRINGGREEN					0x3CB371	//	春天的绿色
#define	COLOR_SEAGREEN						0x2E8B57	//	海洋绿
#define	COLOR_HONEYDEW						0xF0FFF0	//	蜂蜜
#define	COLOR_LIGHTGREEN					0x90EE90	//	淡绿色
#define	COLOR_PALEGREEN						0x98FB98	//	苍白的绿色
#define	COLOR_DARKSEAGREEN					0x8FBC8F	//	深海洋绿
#define	COLOR_LIMEGREEN						0x32CD32	//	酸橙绿
#define	COLOR_LIME							0x00FF00	//	酸橙色
#define	COLOR_FORESTGREEN					0x228B22	//	森林绿
#define	COLOR_GREEN							0x008000	//	纯绿
#define	COLOR_DARKGREEN						0x006400	//	深绿色
#define	COLOR_CHARTREUSE					0x7FFF00	//	查特酒绿
#define	COLOR_LAWNGREEN						0x7CFC00	//	草坪绿
#define	COLOR_GREENYELLOW					0xADFF2F	//	绿黄色
#define	COLOR_OLIVEDRAB						0x556B2F	//	橄榄土褐色
#define	COLOR_BEIGE							0xF5F5DC	//	米色(浅褐色)
#define	COLOR_LIGHTGOLDENRODYELLOW			0xFAFAD2	//	浅秋麒麟黄
#define	COLOR_IVORY							0xFFFFF0	//	象牙
#define	COLOR_LIGHTYELLOW					0xFFFFE0	//	浅黄色
#define	COLOR_YELLOW						0xFFFF00	//	纯黄
#define	COLOR_OLIVE							0x808000	//	橄榄
#define	COLOR_DARKKHAKI						0xBDB76B	//	深卡其布
#define	COLOR_LEMONCHIFFON					0xFFFACD	//	柠檬薄纱
#define	COLOR_PALEGODENROD					0xEEE8AA	//	灰秋麒麟
#define	COLOR_KHAKI							0xF0E68C	//	卡其布
#define	COLOR_GOLD							0xFFD700	//	金
#define	COLOR_CORNISLK						0xFFF8DC	//	玉米色
#define	COLOR_GOLDENROD						0xDAA520	//	秋麒麟
#define	COLOR_FLORALWHITE					0xFFFAF0	//	花的白色
#define	COLOR_OLDLACE						0xFDF5E6	//	老饰带
#define	COLOR_WHEAT							0xF5DEB3	//	小麦色
#define	COLOR_MOCCASIN						0xFFE4B5	//	鹿皮鞋
#define	COLOR_ORANGE						0xFFA500	//	橙色
#define	COLOR_PAPAYAWHIP					0xFFEFD5	//	番木瓜
#define	COLOR_BLANCHEDALMOND				0xFFEBCD	//	漂白的杏仁
#define	COLOR_NAVAJOWHITE					0xFFDEAD	//	纳瓦霍白
#define	COLOR_ANTIQUEWHITE					0xFAEBD7	//	古代的白色
#define	COLOR_TAN							0xD2B48C	//	晒黑
#define	COLOR_BRULYWOOD						0xDEB887	//	结实的树
#define	COLOR_BISQUE						0xFFE4C4	//	(浓汤)乳脂,番茄等
#define	COLOR_DARKORANGE					0xFF8C00	//	深橙色
#define	COLOR_LINEN							0xFAF0E6	//	亚麻布
#define	COLOR_PERU							0xCD853F	//	秘鲁
#define	COLOR_PEACHPUFF						0xFFDAB9	//	桃色
#define	COLOR_SANDYBROWN					0xF4A460	//	沙棕色
#define	COLOR_CHOCOLATE						0xD2691E	//	巧克力
#define	COLOR_SADDLEBROWN					0x8B4513	//	马鞍棕色
#define	COLOR_SEASHELL						0xFFF5EE	//	海贝壳
#define	COLOR_SIENNA						0xA0522D	//	黄土赭色
#define	COLOR_LIGHTSALMON					0xFFA07A	//	浅鲜肉(鲑鱼)色
#define	COLOR_CORAL							0xFF7F50	//	珊瑚
#define	COLOR_ORANGERED						0xFF4500	//	橙红色
#define	COLOR_DARKSALMON					0xE9967A	//	深鲜肉(鲑鱼)色
#define	COLOR_TOMATO						0xFF6347	//	番茄
#define	COLOR_MISTYROSE						0xFFE4E1	//	薄雾玫瑰
#define	COLOR_SALMON						0xFA8072	//	鲜肉(鲑鱼)色
#define	COLOR_SNOW							0xFFFAFA	//	雪
#define	COLOR_LIGHTCORAL					0xF08080	//	淡珊瑚色
#define	COLOR_ROSYBROWN						0xBC8F8F	//	玫瑰棕色
#define	COLOR_INDIANRED						0xCD5C5C	//	印度红
#define	COLOR_RED							0xFF0000	//	纯红
#define	COLOR_BROWN							0xA52A2A	//	棕色
#define	COLOR_FIREBRICK						0xB22222	//	耐火砖
#define	COLOR_DARKRED						0x8B0000	//	深红色
#define	COLOR_MAROON						0x800000	//	栗色
#define	COLOR_WHITE							0xFFFFFF	//	纯白
#define	COLOR_WHITESMOKE					0xF5F5F5	//	白烟
#define	COLOR_GAINSBORO						0xDCDCDC	//	亮灰色
#define	COLOR_LIGHTGREY						0xD3D3D3	//	浅灰色
#define	COLOR_SILVER						0xC0C0C0	//	银白色
#define	COLOR_DARKGRAY						0xA9A9A9	//	深灰色
#define	COLOR_GRAY							0x808080	//	灰色
#define	COLOR_DIMGRAY						0x696969	//	暗淡的灰色
#define	COLOR_BLACK							0x000000	//	纯黑

//声明功能函数
void Ws2812b_Configuration(void);
void Ws2812b_Write(u32 color);


void RGB_Refresh(u32 color,u8 num);
void RGB_Cycle(u32 color,u8 num,u16 speed);
void RGB_Wave(u32 color,u8 num,u16 speed);
void RGB_Flash(u32 color,u8 num,u16 speed,u8 cnt);

#endif

两者代码大同小异,关键是高低电平时间,这个是我用示波器慢慢测试出来的,亲测能用,最高支持整个灯板(88颗灯珠),在调试和测试结果上看STM8优于N76E003。

完整代码可以加QQ群下载:733945348

By Urien 2019年8月11日 14:25:04

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章