RGB_LED,WS2811綵帶驅動設計

 

 

 

WS2811.h

#ifndef __WS2811_H#define __WS2811_H         
#include "sys.h"
#define White       0xFFFFFF  // 白色
#define Black       0x000000  // 黑色
#define Red         0x00ff00  // 紅色
#define Green       0xff0000  // 綠色
#define Blue        0x0000ff  // 藍色
#define nWs 16                // 有多少顆WS2811級聯
extern unsigned long WsDat[];
extern void WS_Init(void);
extern void WS_SetAll(void);
extern u32 ColorToColor(unsigned long color0, unsigned long color1);                                                     #endif


WS2811.c

/****************************************************************************************

* WS2811 彩燈驅動函數

*

* 調用方法:

*        修改宏定義: #define nWs 1        // 有多少顆WS2811級聯

*        WS_Init();        // IO初始化

*        WsDat[0] = 0x808080;//顯存賦值

*        WS_SetAll();  // 發送數據

*        ColorToColor(unsigned long color0, unsigned long color1);// 顏色漸變算法

*

* 作者:

* 日期:

****************************************************************************************/

#include "sys.h"

#include <stm32f10x.h>

#include "WS2811.h"

#include "delay.h"

        

/* 顯存 */

unsigned long WsDat[nWs];

 

 

/**************************************************************************************

* IO初始化(移植時請修改)

**************************************************************************************/

void WS_Init()

{

        GPIO_InitTypeDef  GPIO_InitStructure;        

        

        //端口時鐘,使能

        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );         

 

        // 端口配置

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                // PIN

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                 // 推輓輸出

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                // IO口速度爲50MHz

        GPIO_Init(GPIOA, &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()          { PAout(0) = 1; delay05us(); PAout(0) = 0; delay2us(); } // 發送0

void TX1()          { PAout(0) = 1; delay2us();  PAout(0) = 0; delay05us(); } // 發送1

void WS_Reset() { PAout(0) = 0; delay_us(60);  PAout(0) = 1; PAout(0) = 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(1);                                                // 漸變速度

        }

        // 漸變結束

        

        return color;

}
 

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