/****************************************************************************************
* WS2811 ²ÊµÆÇý¶¯º¯Êý
*
* µ÷Ó÷½·¨£º
* Ð޸ĺ궨Ò壺 #define nWs 1 // ÓжàÉÙ¿ÅWS2811¼¶Áª
* WS_Init(); // IO³õʼ»¯
* WsDat[0] = 0x808080;//ÏԴ渳ֵ
* WS_SetAll(); // ·¢ËÍÊý¾Ý
* ColorToColor(unsigned long color0, unsigned long color1);// ÑÕÉ«½¥±äËã·¨
*
* ×÷ÕߣºÐÇÏ£Íû£¨ÒÑУÑ飩
* ÈÕÆÚ£º2015Äê6ÔÂ24ÈÕ
****************************************************************************************/
#include "sys.h"
#include <stm32f10x.h>
#include "WS2811.h"
#include "delay.h"
/* ÏÔ´æ */
unsigned long WsDat[nWs];
const unsigned long RGB_data[32]={
0x00ff00,0x00ff00,0x00ff00,
0xFF7F00,0xFF7F00,0xFF7F00,
0xFFFF00,0xFFFF00,0xFFFF00,
0x0000EE,0x0000EE,0x0000EE,
0x00FFFF,0x00FFFF,0x00FFFF,0x00FFFF
// 0x00ff00,0x00ff00,0x00ff00,0x00ff00,
// 0xFF7F00,0xFF7F00,0xFF7F00,
// 0xFFFF00,0xFFFF00,0xFFFF00,
// 0x0000EE,0x0000EE,0x0000EE,
// 0x00FFFF,0x00FFFF,0x00FFFF
// 0x00FFFF,
// 0x00ff00,0x00ff00,0x00ff00,0x00ff00,
// 0xFF7F00,0xFF7F00,0xFF7F00,
// 0xFFFF00,0xFFFF00,0xFFFF00,
// 0x0000EE,0x0000EE,0x0000EE,
// 0x00FFFF,0x00FFFF
};
/**************************************************************************************
* IO³õʼ»¯£¨ÒÆֲʱÇëÐ޸ģ©
**************************************************************************************/
void WS_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
//¶Ë¿ÚʱÖÓ£¬Ê¹ÄÜ
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE, ENABLE );
// ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; // PIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOE, &GPIO_InitStructure); // ¸ù¾ÝÉ趨²ÎÊý³õʼ»¯
}
/**************************
* ÄÚ²¿ÑÓʱ
***************************/
void delay2us()
{
unsigned char i;
for(i=0; i<12; i++);
}
void delay05us()
{
unsigned char i;
for(i=0; i<1; i++);
}
/***************************
* ·¢ËÍÒ»±ÈÌØ
****************************/
void TX0() { PEout(1) = 1; delay05us(); PEout(1) = 0; delay2us(); } // ·¢ËÍ0
void TX1() { PEout(1) = 1; delay2us(); PEout(1) = 0; delay05us(); } // ·¢ËÍ1
void WS_Reset() { PEout(1) = 0; delay_us(60); PEout(1) = 1; PEout(1) = 0; }
/**************************************************************************************
* ·¢ËÍÒ»×Ö½Ú
**************************************************************************************/
void WS_Set1(unsigned long dat)
{
unsigned char i;
for(i=0; i<24; i++)
{
if(0x800000 == (dat & 0x800000) ) TX1();
else TX0();
dat<<=1; //×óÒÆһλ
}
}
/**************************************************************************************
* ·¢ËÍËùÓÐ×Ö½Ú
**************************************************************************************/
void WS_SetAll()
{
unsigned char j;
for(j=0; j<nWs; j++)
{
WS_Set1(WsDat[0]); // j / 0
}
WS_Reset();
}
/********************************************
* Çó¾ø¶ÔÖµ
********************************************/
unsigned char abs0(int num)
{
if(num>0) return num;
num = -num;
return (unsigned char) num;
}
/***********************************************************************************
* ÑÕÉ«½¥±äËã·¨
* Îó²î <= 2
************************************************************************************/
u32 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 = Red0 + (int)(RedStep * i);
Green1 = Green0 + (int)(GreenStep * i);
Blue1 = Blue0 + (int)(BlueStep * i);
color = Green1<<16 | Red1<<8 | Blue1; // ºÏ³É Â̺ìÀ¶
WsDat[0] = color;
WS_SetAll(); // Êä³ö
delay_ms(5); // ½¥±äËÙ¶È
}
// ½¥±ä½áÊø
return color;
}
u32 ColorToColor_r(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;
unsigned char j,t;
unsigned char xunhuan=15;
// ÂÌ ºì À¶ ÈýÔÉ«·Ö½â
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 = Red0 + (int)(RedStep * i);
Green1 = Green0 + (int)(GreenStep * i);
Blue1 = Blue0 + (int)(BlueStep * i);
color = Green1<<16 | Red1<<8 | Blue1; // ºÏ³É Â̺ìÀ¶
WsDat[0] = color;
WS_SetAll(); // Êä³ö
for(i=0; i<nWs; i++)
{
for(j=0; j<nWs-xunhuan; j++)
{
WS_Set1(WsDat[0]);
}
WS_Reset();
xunhuan--;
}
delay_ms(200); // ½¥±äËÙ¶È
}
// ½¥±ä½áÊø
return color;
}
|