最近正在用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.h文件
- /**
- * Copyright ©2017 Thogo Tech. All Rights Reserved.
- *
- *@description: This file contains data structure and API
- definition for RGB LED ws2812.it's sensible for time
- *@author: infinite.ft
- *@create_at: 2017/03/09
- *@update_at: 2017/03/12
- *@version: 0.0.1
- *
- */
- #ifndef __WS2812_H
- #define __WS2812_H
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #include "stm32f10x_conf.h"
- // The following macro must be ported to your platform
- #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 color type structure
- typedef union
- {
- struct {
- uint8_t ucPadding;
- uint8_t ucBlue;
- uint8_t ucRed;
- uint8_t ucGreen;
- }xRGB;
- uint32_t ulBits;
- }Color_t;
- // when you want to use ws2812 , you must call this function
- void ws2812_Init(void);
- void ws2812_SendRes(void);
- void ws2812_SendColorData(Color_t xColor);
- /*** typical usage for above API
- Color_t xColor;
- xColor.xRGB.ucRed = 0xff;
- xColor.xRGB.ucGreen = 0x00;
- xColor.xRGB.ucBlue = 0x00;
- while(1)
- {
- // first ws2812
- ws2812_SendColorData(xColor);
- // second ws2812
- ws2812_SendColorData(xColor);
- // third ws2812
- ws2812_SendColorData(xColor);
- // fourth ws2812
- ws2812_SendColorData(xColor);
- // send frame seperator
- ws2812_SendRes();
- }
- */
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码ws2812.c 文件
- /**
- *
- * Copyright ©2017 Thogo Tech. All Rights Reserved.
- *
- * @description: this file contains functions that responsible
- for performing operation for RGB LED ws2812
- * @author: infinite.ft
- * @create_at: 2017/03/09
- * @update_at: 2017/03/12
- * @version: 0.0.1
- *
- */
- #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 color value to a ws2812,
- * @Args: xColor, rgb color value variable
- * @Returns: NOne
- *
- */
- void ws2812_SendColorData(Color_t xColor)
- {
- uint32_t i = 0;
- // send 24 bits color data
- for(i = 0; i < 24; i++)
- {
- if((xColor.ulBits & (uint32_t)(0x01 << (31-i))))
- {
- ws2812DIN_HIGH();
- DelayNop(20);
- ws2812DIN_LOW();
- }
- else
- {
- ws2812DIN_HIGH();
- ws2812DIN_LOW();
- DelayNop(20);
- }
- }
- }
- /*
- * @Desc: send a frame seperator
- * @Args: None
- * @Returns: None
- */
- void
- ws2812_SendRes(void)
- {
- ws2812DIN_LOW();
- DelayUs(100);
- }
复制代码一周热门 更多>