|
沙发
楼主 |
发表于 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[600];
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[j++]=1) : (buff[j++]=0);
mask >>= 1;
}
}
i=0;
while (i<= j)
{
( buff[i++] & 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);
void ws2812_init();
#endif
|
|