stm32 驱动ws2812

2020-01-07 19:22发布

最近正在用stm32做一个台灯,使用到了ws2812 LED,到网上找ws2812的stm32驱动,找半天找到一个,结果还是用不了。所以决定还是自己动手来吧。
下面的驱动代码贴出来,时候大家要使用也不错的
ws2812.h

  1. #ifndef __WS2812_H
  2. #define __WS2812_H

  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif

  7. #include "stm32f10x_conf.h"

  8. // The following macro must be ported to  your platform
  9. // 以下的宏定义请移植到你的IO Pin
  10. #define   ws2812DIN_HIGH()     (GPIOA->BSRR = 0x00001000)
  11. #define   ws2812DIN_LOW()      (GPIOA->BSRR = 0x10000000)
  12. #define   ws2812DIN_INIT()     {
  13.                                                                 RCC->APB2ENR |= 0x01<<2;
  14.                                                                 GPIOA->CRH &= ~(0x000f0000);
  15.                                                                 GPIOA->CRH |= (0x00030000);
  16.                                                                 GPIOA->BSRR |= (0x10000000);
  17.                                                                 }

  18. // RGB structure
  19. typedef struct
  20. {
  21.         // R,G,B color value
  22.     uint8_t ucRedVal;
  23.     uint8_t ucGreenVal;
  24.     uint8_t ucBlueVal;
  25. }RGB_t;
  26. // when you want to use ws2812 , you must call this function
  27. void ws2812_Init(void);
  28. void ws2812_SendRes(void);
  29. void ws2812_SendRGBData(RGB_t xRGB);


  30. /*** typical usage for above API, 典型的使用方法
  31.  ws2812_Init();
  32.     GRB_t xRGB;
  33.     xRGB.ucRedVal = 0xff;
  34.     xRGB.ucGreenVal = 0x00;
  35.     xRGB.ucBlueVal = 0x00;
  36.     while(1)
  37.     {
  38.             // The first ws2812
  39.             ws2812_SendRGBData(xRGB);
  40.             // The second ws2812
  41.             ws2812_SendRGBData(xRGB);
  42.             // The third ws2812
  43.             ws2812_SendRGBData(xRGB);
  44.             //  The fourth ws2812
  45.             ws2812_SendRGBData(xRGB);
  46.             // send frame seperator
  47.             ws2812_SendRes();


  48.     }

  49. */

  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif
复制代码

ws2812.c 文件


  1. #include "ws2812.h"

  2. /*
  3. * @Desc: delay a approximate us
  4. * @Args: ulUs, us about to delay
  5. * @Returns: None
  6. */
  7. static void
  8. DelayUs(uint32_t ulUs)
  9. {
  10.         uint32_t j;
  11.         while(ulUs--)
  12.         {
  13.                 j = 12;
  14.                 while(j)
  15.                 {
  16.                         j--;
  17.                 }
  18.         }
  19. }


  20. /*
  21. * @Desc: delay a number of nop for your platform(one nop equal to 13.9 ns)
  22. * @Args: ulNopNum, nop number
  23. * @Returns: None
  24. *
  25. */
  26. static void
  27. DelayNop(uint32_t ulNopNum)
  28. {
  29.         while(ulNopNum)
  30.         {
  31.                 __NOP();
  32.                 ulNopNum--;
  33.         }
  34. }


  35. /*
  36. * @Desc: init ws2812, just init io Pin,
  37. *   you must call this function firstly whenever
  38. *   you want to use ws28128
  39. * @Args: None
  40. * @Returns: None
  41. *
  42. */
  43. void
  44. ws2812_Init(void)
  45. {
  46.         ws2812DIN_INIT();
  47. }


  48. /*
  49. * @Desc: send rgb value to a ws2812,
  50. * @Args: xRGB, rgb value container variable
  51. * @Returns: NOne
  52. *
  53. */
  54. void
  55. ws2812_SendRGBData(RGB_t xRGB)
  56. {
  57.         uint32_t i = 0;
  58.         uint32_t ulColor = 0;
  59.         // put blue color  bit to ulColor
  60.         for(i = 0; i < 8; i++)
  61.         {
  62.                 ulColor = ((xRGB.ucBlueVal & 0x01) | (ulColor << 1));
  63.                 xRGB.ucBlueVal >>= 1;
  64.         }
  65.         // put red color  bit to ulColor
  66.         for(i = 8; i < 16; i++)
  67.         {
  68.                 ulColor = ((xRGB.ucRedVal & 0x01) | (ulColor << 1));
  69.                 xRGB.ucRedVal >>= 1;
  70.         }
  71.         // put green color bit to ulColor
  72.         for(i = 16; i < 24; i++)
  73.         {
  74.                 ulColor = ((xRGB.ucGreenVal & 0x01) | (ulColor << 1));
  75.                 xRGB.ucGreenVal >>= 1;
  76.         }
  77.         // send 24 bits color data
  78.         for(i = 0; i < 24; i++)
  79.         {
  80.                 if((ulColor & (uint32_t)0x01))
  81.                 {
  82.                         ws2812DIN_HIGH();
  83.                         DelayNop(20);
  84.                         ws2812DIN_LOW();
  85.                 }
  86.                 else
  87.                 {
  88.                         ws2812DIN_HIGH();
  89.                         ws2812DIN_LOW();
  90.                         DelayNop(20);
  91.                 }
  92.                 ulColor >>= 1;
  93.         }
  94. }

  95. /*
  96. * @Desc: send a frame seperator
  97. * @Args: None
  98. * @Returns: None
  99. */
  100. void
  101. ws2812_SendRes(void)
  102. {
  103.     ws2812DIN_LOW();
  104.     DelayUs(100);
  105. }

复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
37条回答
helloforworld
2020-01-07 23:11
仔细想了一下把,又把驱动重新的写了一下,(主要把数据结构改了,把ws812_SendRGBData() 换成了ws2812_SendColorData() ),具体地如下:
ws2812.h文件

  1. /**
  2. * Copyright ©2017 Thogo Tech. All Rights Reserved.
  3. *
  4. *@description: This file contains data structure and API
  5.      definition for RGB LED ws2812.it's sensible for time
  6. *@author: infinite.ft
  7. *@create_at: 2017/03/09
  8. *@update_at: 2017/03/12
  9. *@version: 0.0.1
  10. *
  11. */
  12. #ifndef __WS2812_H
  13. #define __WS2812_H

  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif

  18. #include "stm32f10x_conf.h"

  19. // The following macro must be ported to  your platform
  20. #define   ws2812DIN_HIGH()     (GPIOA->BSRR = 0x00001000)
  21. #define   ws2812DIN_LOW()      (GPIOA->BSRR = 0x10000000)
  22. #define   ws2812DIN_INIT()     {
  23.                                                                 RCC->APB2ENR |= 0x01<<2;
  24.                                                                 GPIOA->CRH &= ~(0x000f0000);
  25.                                                                 GPIOA->CRH |= (0x00030000);
  26.                                                                 GPIOA->BSRR |= (0x10000000);
  27.                                                                 }
  28. // RGB color type structure
  29. typedef union
  30. {
  31.         struct {
  32.                 uint8_t ucPadding;
  33.                 uint8_t ucBlue;
  34.                 uint8_t ucRed;
  35.                 uint8_t ucGreen;
  36.         }xRGB;
  37.         uint32_t ulBits;
  38. }Color_t;

  39. // when you want to use ws2812 , you must call this function
  40. void ws2812_Init(void);
  41. void ws2812_SendRes(void);
  42. void ws2812_SendColorData(Color_t xColor);

  43. /*** typical usage for above API
  44.     Color_t xColor;
  45.     xColor.xRGB.ucRed = 0xff;
  46.     xColor.xRGB.ucGreen = 0x00;
  47.     xColor.xRGB.ucBlue = 0x00;
  48.     while(1)
  49.     {
  50.             // first ws2812
  51.             ws2812_SendColorData(xColor);
  52.             // second ws2812
  53.             ws2812_SendColorData(xColor);
  54.             // third ws2812
  55.             ws2812_SendColorData(xColor);
  56.             // fourth ws2812
  57.             ws2812_SendColorData(xColor);
  58.             // send frame seperator
  59.             ws2812_SendRes();


  60.     }

  61. */

  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif

复制代码

ws2812.c 文件

  1. /**
  2. *
  3. * Copyright ©2017 Thogo Tech. All Rights Reserved.
  4. *
  5. * @description: this file contains functions that responsible
  6.      for performing operation for RGB LED ws2812
  7. * @author: infinite.ft
  8. * @create_at: 2017/03/09
  9. * @update_at: 2017/03/12
  10. * @version: 0.0.1
  11. *
  12. */

  13. #include "ws2812.h"

  14. /*
  15. * @Desc: delay a approximate us
  16. * @Args: ulUs, us about to delay
  17. * @Returns: None
  18. */
  19. static void
  20. DelayUs(uint32_t ulUs)
  21. {
  22.         uint32_t j;
  23.         while(ulUs--)
  24.         {
  25.                 j = 12;
  26.                 while(j)
  27.                 {
  28.                         j--;
  29.                 }
  30.         }
  31. }


  32. /*
  33. * @Desc: delay a number of nop for your platform(one nop equal to 13.9 ns)
  34. * @Args: ulNopNum, nop number
  35. * @Returns: None
  36. *
  37. */
  38. static void
  39. DelayNop(uint32_t ulNopNum)
  40. {
  41.         while(ulNopNum)
  42.         {
  43.                 __NOP();
  44.                 ulNopNum--;
  45.         }
  46. }


  47. /*
  48. * @Desc: init ws2812, just init io Pin,
  49. *   you must call this function firstly whenever
  50. *   you want to use ws28128
  51. * @Args: None
  52. * @Returns: None
  53. *
  54. */
  55. void
  56. ws2812_Init(void)
  57. {
  58.         ws2812DIN_INIT();
  59. }


  60. /*
  61. * @Desc: send RGB color value to a ws2812,
  62. * @Args: xColor, rgb color value  variable
  63. * @Returns: NOne
  64. *
  65. */
  66. void ws2812_SendColorData(Color_t xColor)
  67. {
  68.         uint32_t i = 0;
  69.         // send 24 bits color data
  70.         for(i = 0; i < 24; i++)
  71.         {
  72.                 if((xColor.ulBits & (uint32_t)(0x01 << (31-i))))
  73.                 {
  74.                         ws2812DIN_HIGH();
  75.                         DelayNop(20);
  76.                         ws2812DIN_LOW();
  77.                 }
  78.                 else
  79.                 {
  80.                         ws2812DIN_HIGH();
  81.                         ws2812DIN_LOW();
  82.                         DelayNop(20);
  83.                 }
  84.         }
  85. }
  86. /*
  87. * @Desc: send a frame seperator
  88. * @Args: None
  89. * @Returns: None
  90. */
  91. void
  92. ws2812_SendRes(void)
  93. {
  94.     ws2812DIN_LOW();
  95.     DelayUs(100);
  96. }


复制代码

一周热门 更多>