STM32 ws2812b 最快点灯cubemx

文章目录

  • 前言
  • 一、cubemx配置
  • 二、代码
    • 1.ws2812b.c/ws2812b.h
    • 2.主函数


前言

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

本文使用的是pwm+dma的方式
文件

STM32 ws2812b 最快点灯cubemx_第1张图片


一、cubemx配置

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

STM32 ws2812b 最快点灯cubemx_第2张图片
接着打开tim的pwm
其中

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

STM32 ws2812b 最快点灯cubemx_第3张图片
还要打开DMA框起来的这几点一定要注意哈!!

STM32 ws2812b 最快点灯cubemx_第4张图片
生成文件即可

二、代码

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<
//			{
//				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();

你可能感兴趣的:(stm32,stm32,嵌入式硬件,单片机)