简介
引脚
注:带三角形的一头是VSS
特性
时序
上图输入码型的意思是,输入是0还是1,是通过对高低电平的不同延时,来区分0和1的输入
注:具体高低电平的延长时间,参考手册。
1)高位先发
2)在级联时,每经过一个像素点传输,信号减少24bit
3)24位代表RGB,每8位代表一个颜色,不过WS2812B中是GRB的顺序
代码
rgb.c文件
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "rgb.h"
//RGB存储,总共四个灯珠
uint8_t rgb_Ram[12]= {0};
/*****************************************************************************
* 功 能:ns延时
* 参 数:xns 多少ns
* 返回值:无
*****************************************************************************/
void Delay_ns(uint32_t xns) {
while(xns--)
__NOP();
}
/*****************************************************************************
* 功 能:发送Reset
* 参 数:无
* 返回值:无
*****************************************************************************/
void Ws2812b_Reset(void)
{
Delay_us(55);
}
/*****************************************************************************
* 功 能:发送0
* 参 数:无
* 返回值:无
*****************************************************************************/
void Ws2812b_Send0Code(void)
{
RGB_HIGH;
Delay_ns(2);
RGB_LOW;
Delay_ns(10);
}
/*****************************************************************************
* 功 能:发送1
* 参 数:无
* 返回值:无
*****************************************************************************/
void Ws2812b_Send1Code(void)
{
RGB_HIGH;
Delay_ns(10);
RGB_LOW;
Delay_ns(2);
}
/*****************************************************************************
* 功 能:设置对应RGB的颜色
* 参 数:several 第几个RGB
* r RGB中的R
* g RGB中的G
* b RGB中的B
* 返回值:无
*****************************************************************************/
void Ws2812b_SetRGB(uint8_t several,uint8_t r,uint8_t g,uint8_t b)
{
rgb_Ram[(several-1)*3+0]=g;
rgb_Ram[(several-1)*3+1]=r;
rgb_Ram[(several-1)*3+2]=b;
}
/*****************************************************************************
* 功 能:Refresh,点亮几个灯
* 参 数:several 点亮几个RGB
* 返回值:无
*****************************************************************************/
void Ws2812b_Refresh(uint8_t several)
{
uint8_t i =0,j =0;
for(i=0; i<several*3; i++)
{
for(j=0; j<8; j++)
{
if(rgb_Ram[i]&(0x80>>j)) {
Ws2812b_Send1Code();
}
else {
Ws2812b_Send0Code();
}
}
}
}
/*****************************************************************************
* 功 能:初始化RGB
* 参 数:无
* 返回值:无
*****************************************************************************/
void RGB_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=RGB;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
RGB_LOW;
SetRGB(1,Green);
SetRGB(2,Yellow);
SetRGB(3,Red);
SetRGB(4,Blue);
}
/*****************************************************************************
* 功 能:点亮RGB
* 参 数:无
* 返回值:无
*****************************************************************************/
void LightenRGB(void)
{
Ws2812b_Reset();
Ws2812b_Refresh(4);
}
rgb.h文件
#ifndef __RGB_H
#define __RGB_H
//引脚定义
#define RGB GPIO_Pin_9
#define RGB_HIGH GPIOB->BSRR = GPIO_Pin_9
#define RGB_LOW GPIOB->BRR = GPIO_Pin_9
//颜色定义
#define Green 0 ,255, 0
#define Pink 255 ,192 ,203
#define Red 255 ,0 ,0
#define Blue 0,0 ,255
#define Yellow 255 ,255, 0
void RGB_Init(void);
void LightenRGB(void);
void Ws2812b_SetRGB(uint8_t several,uint8_t r,uint8_t g,uint8_t b);
#endif
1)复制粘贴直接调用LightenRGB函数,就可以运行(需要添加Delay头文件)
2)时间需要自己调试
3)由于是ns级别的,推荐使用
翻转IO口
4)stm32的时钟周期在72mhz的系统时钟下为13.89ns
5)一个__nop();语句占用的是13.89ns
END~~