在这里插入代码片
`
#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)
{
GPIOA->BSRR = WS281x_GPIO_PIN;
ws281x_delay(1);
GPIOA->BRR = WS281x_GPIO_PIN;
ws281x_delay(2);
}
void ws281x_sendOne(void)
{
GPIOA->BSRR = WS281x_GPIO_PIN;
ws281x_delay(2);
GPIOA->BRR = WS281x_GPIO_PIN;
ws281x_delay(1);
}
void ws281x_sendReset(void)
{
GPIO_ResetBits(WS281x_GPIOx, WS281x_GPIO_PIN);
ws281x_delay(114);
}
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);
}
}
}
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);
}
void ws281x_colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<PIXEL_NUM; i++) {
ws281x_setPixelColor(i, c);
ws281x_show();
delay_ms(wait);
}
}
void ws281x_rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<PIXEL_NUM; i++) {
ws281x_setPixelColor(i, ws281x_wheel((i+j) & 255));
}
ws281x_show();
delay_ms(wait);
}
}
void ws281x_rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) {
for(i=0; i< PIXEL_NUM; i++) {
ws281x_setPixelColor(i,ws281x_wheel(((i * 256 / PIXEL_NUM) + j) & 255));
}
ws281x_show();
delay_ms(wait);
}
}
void ws281x_theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) {
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
ws281x_setPixelColor(i+q, c);
}
ws281x_show();
delay_ms(wait);
for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
ws281x_setPixelColor(i+q, 0);
}
}
}
}
void ws281x_theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) {
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
ws281x_setPixelColor(i+q, ws281x_wheel( (i+j) % 255));
}
ws281x_show();
delay_ms(wait);
for (uint16_t i=0; i < PIXEL_NUM; i=i+3) {
ws281x_setPixelColor(i+q, 0);
}
}
}
}
#ifndef __BSP_WS281X_H
#define __BSP_WS281X_H
#include "stm32f10x.h"
#define WS281x_GPIO_CLK RCC_APB2Periph_GPIOA
#define WS281x_GPIO_CLK_FUNCTION RCC_APB2PeriphClockCmd
#define WS281x_GPIOx GPIOA
#define WS281x_GPIO_PIN GPIO_Pin_0
#define PIXEL_NUM 15
#define GRB 24
#define WS_HIGH 1
#define WS_LOW 0
void ws281x_sendOne(void);
void ws281x_sendTero(void);
void ws281x_init(void);
void ws281x_closeAll(void);
void ws281x_rainbowCycle(uint8_t wait);
uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue);
void ws281x_setPixelColor(uint16_t n ,uint32_t GRBcolor);
void ws281x_show(void);
void ws281x_theaterChase(uint32_t c, uint8_t wait);
void ws281x_colorWipe(uint32_t c, uint8_t wait);
void ws281x_rainbow(uint8_t wait);
void ws281x_theaterChaseRainbow(uint8_t wait);
#endif
******************************************************************************
* @file main.c
* @author Apex yuan
* @version V1.0.0
* @date 2019-8-16
* @brief Main program body
******************************************************************************
* @attention
******************************************************************************
*/
#include "stm32f10x.h"
#include "bsp.h"
int main(void)
{
bsp_init();
while (1)
{
ws281x_rainbowCycle(20);
ws281x_closeAll();
delay_ms(1000);
}
}