最近正在用stm32做一个台灯,使用到了ws2812 LED,到网上找ws2812的stm32驱动,找半天找到一个,结果还是用不了。所以决定还是自己动手来吧。
下面的驱动代码贴出来,时候大家要使用也不错的
ws2812.h
- #ifndef __WS2812_H
- #define __WS2812_H
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #include "stm32f10x_conf.h"
- // The following macro must be ported to your platform
- // 以下的宏定义请移植到你的IO Pin
- #define ws2812DIN_HIGH() (GPIOA->BSRR = 0x00001000)
- #define ws2812DIN_LOW() (GPIOA->BSRR = 0x10000000)
- #define ws2812DIN_INIT() {
- RCC->APB2ENR |= 0x01<<2;
- GPIOA->CRH &= ~(0x000f0000);
- GPIOA->CRH |= (0x00030000);
- GPIOA->BSRR |= (0x10000000);
- }
- // RGB structure
- typedef struct
- {
- // R,G,B color value
- uint8_t ucRedVal;
- uint8_t ucGreenVal;
- uint8_t ucBlueVal;
- }RGB_t;
- // when you want to use ws2812 , you must call this function
- void ws2812_Init(void);
- void ws2812_SendRes(void);
- void ws2812_SendRGBData(RGB_t xRGB);
- /*** typical usage for above API, 典型的使用方法
- ws2812_Init();
- GRB_t xRGB;
- xRGB.ucRedVal = 0xff;
- xRGB.ucGreenVal = 0x00;
- xRGB.ucBlueVal = 0x00;
- while(1)
- {
- // The first ws2812
- ws2812_SendRGBData(xRGB);
- // The second ws2812
- ws2812_SendRGBData(xRGB);
- // The third ws2812
- ws2812_SendRGBData(xRGB);
- // The fourth ws2812
- ws2812_SendRGBData(xRGB);
- // send frame seperator
- ws2812_SendRes();
- }
- */
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码
ws2812.c 文件
- #include "ws2812.h"
- /*
- * @Desc: delay a approximate us
- * @Args: ulUs, us about to delay
- * @Returns: None
- */
- static void
- DelayUs(uint32_t ulUs)
- {
- uint32_t j;
- while(ulUs--)
- {
- j = 12;
- while(j)
- {
- j--;
- }
- }
- }
- /*
- * @Desc: delay a number of nop for your platform(one nop equal to 13.9 ns)
- * @Args: ulNopNum, nop number
- * @Returns: None
- *
- */
- static void
- DelayNop(uint32_t ulNopNum)
- {
- while(ulNopNum)
- {
- __NOP();
- ulNopNum--;
- }
- }
- /*
- * @Desc: init ws2812, just init io Pin,
- * you must call this function firstly whenever
- * you want to use ws28128
- * @Args: None
- * @Returns: None
- *
- */
- void
- ws2812_Init(void)
- {
- ws2812DIN_INIT();
- }
- /*
- * @Desc: send rgb value to a ws2812,
- * @Args: xRGB, rgb value container variable
- * @Returns: NOne
- *
- */
- void
- ws2812_SendRGBData(RGB_t xRGB)
- {
- uint32_t i = 0;
- uint32_t ulColor = 0;
- // put blue color bit to ulColor
- for(i = 0; i < 8; i++)
- {
- ulColor = ((xRGB.ucBlueVal & 0x01) | (ulColor << 1));
- xRGB.ucBlueVal >>= 1;
- }
- // put red color bit to ulColor
- for(i = 8; i < 16; i++)
- {
- ulColor = ((xRGB.ucRedVal & 0x01) | (ulColor << 1));
- xRGB.ucRedVal >>= 1;
- }
- // put green color bit to ulColor
- for(i = 16; i < 24; i++)
- {
- ulColor = ((xRGB.ucGreenVal & 0x01) | (ulColor << 1));
- xRGB.ucGreenVal >>= 1;
- }
- // send 24 bits color data
- for(i = 0; i < 24; i++)
- {
- if((ulColor & (uint32_t)0x01))
- {
- ws2812DIN_HIGH();
- DelayNop(20);
- ws2812DIN_LOW();
- }
- else
- {
- ws2812DIN_HIGH();
- ws2812DIN_LOW();
- DelayNop(20);
- }
- ulColor >>= 1;
- }
- }
- /*
- * @Desc: send a frame seperator
- * @Args: None
- * @Returns: None
- */
- void
- ws2812_SendRes(void)
- {
- ws2812DIN_LOW();
- DelayUs(100);
- }
复制代码
看了下WS2812的驱动。。。0和1是占空比不一样,
高 低
0 是 0.35 0.8 us
1 是 0.7 0.6 us
这种用SPI怎么实现?SPI的波特率是一定的啊,除非 SPI 设定的波特率位时间可以到0.1us才有可能实现
一周热门 更多>