首先是WS2812引腳初始化代碼,這點和其他配置GPIO代碼相同
void WS2812_PinInit(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
WS2812_Reset();
}
其次是要向WS2812發送數據,我們看一下WS2812時序要求
aee76f7912338d4949a810e3a4f3a015.png (59.23 KB, 下載次數: 1)
下載附件
2024-10-27 22:31 上傳
可以看到WS2812對時間要求比較嚴格,我們來操作GPIO
首先如果直接調用庫函數中的GPIO_SetBits()和GPIO_ResetBits()函數
我們來試一下
int main(void)
{
WS2812_PinInit();
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_11);
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
}
}
51hei圖片_20241027223800.jpg (368.5 KB, 下載次數: 0)
下載附件
2024-10-27 22:38 上傳
可以看到,單片機主頻72MHz才堪堪達到要求,如果更換其他型號主頻較低的單片機,可能就達不到要求了
我們打開GPIO_SetBits()和GPIO_ResetBits()函數
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = GPIO_Pin;
}
其中,assert_param()函數在這里的作用是檢查GPIO_SetBits庫函數傳入的參數是否為真,如果為真,就什么也不執行,如果為假,就會在源程序編譯的時候報錯!
而GPIOx->BSRR = GPIO_Pin;就是操作GPIO引腳設置高電平
GPIOx->BRR = GPIO_Pin; 就是操作GPIO引腳設置低電平
那么,具體怎么操作呢
int main(void)
{
WS2812_PinInit();
while(1)
{
GPIOB->BSRR = GPIO_Pin_11; //拉高PB11
GPIOB->BRR = GPIO_Pin_11; //拉低PB11
}
}
51hei圖片_20241027224857.jpg (165.93 KB, 下載次數: 1)
下載附件
2024-10-27 22:49 上傳
可以看到,高電平時間由原來的約160us變為了現在的約40us
接下來就開始向WS2812發送 ‘0’ 碼
int main(void)
{
WS2812_PinInit();
while(1)
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
}
}
51hei圖片_20241027225338.jpg (400.73 KB, 下載次數: 1)
下載附件
2024-10-27 22:54 上傳
可以看到,在添加12個nop函數后,高電平時序大概能滿足要求了
接下來就是滿足低電平的時間
int main(void)
{
WS2812_PinInit();
while(1)
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
51hei圖片_20241027225907.jpg (401.97 KB, 下載次數: 1)
下載附件
2024-10-27 22:59 上傳
低電平延時大概30個nop
接下來就是 ‘1’ 碼
int main(void)
{
WS2812_PinInit();
while(1)
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
51hei圖片_20241027230344.jpg (370.09 KB, 下載次數: 1)
下載附件
2024-10-27 23:04 上傳
因為要合并兩個函數,所以預留了一點時間
接下來合并函數
void WS2812_SendBit(uint8_t Bit);
int main(void)
{
WS2812_PinInit();
while(1)
{
WS2812_SendBit(1);
WS2812_SendBit(0);
}
}
void WS2812_SendBit(uint8_t Bit)
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
if(Bit != 0)
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
51hei圖片_20241027231101.jpg (431.07 KB, 下載次數: 1)
下載附件
2024-10-27 23:11 上傳
可以看到,波形是可以滿足時序要求的
接下來就是向WS2812發送 3 字節的RGB數據
需要注意的是,WS2812數據發送順序是G R B,也就是綠色在前,然后是紅色,藍色。而不是紅色,綠色,藍色
void WS2812_SendRGB(uint8_t R,uint8_t G,uint8_t B);
int main(void)
{
WS2812_PinInit();
WS2812_SendRGB(50,0,0);
while(1)
{
}
}
void WS2812_SendRGB(uint8_t R,uint8_t G,uint8_t B)
{
for(int i = 0;i < 8;i ++) //發送8位綠色數據
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
if((G & (0x80 >> i)) == 0)
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
for(int i = 0;i < 8;i ++) //發送8位紅色數據
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
if((R & (0x80 >> i)) == 0)
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
for(int i = 0;i < 8;i ++) //發送8位藍色數據
{
GPIOB->BSRR = GPIO_Pin_11;//拉高PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
if((B & (0x80 >> i)) == 0)
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
GPIOB->BRR = GPIO_Pin_11;//拉低PB11
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
51hei圖片_20241027232302.jpg (250.57 KB, 下載次數: 0)
下載附件
2024-10-27 23:23 上傳
可以看到,這顆燈珠已經可以被點亮了
|