首页 > 编程知识 正文

ws2812b灯效果图,ws2812驱动芯片

时间:2023-05-03 16:54:33 阅读:181499 作者:3771

今天心血来潮,花了点时间写了WS2812B的驱动,打开了灯。

WS2812B为全色LED控制IC,为单总线控制,信号时序如下图所示。 其他信息请参阅规格书。 注意高低水平的时间必须在规格书的要求范围内。 WS2812B可以级联连接,各灯接收24位数据(GRB色值),D1灯在接收到24位数据后,保存数据,在接收到数据时通过DO脚传输给D2

STM32F0芯片,时钟48M,驱动器代码如下。

头文件:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ltd.@ filename : ws 2812 b.h @ author :糊读虫QQ :570525287 @ version :2020-12-25 @ description 3:0 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 修改define ws 2812 b _ portgpioa # define ws 2812 b _ pingping dfi news 2812 b _ RCC _ ahbrcc _ ahbperiph _ gpioa//RCC时钟# define ws 2812 b _ hi (ws 2812 b _ port-bsrr=ws 2812 b _ pipi le D1 _ pin ) # define ws 2812 b _ low (ws 2812 b _ port-brr=ws 2812 b ) u8 B; }Color_TypeDef; //--------definepixel_num59//led灯的个数voidws2812b_init(void ); voidws2812b_reset(void; void ws 2812 b _ write color (color _ typedef * p color ); voidws2812b_Refreshpixel(void; voidws2812b_fillcolor(U16start,u16 end,Color_TypeDef *pColor ); voidws2812b_movepixel(U8dir; voidws2812b_test(void; #endif /* __WS2812B_H */源文件

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ltd.@ filename : ws 2812 b.c @ author :糊读虫QQ :570525287 @ version :2020-12-25 @ description :57.0 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //晶振48M,每个nop 20.83 ns # define delay _ 20 _8ns _ nop () /一个nop为20.8 ns # define delay _ 104 ns delay _ 20 _8ns; DELAY_20_8nS; DELAY_20_8nS; DELAY_20_8nS; DELAY_20_8nS //5个nop是104ns#define Delay_320nS () DELAY_104nS; DELAY_104nS; DELAY_104nS //30个nop/*---------- -请参阅@Others :T1H T0L的时间为580ns----1.6us,取850ns

------------------------------------*/void Delay_850nS(void){//进入函数所用的时间约为310nsDELAY_104nS;DELAY_104nS;Delay_320nS();DELAY_20_8nS;}/*---------------------------------------------------------------------------@Function :Delay_300uS@Description:@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void Delay_300uS(void){uint8_t i;while(i--){Delay_320nS();}}/*---------------------------------------------------------------------------@Function :WS2812B_Init@Description:初始化@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_Init(void){GPIO_InitTypeDef GPIO_InitStruct;RCC_AHBPeriphClockCmd(WS2812B_RCC_AHB, ENABLE);GPIO_InitStruct.GPIO_Pin = WS2812B_PIN ;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_3;GPIO_Init(WS2812B_PORT, &GPIO_InitStruct);GPIO_SetBits(WS2812B_PORT, WS2812B_PIN);}/*---------------------------------------------------------------------------@Function :WS2812B_Reset@Description:复位@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_Reset(void) //复位函数{WS2812B_Low();Delay_300uS();}/*---------------------------------------------------------------------------@Function :WS2812B_WriteByte@Description:写一个字节@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_WriteByte(uint8_t dat){u8 i;for (i=0;i<8;i++){//先发送高位if (dat & 0x80) //1{WS2812B_Hi();Delay_850nS(); //T1HWS2812B_Low();Delay_320nS(); //T1L}else//0{WS2812B_Hi();Delay_320nS(); //T0HWS2812B_Low();Delay_850nS(); //T0L}dat<<=1;}}/*---------------------------------------------------------------------------@Function :WS2812B_WriteColor@Description:写入1个24bit颜色数据@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_WriteColor(Color_TypeDef *pColor){WS2812B_WriteByte(pColor->G);WS2812B_WriteByte(pColor->R);WS2812B_WriteByte(pColor->B);}/*---------------------------------------------------------------------------@Function :WS2812B_RefreshPixel@Description:更新显示@Input :无@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_RefreshPixel(void){u8 i;for(i=0;i<PIXEL_NUM;i++){WS2812B_WriteColor(&PixelBuf[i]);}}//----------------------------------------void WS2812B_Test(void){u8 i;Color_TypeDef temp;temp.B = 0x50;temp.R = 0x60;temp.G = 0x70;for(i=0;i<60;i++){WS2812B_WriteColor(&temp);}}//测试延时时间void WS2812B_Test2(void){WS2812B_Hi();Delay_850nS();WS2812B_Low();}//============================================================================void Copy_Color(Color_TypeDef *pDst,Color_TypeDef *pScr){pDst->R = pScr->R;pDst->G = pScr->G;pDst->B = pScr->B;}/*---------------------------------------------------------------------------@Function :WS2812B_FillColor@Description:填充颜色@Input :start:开始位置;end:结束信置;pColor:颜色值@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_FillColor(u16 start,u16 end,Color_TypeDef *pColor){if (start > end) //交换位置{u16 temp;temp = start;start = end;end = temp;}if (start >= PIXEL_NUM)return; //超出范围if (end >= PIXEL_NUM)end = PIXEL_NUM-1;//填充颜色值while(start <= end){Copy_Color(&PixelBuf[start],pColor);start++;}}/*---------------------------------------------------------------------------@Function :WS2812B_MovePixel@Description:循环移动像素颜色@Input :dir:方向;@Retrun :无@Others :----------------------------------------------------------------------------*/void WS2812B_MovePixel(u8 dir){Color_TypeDef temp;u8 i;if (dir) //向左移动{Copy_Color(&temp,&PixelBuf[PIXEL_NUM-1]);i = PIXEL_NUM-1;while(i){ Copy_Color(&PixelBuf[i],&PixelBuf[i-1]); i--;}Copy_Color(&PixelBuf[0],&temp);}else //向右移动{Copy_Color(&temp,&PixelBuf[0]);i = 0;while(i < (PIXEL_NUM-1)){Copy_Color(&PixelBuf[i],&PixelBuf[i+1]);i++;}Copy_Color(&PixelBuf[PIXEL_NUM-1],&temp);}}//----------------------------------END OF FILE------------------------------ /*---------------------------------------------------------------------------@Function :main@Description:主函数@Input :@Retrun :无@Others :----------------------------------------------------------------------------*/int main(void){Color_TypeDef temp;HardWare_Init();WS2812B_Init();temp.R = 0x00;temp.G = 0xff;temp.B = 0x80;WS2812B_FillColor(10,20,&temp);while(1){if (SysGetSignal_ms(20,0)) //20ms{WS2812B_MovePixel(1);WS2812B_RefreshPixel();}}}

代码实现了循环移动的流水灯功能,点亮效果:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。