• ESP8266--SDK开发(驱动WS2812B)


    一、WS2812彩灯介绍

    WS2812是一个集控制电路与发光电路于一体的智能外控LED光源,外型与5050LED灯珠相同,每个灯珠都是一个像素点。像素点内包含数字接口、数据锁存、信号整形放大驱动电路,还包含高精度的内部振荡器和12V高压可编程定电流控制部分。
    在这里插入图片描述
    数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。
    在这里插入图片描述

    按照数据传输协议即可对WS2812进行驱动,当然对于WS2812有许多开源的驱动库,例如:Adafruit_NeoPixel驱动库

    #ifndef _Adafruit_NeoPixel_H
    #define _Adafruit_NeoPixel_H
    
    #include "c_types.h"
    #include "user_interface.h"
    #include "ets_sys.h"
    #include "gpio.h"
    
    //thanks for https://github.com/cnlohr/ws2812esp8266
    //thanks for https://github.com/adafruit/Adafruit_NeoPixel
    
    #define WSGPIO 0 //must use the ESP8266 GPIO0 as the pin to drive WS2812B RGB LED!!!
    //user can change
    #define PIXEL_MAX 1//the total numbers of LEDs you are used in your project
    
    //You will have to 	os_intr_lock();  	os_intr_unlock();
    
    void setAllColor(uint8_t r, uint8_t g, uint8_t b);
    void setOneColor(uint16_t n, uint32_t c);
    //void setAllColor(uint32_t c);
    void cleanOneColor(uint16_t n, uint32_t c);
    void cleanAllColor(void);
    
    void setAllPixel(void);
    
    uint32_t Color(uint8_t r, uint8_t g, uint8_t b);
    uint32_t Wheel(uint8_t WheelPos);
    void rainbowCycle(uint8_t wait) ;
    void theaterChase(uint32_t c, uint8_t wait);
    void theaterChaseRainbow(uint8_t wait);
    void colorWipe(uint32_t c, uint8_t wait);
    void WS2812B_Test(void);
    void ICACHE_FLASH_ATTR WS2812B_Init(void);
    
    uint32_t changeL(uint32_t rgb, float k);
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    #include "driver/Adafruit_NeoPixel.h"
    #include "ets_sys.h"
    #include "osapi.h"
    /*
     #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
    	gpio_output_set(bit_value<
    
    //I just used a scope to figure out the right time periods.
    void SEND_WS_0() {
    	uint8_t time;
    	time = 3;
    	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 SEND_WS_1() {
    	uint8_t time;
    	time = 7;
    	while (time--)
    		WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 1);
    	time = 3;
    	while (time--)
    		WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ID_PIN(WSGPIO), 0);
    }
    
    void // ICACHE_FLASH_ATTR
    WS2812Send_8bit(uint8_t dat) {
    	uint16_t i;
    	GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0);
    	ets_intr_lock();
    	uint8_t mask = 0x80;
    	uint8_t byte = dat;
    	while (mask) {
    		if (byte & mask)
    			SEND_WS_1();
    		else
    			SEND_WS_0();
    		mask >>= 1;
    	}
    
    	ets_intr_unlock();
    }
    
    //GRB format,MSB firsr.
    void //ICACHE_FLASH_ATTR
    WS2812BSend_24bit(uint8_t R, uint8_t G, uint8_t B) {
    	WS2812Send_8bit(G);
    	WS2812Send_8bit(R);
    	WS2812Send_8bit(B);
    }
    //delay for millisecond
    void HAL_Delay(int time) {
    	os_delay_us(time * 1000);
    }
    
    static uint8_t rBuffer[PIXEL_MAX] = { 0 };
    static uint8_t gBuffer[PIXEL_MAX] = { 0 };
    static uint8_t bBuffer[PIXEL_MAX] = { 0 };
    
    void setAllColor(uint8_t r, uint8_t g, uint8_t b)
    {
    	uint8_t i;
    	for (i = 0; i < PIXEL_MAX; i++) {
    		rBuffer[i] = r;
    		gBuffer[i] = g;
    		bBuffer[i] = b;
    	}
    }
    
    void setOneColor(uint16_t n, uint32_t c) {
    	rBuffer[n] = (uint8_t) (c >> 16);
    	gBuffer[n] = (uint8_t) (c >> 8);
    	bBuffer[n] = (uint8_t) c;
    }
    
    //void setAllColor(uint32_t c) {
    //	uint8_t r, g, b;
    //	r = (uint8_t) (c >> 16);
    //	g = (uint8_t) (c >> 8);
    //	b = (uint8_t) c;
    //	uint8_t i;
    //	for (i = 0; i < PIXEL_MAX; i++) {
    //		rBuffer[i] = r;
    //		gBuffer[i] = g;
    //		bBuffer[i] = b;
    //	}
    //}
    
    void cleanOneColor(uint16_t n, uint32_t c) {
    	rBuffer[n] = 0;
    	gBuffer[n] = 0;
    	bBuffer[n] = 0;
    }
    
    void cleanAllColor() {
    	uint8_t i;
    	for (i = 0; i < PIXEL_MAX; i++) {
    		rBuffer[i] = 0;
    		gBuffer[i] = 0;
    		bBuffer[i] = 0;
    	}
    }
    
    void setAllPixel(void) {
    	uint8_t i;
    	for (i = 0; i < PIXEL_MAX; i++) {
    		WS2812BSend_24bit(rBuffer[i], gBuffer[i], bBuffer[i]);
    	}
    }
    
    uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
    	return ((uint32_t) r << 16) | ((uint32_t) g << 8) | b;
    }
    uint32_t Wheel(uint8_t WheelPos) {
    	WheelPos = 255 - WheelPos;
    	if (WheelPos < 85) {
    		return Color(255 - WheelPos * 3, 0, WheelPos * 3);
    	}
    	if (WheelPos < 170) {
    		WheelPos -= 85;
    		return Color(0, WheelPos * 3, 255 - WheelPos * 3);
    	}
    	WheelPos -= 170;
    	return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
    }
    
    void rainbow(uint8_t wait) {
    	uint16_t i, j;
    	for (j = 0; j < 256; j++) {
    		for (i = 0; i < PIXEL_MAX; i++) {
    			setOneColor(i, Wheel((i + j) & 255));
    		}
    		HAL_Delay(wait);
    	}
    	setAllPixel();
    }
    // Slightly different, this makes the rainbow equally distributed throughout
    void rainbowCycle(uint8_t wait) {
    	uint16_t i, j;
    
    	for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    		for (i = 0; i < PIXEL_MAX; i++) {
    			setOneColor(i, Wheel(((i * 256 / PIXEL_MAX) + j) & 255));
    		}
    		HAL_Delay(wait);
    	}
    	setAllPixel();
    }
    //Theatre-style crawling lights.o??¨¹¦Ì?
    void theaterChase(uint32_t c, uint8_t wait) {
    	int i, j, q;
    	for (j = 0; j < 10; j++) {  //do 10 cycles of chasing
    		for (q = 0; q < 3; q++) {
    			for (i = 0; i < PIXEL_MAX; i = i + 1)  //turn every one pixel on
    					{
    				setOneColor(i + q, c);
    			}
    			HAL_Delay(wait);
    
    			for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel off
    					{
    				setOneColor(i + q, 0);
    			}
    		}
    	}
    	setAllPixel();
    }
    
    //Theatre-style crawling lights with rainbow effect
    void theaterChaseRainbow(uint8_t wait) {
    	int i, j, q;
    	for (j = 0; j < 256; j++) {     // cycle all 256 colors in the wheel
    		for (q = 0; q < 3; q++) {
    			for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel on
    					{
    				setOneColor(i + q, Wheel((i + j) % 255));
    			}
    
    			HAL_Delay(wait);
    
    			for (i = 0; i < PIXEL_MAX; i = i + 1) //turn every one pixel off
    					{
    				setOneColor(i + q, 0);
    			}
    		}
    	}
    	setAllPixel();
    }
    // Fill the dots one after the other with a color
    void colorWipe(uint32_t c, uint8_t wait) {
    	uint16_t i = 0;
    	for (i = 0; i < PIXEL_MAX; i++) {
    		setOneColor(i, c);
    		HAL_Delay(wait);
    	}
    }
    
    void //ICACHE_FLASH_ATTR
    WS2812B_Init(void) {
    	setAllColor(0,0,0);
    	setAllPixel();
    }
    
    void WS2812B_Test(void) {
    	HAL_Delay(500);
    	rainbowCycle(20);
    	theaterChaseRainbow(50);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212

    使用注意事项

    1、将文件复制到自己的SDK工程

    复制文件到文件夹,Clean Project一下就会自动加载到IDE中

    在这里插入图片描述
    在这里插入图片描述


    2、WS2812B的数据线一定要使用GPIO0

    在这里插入图片描述


    3、设置WS2812B的小灯个数

    在这里插入图片描述


    4、导入头文件并初始化

    #include "driver/Adafruit_NeoPixel.h"
    
    //初始化GPIO0
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
    GPIO_OUTPUT_SET(0, 0);
    os_delay_us(200);
    
    //初始化WS2812B
    WS2812B_Init();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5、设置/清除RGB的值

    设置了RGB的值,只是将颜色值写入颜色缓存数组而已,必须发送到WS2812B才能改变颜色

    在这里插入图片描述


    5、发送RGB的值

    在这里插入图片描述


    二、效果展示

    在这里插入图片描述


    三、七色灯代码

    #include "driver/Adafruit_NeoPixel.h"
    
    /******************************************************************************
     * FunctionName : user_init
     * Description  : entry of user application, init user function here
     * Parameters   : none
     * Returns      : none
    *******************************************************************************/
    void ICACHE_FLASH_ATTR
    user_init(void)
    {
    	uart_init(115200,115200);
    	os_delay_us(1000);
    	os_printf("\r\n=================================================\r\n");
    	os_printf("\t Project:\t%s\r\n", ProjectName);
    	os_printf("\t SDK version:\t%s", system_get_sdk_version());
    	os_printf("\r\n=================================================\r\n");
    
    	PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
    	GPIO_OUTPUT_SET(0, 0);
    	os_delay_us(200);
    
    	WS2812B_Init();
    
    	while(1) {
    		//红
    		setAllColor(255,0,0);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//橙
    		setAllColor(255,165,0);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//黄
    		setAllColor(255,255,0);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//绿
    		setAllColor(0,255,0);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//蓝
    		setAllColor(0,0,255);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//靛
    		setAllColor(0,255,255);
    		setAllPixel();
    		os_delay_us(300000);
    
    		//紫
    		setAllColor(139,0,255);
    		setAllPixel();
    		os_delay_us(300000);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    附:彩虹七色码值

    彩虹七色16进制码
    红色 #FF0000
    橙色 #FF7F00
    黄色 #FFFF00
    绿色 #00FF00
    青色 #00FFFF
    蓝色 #0000FF
    紫色 #8B00FF

    彩虹七色RGB值
    赤色 【RGB】255, 0, 0
    橙色 【RGB】 255, 165, 0
    黄色 【RGB】255, 255, 0
    绿色 【RGB】0, 255, 0
    青色 【RGB】0, 255, 255
    蓝色 【RGB】0, 0, 255
    紫色 【RGB】139, 0, 255

  • 相关阅读:
    2023工博会强势回归!智微工业携八大系列重磅亮相
    AI更改视频语言的神奇网址:让郭德纲讲英语成为现实!
    计算机丢失msvcp140_1.dll的解决办法,丢失msvcp140_1.dll的原因
    R-FCN: Object Detection via Region-based Fully Convolutional Networks(2016.6)
    服装行业数字化改革及未来趋势
    JOSEF 同步检查继电器 JT-1 额定电压100V 柜内固定安装,板前接线
    智能机器人云控平台
    2022最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)
    nginx配置参数解释
    javasebasic
  • 原文地址:https://blog.csdn.net/Mr_robot_strange/article/details/127727530