写一个用stm32f103c8t6驱动ws2812灯带的代码
时间: 2023-02-21 09:20:19 浏览: 156
#include "stm32f10x.h"
//定义IO口
#define WS2812_PORT GPIOB
#define WS2812_PIN GPIO_Pin_12
//定义颜色结构体
typedef struct
{
uint8_t g;
uint8_t r;
uint8_t b;
}WS2812_COLOR;
//定义灯带颜色数组
WS2812_COLOR led[8];
//定义灯带数据发送函数
void WS2812_Send(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
for(j=0;j<24;j++)
{
if((led[i].g>>j)&0x01)
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
else
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
if((led[i].r>>j)&0x01)
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
else
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
if((led[i].b>>j)&0x01)
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
else
{
GPIO_SetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
GPIO_ResetBits(WS2812_PORT,WS2812_PIN);
delay_us(1);
}
}
}
}
//定义灯带初始化函数
void WS2812_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = WS2812_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WS2812_PORT,&GPIO_InitStructure);
}
//定义延时函数
void delay_us(uint16_t us)
{
uint16_t i;
while(us--)
{
i=10;
while(i--);
}
}
我能为您做些什么?
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)