找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1477|回复: 2
打印 上一主题 下一主题
收起左侧

STM32F103C8T6单片机驱动WS2812,从零到放弃

[复制链接]
跳转到指定楼层
楼主
首先是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时序要求


可以看到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);
        }
}



可以看到,单片机主频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
        }
}



可以看到,高电平时间由原来的约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
        }
}



可以看到,在添加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();
        }
}



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



因为要合并两个函数,所以预留了一点时间
接下来合并函数
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发送 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();
        }
}


可以看到,这颗灯珠已经可以被点亮了






评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:277550 发表于 2024-10-28 09:37 | 只看该作者
arduino却容易很多
回复

使用道具 举报

板凳
ID:1001155 发表于 2024-10-28 10:05 | 只看该作者
最后一段代码WS2812_SendRGB以前要放10ms上电延时!!!要放10ms上电延时!!!10ms上电延时!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表