WS2812驱动

ws2812驱动程序

image

/**
 * @file ws_Agreement.c
 * @brief  使用范围WS2812 LED协议
 * @author HadyLiu email:HadyLiu@outlook.com
 * @version 0.0.1
 * @date 2026-01-21
 *
 * @copyright Apache 2.0
 */

#include "WS_Agreement.h"

#define WS_LogicDetectOutput(sourceByte, Detection) \
    do                                              \
    {                                               \
        if ((sourceByte & Detection) > 0)           \
        {                                           \
            WS_Logic_HIGH();                        \
        }                                           \
        else                                        \
        {                                           \
            WS_Logic_LOW();                         \
        }                                           \
    } while (0)

// 发送WS2812E LED数据
void WS_WriteXbyte(unsigned char data[], unsigned char len)
{
    unsigned char Offset;
    unsigned char bite;
    WS_Logic_LOW();
    WS_Delay_us();
    G_IRQ_DISABLE();
    for (Offset = 0; Offset < len; Offset++)
    {
        bite = data[Offset];
        WS_LogicDetectOutput(bite, 0x80);
        WS_LogicDetectOutput(bite, 0x40);
        WS_LogicDetectOutput(bite, 0x20);
        WS_LogicDetectOutput(bite, 0x10);
        WS_LogicDetectOutput(bite, 0x08);
        WS_LogicDetectOutput(bite, 0x04);
        WS_LogicDetectOutput(bite, 0x02);
        WS_LogicDetectOutput(bite, 0x01);
    }
    G_IRQ_ENABLE();
}


#ifndef WS_AGREEMENT_H
#define WS_AGREEMENT_H

#include "SYSCFG.H"
#include "sys_init.h"

#define WS_PinOut_HIGH() (RB6 = 1)
#define WS_PinOut_LOW() (RB6 = 0)

//在这里不要使用NOP();
//注:使用控制设置IO电平再去NOP()可能导致翻页问题导致延时不准,不要这么做
#define WS_Logic_HIGH()   \
    do                    \
    {                     \
        WS_PinOut_HIGH(); \
        WS_PinOut_HIGH(); \
        WS_PinOut_HIGH(); \
        WS_PinOut_HIGH(); \
        WS_PinOut_LOW();  \
        WS_PinOut_LOW();  \
    } while (0)

#define WS_Logic_LOW()    \
    do                    \
    {                     \
        WS_PinOut_HIGH(); \
        WS_PinOut_HIGH(); \
        WS_PinOut_LOW();  \
        WS_PinOut_LOW();  \
        WS_PinOut_LOW();  \
        WS_PinOut_LOW();  \
    } while (0)

#define WS_Delay_us() __delay_us(100)
#define G_IRQ_DISABLE() (GIE = 0)
#define G_IRQ_ENABLE() (GIE = 1) //(__enable_irq())

extern void WS_WriteXbyte(unsigned char data[], unsigned char len);

#endif

posted @ 2026-02-04 22:47  HadyLiu  阅读(0)  评论(0)    收藏  举报