| 
 
| 1. 说明 WS2812网上的芯片手册乱七八糟,时间参数都不一样,此程序仅针对我手上的灯珠。
 MCU是新唐M031,时钟48M,使用软件延时
 2. 代码
 
 
 #define DEF_HW_RGB_MODULE
 /*********************************************************************************
 *                                   INCLUDES
 *********************************************************************************/
 #include "hw_rgb.h"
 #include "NuMicro.h"
 /*********************************************************************************
 *                                   DEFINES
 *********************************************************************************/
 #define RGB_PORT    PA
 #define RGB_BIT     BIT5
 #define RGB_IO      PA5
 /*********************************************************************************
 *                                   MACROS
 *********************************************************************************/
 
 /*********************************************************************************
 *                                  TYPEDEFS
 *********************************************************************************/
 
 /*********************************************************************************
 *                               STATIC FUNCTION
 *********************************************************************************/
 static void out_delay(void)
 {
 __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
 }
 
 static void out_0(void)
 {
 RGB_IO = 1;
 out_delay();
 RGB_IO = 0;
 out_delay();
 out_delay();
 
 }
 
 static void out_1(void)
 {
 RGB_IO = 1;
 out_delay();
 out_delay();
 RGB_IO = 0;
 out_delay();
 
 }
 
 static void out_rst(void)
 {
 RGB_IO = 0;
 DelaySoft_us(100);
 }
 
 /*********************************************************************************
 *                               GLOBAL FUNCTION
 *********************************************************************************/
 /* RGB Init */
 void hw_RGBInit(void)
 {
 GPIO_SetMode(RGB_PORT, RGB_BIT, GPIO_MODE_OUTPUT);
 
 out_rst();
 }
 
 /* rgb: 24位RGB */
 void hw_RGBSet(uint32_t rgb)
 {
 uint8_t r = (rgb >> 8) & 0xff;
 uint8_t g = (rgb >> 16) & 0xff;
 uint8_t b = rgb & 0xff;
 uint32_t tmp = (g<<16) | (r<<8) | b;
 uint8_t i = 0;
 
 for(i=0; i<24; i++)
 {
 if((tmp>>(23-i))&0x01)  out_1();
 else                    out_0();
 }
 }
 
 /* 设置所有等待颜色 */
 void hw_RGBSetAll(uint32_t rgb)
 {
 uint8_t i = 0;
 
 for(i=0; i<8; i++)
 {
 hw_RGBSet(rgb);
 }
 }
 
 
 
 
 
 
 ————————————————
 
 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
 
 原文链接:https://blog.csdn.net/yangshuoSB/article/details/144719655
 
 
 | 
 |