找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1554|回復: 2
打印 上一主題 下一主題
收起左側(cè)

STM32F103C8T6單片機驅(qū)動WS2812,從零到放棄

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1001155 發(fā)表于 2024-10-27 23:25 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
首先是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發(fā)送數(shù)據(jù),我們看一下WS2812時序要求


可以看到WS2812對時間要求比較嚴格,我們來操作GPIO
首先如果直接調(diào)用庫函數(shù)中的GPIO_SetBits()GPIO_ResetBits()函數(shù)
我們來試一下
int main(void)
{
        WS2812_PinInit();
        while(1)
        {
                GPIO_SetBits(GPIOB,GPIO_Pin_11);
                GPIO_ResetBits(GPIOB,GPIO_Pin_11);
        }
}



可以看到,單片機主頻72MHz才堪堪達到要求,如果更換其他型號主頻較低的單片機,可能就達不到要求了
我們打開GPIO_SetBits()GPIO_ResetBits()函數(shù)
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()函數(shù)在這里的作用是檢查GPIO_SetBits庫函數(shù)傳入的參數(shù)是否為真,如果為真,就什么也不執(zhí)行,如果為假,就會在源程序編譯的時候報錯!
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
        }
}



可以看到,高電平時間由原來的約160us變?yōu)榱爽F(xiàn)在的約40us
接下來就開始向WS2812發(fā)送 ‘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
        }
}



可以看到,在添加12個nop函數(shù)后,高電平時序大概能滿足要求了
接下來就是滿足低電平的時間
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();
        }
}



低電平延時大概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();
        }
}



因為要合并兩個函數(shù),所以預留了一點時間
接下來合并函數(shù)
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();
}


可以看到,波形是可以滿足時序要求的
接下來就是向WS2812發(fā)送 3 字節(jié)的RGB數(shù)據(jù)
需要注意的是,WS2812數(shù)據(jù)發(fā)送順序是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 ++)                                                                        //發(fā)送8位綠色數(shù)據(jù)
        {
                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 ++)                                                                        //發(fā)送8位紅色數(shù)據(jù)
        {
                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 ++)                                                                        //發(fā)送8位藍色數(shù)據(jù)
        {
                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();
        }
}


可以看到,這顆燈珠已經(jīng)可以被點亮了






評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:277550 發(fā)表于 2024-10-28 09:37 | 只看該作者
arduino卻容易很多
回復

使用道具 舉報

板凳
ID:1001155 發(fā)表于 2024-10-28 10:05 | 只看該作者
最后一段代碼WS2812_SendRGB以前要放10ms上電延時。!要放10ms上電延時!。10ms上電延時。!
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表