STM32 ws2812b 最快点灯cubemx


前言

吐槽
想用stm32控制一下ws2812b的灯珠,结果发下没有一个好用的。
emmm!!!
自己来吧!!!!
本篇基本不讲原理直接照着做就能用。
想知道原理的看b站这个视频我认为这个up讲的很不错。
文件
本文使用的是pwm+dma的方式

在这里插入图片描述

多屏显示

这里是引用


一、cubemx配置

首先配置时钟树,配置成你最高的频率就行,我的是100MHZ

在这里插入图片描述
接着打开tim的pwm
其中

125 = 最高频率/800 000
如果你的是72MHZ那么你就填90-1

在这里插入图片描述
还要打开DMA框起来的这几点一定要注意哈!!

在这里插入图片描述
生成文件即可

二、代码

1.ws2812b.c/ws2812b.h

只需要移植几个地方,首先是灯的数量,第二呢是 HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)pwmData, indx);中的htim1以及TIM_CHANNEL_1

#include "ws2812b.h"
#include "tim.h"

#define MAX_LED 25	//灯的数量

#define USE_BRIGHTNESS 0	//亮度

uint8_t LED_Data[MAX_LED][4];
uint8_t LED_Mod[MAX_LED][4];  // for brightness

void Set_LED (int LEDnum, int Red, int Green, int Blue)//RGB
{
	LED_Data[LEDnum][0] = LEDnum;
	LED_Data[LEDnum][1] = Green;
	LED_Data[LEDnum][2] = Red;
	LED_Data[LEDnum][3] = Blue;
}
void Set_LED_HEX(int LEDnum, uint32_t colorValue) //十六进制
{
	LED_Data[LEDnum][0] = LEDnum;
	LED_Data[LEDnum][2] = (colorValue >> 16) & 0xFF;  // R
	LED_Data[LEDnum][1] = (colorValue >> 8) & 0xFF;   // G
	LED_Data[LEDnum][3] = colorValue & 0xFF;          // Blue
}

#define PI 3.14159265
void Set_Brightness (int brightness)  // 0-45
{
#if USE_BRIGHTNESS

	if (brightness > 45) brightness = 45;
	for (int i=0; i<MAX_LED; i++)
	{
		LED_Mod[i][0] = LED_Data[i][0];
		for (int j=1; j<4; j++)
		{
			float angle = 90-brightness;  // in degrees
			angle = angle*PI / 180;  // in rad
			LED_Mod[i][j] = (LED_Data[i][j])/(tan(angle));
		}
	}

#endif

}


uint16_t pwmData[(24*MAX_LED)+50];

void WS2812_Send (void)
{
	uint32_t indx=0;
	uint32_t color;


	for (int i= 0; i<MAX_LED; i++)
	{
#if USE_BRIGHTNESS
		color = ((LED_Mod[i][1]<<16) | (LED_Mod[i][2]<<8) | (LED_Mod[i][3]));
#else
		color = ((LED_Data[i][1]<<16) | (LED_Data[i][2]<<8) | (LED_Data[i][3]));
#endif
		//手动设置
		for (int i=23; i>=0; i--)
		{
			if (color&(1<<i))
			{
				pwmData[indx] = 83;  // 2/3 of 125
			}

			else pwmData[indx] = 42;  // 1/3 of 125

			indx++;
		}
		//自动获取
//		for (int i=23; i>=0; i--)
//		{
//			if (color&(1<<i))
//			{
//				cc = __HAL_TIM_GET_AUTORELOAD(&htim1);
//				pwmData[indx] = (2.0/3.0*(__HAL_TIM_GET_AUTORELOAD(&htim1)+1));  // 2/3 of 125
//			}

//			else pwmData[indx] = (1.0/3.0*(__HAL_TIM_GET_AUTORELOAD(&htim1)+1));  // 1/3 of 90

//			indx++;
//		}		

	}

	for (int i=0; i<50; i++)
	{
		pwmData[indx] = 0;
		indx++;
	}

	HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)pwmData, indx);

}


#ifndef __WS2812B_H


#define __WS2812B_H
#include "main.h"

void Set_LED (int LEDnum, int Red, int Green, int Blue);//RGB

void Set_LED_HEX(int LEDnum, uint32_t colorValue);//十六进制

void Set_Brightness (int brightness);  // 0-45
void WS2812_Send (void);


#endif

2.主函数

颜色对应的十六进制可以看这个网站

Set_LED_HEX(0,0xFF0000);//十六进制设置
Set_LED (1,0,255,0);//RGB数值设置
Set_LED_HEX(2,0xFF0000);
Set_LED_HEX(3,0xFF0000);
Set_LED_HEX(4,0xFF0000);	
Set_LED_HEX(5,0xFF0000);	
WS2812_Send();

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值