在这里插入代码片
`
#include "bsp_ws281x.h"
#include "bsp_systick.h"
uint8_t pixelBuffer[PIXEL_NUM][GRB];
void ws281x_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
WS281x_GPIO_CLK_FUNCTION(WS281x_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = WS281x_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WS281x_GPIOx, &GPIO_InitStruct);
GPIO_ResetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
ws281x_closeAll();
delay_ms(100);
}
static void ws281x_delay(uint32_t count)
{
while(count--);
}
void ws281x_sendTero(void)
{
//GPIO_SetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
GPIOA->BSRR = WS281x_GPIO_PIN;
ws281x_delay(1);
//GPIO_ResetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
GPIOA->BRR = WS281x_GPIO_PIN;
ws281x_delay(2);
}
void ws281x_sendOne(void)
{
//GPIO_SetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
GPIOA->BSRR = WS281x_GPIO_PIN;
ws281x_delay(2);
//GPIO_ResetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
GPIOA->BRR = WS281x_GPIO_PIN;
ws281x_delay(1);
}
void ws281x_sendReset(void)
{
GPIO_ResetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
ws281x_delay(114); //延时50us
//GPIO_SetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
}
void ws281x_show(void)
{
for(uint16_t i = 0; i < PIXEL_NUM; ++i)
{
for(uint8_t j = 0; j < GRB; ++j)
{
if(pixelBuffer[i][j] == WS_HIGH)
{
ws281x_sendOne();
}
else
{
ws281x_sendTero();
}
}
}
}
void ws281x_closeAll(void)
{
uint16_t i;
uint8_t j;
for(i = 0; i < PIXEL_NUM; ++i)
{
for(j = 0; j < 24; ++j)
{
pixelBuffer[i][j] = WS_LOW;
}
}
ws281x_show();
}
uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
return green << 16 | red << 8 | blue;
}
void ws281x_setPixelColor(uint16_t n, uint32_t GRBColor)
{
uint8_t i;
if(n < PIXEL_NUM)
{
for(i = 0; i < GRB; i++)
{
pixelBuffer[n][i] = ((GRBColor << i) & 0x800000) ? WS_HIGH : WS_LOW;
}
}
}
void ws281x_setPixelRGB(uint16_t n ,uint8_t red, uint8_t green, uint8_t blue)
{
uint8_t i;
if(n < PIXEL_NUM)
{
for(i = 0; i < GRB; ++i)
{
pixelBuffer[n][i] = (((ws281x_color(red,green,blue) << i) & 0x800000) ? WS_HIGH : WS_LOW);
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t ws281x_wheel(uint8_t wheelPos) {
wheelPos = 255 - wheelPos;
if(wheelPos < 85) {
return ws281x_color(255 - wheelPos * 3, 0, wheelPos * 3);
}
if(wheelPos < 170) {
wheelPos -= 85;
return ws281x_color(0, wheelPos * 3, 255 - wheelPos * 3);
}
wheelPos -= 170;
return ws281x_color(wheelPos * 3, 255 - wheelPos * 3, 0);
}
// Fill the dots one after the other with a color
void ws281x_colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i