jipin 发表于 2017-4-6 09:30:51

亲测WS2812 RGB彩灯MCU和SOC方案实现

本帖最后由 jipin 于 2017-4-6 09:37 编辑

      市场上RGB的LED灯很多。控制方式也很多,如PWM的。I2C的。现在介绍一款只需一个GPIO就能控制的内置WS2812的5050灯珠
市场价格在0.4元左右一个。
实现方式论坛里也有介绍。这里介绍亲测的两种方式
第一种MCU方案 基于STM32F103C8T6
第二种 SOC 方案 基于 ESP8266




WS2812B.c


#include"Hal_WS2812B/WS2812B.h"
#include "stm32f10x_dma.h"
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stdio.h"
/* Buffer that holds one complete DMA transmission
*
* Ensure that this buffer is big enough to hold
* all data bytes that need to be sent
*
* The buffer size can be calculated as follows:
* number of LEDs * 24 bytes + 42 bytes
*
* This leaves us with a maximum string length of
* (2^16 bytes per DMA stream - 42 bytes)/24 bytes per LED = 2728 LEDs
*/
#define TIM3_CCR3_Address 0x4000043c         // physical memory address of Timer 3 CCR1 register
//#define TIM3_CCR1_Address 0x40000434      // physical memory address of Timer 3 CCR1 register
      
#define TIMING_ONE50
#define TIMING_ZERO 25
uint16_t LED_BYTE_Buffer;
//---------------------------------------------------------------//

void Timer3_init(void)
{
      TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
      /* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOB, &GPIO_InitStructure);
      
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
      /* Compute the prescaler value */
      //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
      /* Time base configuration */
      TIM_TimeBaseStructure.TIM_Period = 90-1; // 800kHz
      TIM_TimeBaseStructure.TIM_Prescaler = 0;
      TIM_TimeBaseStructure.TIM_ClockDivision = 0;
      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
      TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

      /* PWM1 Mode configuration: Channel1 */
      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
      TIM_OCInitStructure.TIM_Pulse = 0;
      TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
      TIM_OC3Init(TIM3, &TIM_OCInitStructure);
               
      /* configure DMA */
      /* DMA clock enable */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
      
      /* DMA1 Channel6 Config */
      DMA_DeInit(DMA1_Channel3);

      DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM3_CCR3_Address;      // physical address of Timer 3 CCR1
      DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer;                // this is the buffer memory
      DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                                                // data shifted from memory to peripheral
      DMA_InitStructure.DMA_BufferSize = 42;
      DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
      DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                                        // automatically increase buffer index
      DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
      DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
      DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                                      // stop DMA feed after buffer size is reached
      DMA_InitStructure.DMA_Priority = DMA_Priority_High;
      DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
      
      DMA_Init(DMA1_Channel3, &DMA_InitStructure);

                /* TIM3 CC1 DMA Request enable */
      TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE);
}

/* This function sends data bytes out to a string of WS2812s
* The first argument is a pointer to the first RGB triplet to be sent
* The seconds argument is the number of LEDs in the chain
*
* This will result in the RGB triplet passed by argument 1 being sent to
* the LED that is the furthest away from the controller (the point where
* data is injected into the chain)
/ *此函数将数据字节发送到WS2812的字符串
*第一个参数是指向要发送的第一个RGB三元组的指针
* seconds参数是链中的LED数量
*:
*这将导致通过参数1传递的RGB三元组发送到
*离控制器最远的LED(
*数据被注入链中
*/
void WS2812_send(uint8_t (*color), uint16_t len)
{
      uint8_t i;
      uint16_t memaddr;
      uint16_t buffersize;
      buffersize = (len*24)+43;      // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
      memaddr = 0;                              // reset buffer memory index

      while (len)
      {      
                              for(i=0; i<8; i++) // GREEN data
                        {
                                        LED_BYTE_Buffer = ((color<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
                                        memaddr++;
                        }
                        for(i=0; i<8; i++) // RED
                        {
                                        LED_BYTE_Buffer = ((color<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
                                        memaddr++;
                        }
                        for(i=0; i<8; i++) // BLUE
                        {
                                        LED_BYTE_Buffer = ((color<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
                                        memaddr++;
                        }
                        
                  len--;
      }
//===================================================================//      
//bug:最后一个周期波形不知道为什么全是高电平,故增加一个波形
          LED_BYTE_Buffer = ((color<<8) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
//===================================================================//      
          memaddr++;      
                while(memaddr < buffersize)
                {
                        LED_BYTE_Buffer = 0;
                        memaddr++;
                }

                DMA_SetCurrDataCounter(DMA1_Channel3, buffersize);         // load number of bytes to be transferred
                DMA_Cmd(DMA1_Channel3, ENABLE);                         // enable DMA channel 6
                TIM_Cmd(TIM3, ENABLE);                                                 // enable Timer 3
                while(!DMA_GetFlagStatus(DMA1_FLAG_TC3)) ;         // wait until transfer complete
                TIM_Cmd(TIM3, DISABLE);         // disable Timer 3
                DMA_Cmd(DMA1_Channel3, DISABLE);                         // disable DMA channel 6
                DMA_ClearFlag(DMA1_FLAG_TC3);                                 // clear DMA1 Channel 6 transfer complete flag
}
WS2812B.H
#ifndef __WS2812B_H
#define      __WS2812B_H

#include "stm32f10x.h"
#include "delay.h"      

void Timer3_init(void);
void WS2812_send(uint8_t (*color), uint16_t len);

#endif /* __LED_H */


clide2000 发表于 2017-5-5 23:02:57

jipin 发表于 2017-5-5 08:34
试下修改颜色之前 设置一次黑色。

在user_main.c中的init事件中,调用的ws2812的init初始化函数。
但接电模块启动后,还总是总是亮个绿灯。

jipin 发表于 2017-4-6 09:34:32

续。。。
ws2812.c


#include "driver/ws2812.h"
#include "ets_sys.h"
#include "user_interface.h"
#include "osapi.h"

//#define GPIO_OUTPUT_SET(gpio_no, bit_value) gpio_output_set(bit_value<<gpio_no, ((~bit_value)&0x01)<<gpio_no, 1<<gpio_no,0)

void ICACHE_FLASH_ATTR SEND_WS_0()//关
{
       uint8_t time;
       time = 4; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 1 );
       time = 7; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 0 );
}

void ICACHE_FLASH_ATTR SEND_WS_1()//开
{
       uint8_t time;
       time = 10; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 1 );
       time = 2; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 0 );
}

void ICACHE_FLASH_ATTR ws2812_strip(uint8_t R,uint8_t G,uint8_t B, uint16_t length)
{
      uint16_t i=0;
      uint16_t j=0;
      uint8_t byte;
      uint8_t mask=0x80;
      uint8_t buff;
      uint16_t length2=(length*3);
       GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0);
       system_soft_wdt_stop();
       ets_intr_lock();

       while(length2--)
       {
            mask = 0x80;
            switch(i)
            {
                     case 0:byte=G; i++;
                     break;
                     case 1:byte=R; i++;
                     break;
                     case 2:byte=B; i=0;
                     break;
            }
            while (mask)
            {

                  ( byte & mask ) ? (buff=1) : (buff=0);
                     mask >>= 1;
            }
       }
            i=0;
            while (i<= j)
            {

                  ( buff & 0x01) ? SEND_WS_1() : SEND_WS_0();
            }
       //}
            // while (mask)
            // {

            //       ( byte & mask ) ? SEND_WS_1() : SEND_WS_0();
            //      mask >>= 1;

            // }
       ets_intr_unlock();
       system_soft_wdt_restart();
}

void ICACHE_FLASH_ATTR ws2812_init()
{
//PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U);
//PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO0_U);
//GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0);
       ws2812_strip(0,0,0, LEDS);
}









ws2812.h
#ifndef _WS2812_H
#define _WS2812_H

#include "c_types.h"
#include "user_interface.h"
#include "ets_sys.h"
#include "gpio.h"

#define WSGPIO 0 //只能是GPIO0,其它端口无用,不解
#define LEDS 21 //LED数量
void ICACHE_FLASH_ATTR ws2812_strip(uint8_t R,uint8_t G,uint8_t B, uint16_t length);
voidws2812_init();

#endif


soso123 发表于 2017-4-7 16:12:41

6666666666666666666

金金金金· 发表于 2017-4-7 16:21:19

jipin 发表于 2017-4-6 09:34
续。。。
ws2812.c



楼主我有些细节问题想请教,能加个qq联系吗?

jipin 发表于 2017-4-7 17:19:40

金金金金· 发表于 2017-4-7 16:21
楼主我有些细节问题想请教,能加个qq联系吗?

49707555
49707555

clide2000 发表于 2017-4-9 23:46:08

本帖最后由 clide2000 于 2017-5-2 17:17 编辑

您好,关于SOC方案调用ws2812,有例程吗?
我直接试了。在初始化以后,一直是绿灯,开灯和关灯,都没有效果,改变颜色与没有成功。
方便的话,是否能提供个使用的demo,估计是我使用的不对。
我是直接找了个esp8266的空工程添加的这2个文件,测试的。


已解决,见10楼。
直接按soc方案的烧写设置就成

jipin 发表于 2017-4-11 10:10:17

这个不难理解。自己找下原因、

智能小鸟 发表于 2017-4-11 16:53:47

这个不错啊,回头自己可以diy一个wifi控制的彩灯了,谢谢分享啊:)

经典智能 发表于 2017-4-12 16:04:26

不错,学习了!!

clide2000 发表于 2017-4-13 20:53:33

哪位兄台也玩过这个,可否给个调用的例子学习下。
一直尝试了几天,到目前也没有成功:Q

jipin 发表于 2017-4-14 22:54:51

clide2000 发表于 2017-4-13 20:53
哪位兄台也玩过这个,可否给个调用的例子学习下。
一直尝试了几天,到目前也没有成功 ...

看下代码就行了

clide2000 发表于 2017-5-2 17:16:05

不知是个人原因,还是通病,代码稍修改后,可以正常使用了。1复制ws2812.c文件到app\driver目录
2复制ws2812.h文件到app\include\driver目录
3在UserMain.c中的user_init()函数中添加初始化代码:
ws2812_init();   
4具体颜色调用
ws2812_strip(255,0,0, LEDS);//绿色
gizMSleep(); //延时
ws2812_strip(0,255,0, LEDS);//红色
gizMSleep();
ws2812_strip(0,0,255, LEDS);//蓝色
gizMSleep();
ws2812_strip(255,255,255, LEDS);//白色
gizMSleep();
ws2812_strip(0,0,0, LEDS);//关灯







另外,就在刚刚,又看到了bigfanofloT大牛发的新库,一会去试试。

http://club.gizwits.com/thread-6116-1-1.html?tn=96100419_hao_pg

clide2000 发表于 2017-5-3 20:36:12

其他朋友,有没有遇到初始化以后,总是亮着一个绿灯。
然后在调色时,有时候也会出一个灯颜色不正常。
有时全部关闭后,还是亮一个绿灯的情况?
我这测试了12颗灯的灯板。有这样的情况。

jipin 发表于 2017-5-5 08:34:42

clide2000 发表于 2017-5-3 20:36
其他朋友,有没有遇到初始化以后,总是亮着一个绿灯。
然后在调色时,有时候也会出一个灯颜色不正常。
有时 ...

试下修改颜色之前 设置一次黑色。

nile 发表于 2017-9-16 08:51:18

这个方案有原理图可以提供吗

xiaomingst 发表于 2020-1-9 14:13:56

这个很赞啊 成品方案智能灯我最近也准备弄一个 ~
页: [1]
查看完整版本: 亲测WS2812 RGB彩灯MCU和SOC方案实现