天天看點

STM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGBSTM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGB

STM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGB

系列文章傳送門:

STM32F4多路PWM DMA控制千顆WS2812/SK6812配置過程全解析

STM32F405 多路DMA+PWM方式控制2000+WS2812燈優化buffer占用flash空間不足問題

借WS2812淺談WS2812調試思想

正文:

STM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGBSTM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGB
STM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGBSTM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGB

關鍵初始化代碼如下

定時器初始化

#include "gpio_ws2812.h"
#include "pwm.h"
/*************************
函數名:WS2812_Init
函數功能:WS2812的PWM配置
函數輸入:無
函數傳回值: 無
描述:配置的引腳 PH10-12 PI0
*************************/
void WS2812_TIM_Init(void)
{
	GPIO_InitTypeDef          gpio;
	TIM_TimeBaseInitTypeDef   tim;
	TIM_OCInitTypeDef         oc;
	
	gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 ; //CH1,2,3
	gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOH,&gpio);
	
	gpio.GPIO_Pin = GPIO_Pin_0; //CH4
	gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOI,&gpio);
	
	GPIO_PinAFConfig(GPIOH,GPIO_PinSource10,GPIO_AF_TIM5);//定時器5 通道1
	GPIO_PinAFConfig(GPIOH,GPIO_PinSource11,GPIO_AF_TIM5);//定時器5 通道2
	GPIO_PinAFConfig(GPIOH,GPIO_PinSource12,GPIO_AF_TIM5);//定時器5 通道3
	GPIO_PinAFConfig(GPIOI,GPIO_PinSource0,GPIO_AF_TIM5);//定時器5 通道4
	
	/* TIM5 */
	tim.TIM_Prescaler = 19-1; //18OK
	tim.TIM_CounterMode = TIM_CounterMode_Up;
	tim.TIM_Period = 1*5;   //4->1us //0.5HIGH 0.75LOW 0碼; 0.75HIGH 0.5LOW 1碼
	tim.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM5,&tim); //初始化時間基數機關
	
	oc.TIM_OCMode = TIM_OCMode_PWM2;
	oc.TIM_OutputState = TIM_OutputState_Enable;
	oc.TIM_OutputNState = TIM_OutputState_Disable;
	oc.TIM_Pulse = 0;
	oc.TIM_OCPolarity = TIM_OCPolarity_Low;
	oc.TIM_OCNPolarity = TIM_OCPolarity_High;
	oc.TIM_OCIdleState = TIM_OCIdleState_Reset;
	oc.TIM_OCNIdleState = TIM_OCIdleState_Set;
	
	TIM_OC1Init(TIM5,&oc);//定時器5 通道1
	TIM_OC2Init(TIM5,&oc);//定時器5 通道2
	TIM_OC3Init(TIM5,&oc);//定時器5 通道3
	TIM_OC4Init(TIM5,&oc);//定時器5 通道4
	
	TIM_OC1PreloadConfig(TIM5,TIM_OCPreload_Enable);
	TIM_OC2PreloadConfig(TIM5,TIM_OCPreload_Enable);
	TIM_OC3PreloadConfig(TIM5,TIM_OCPreload_Enable);
	TIM_OC4PreloadConfig(TIM5,TIM_OCPreload_Enable);
	
	TIM_ARRPreloadConfig(TIM5,ENABLE);
	
	TIM_Cmd(TIM5,DISABLE);
	}
           

DMA初始化

#define WS2812BIT_HIGH 3
#define WS2812BIT_LOW 1
const u8 WS2812BitDef[2] ={WS2812BIT_LOW,WS2812BIT_HIGH};
#define WS2812_NUMS 350
#define WS2812_SIZE 24*WS2812_NUMS+1 //最後一bit為reset電平 //還有另一種實作思路是使能DMA傳輸完成中斷 并在該中斷中将CCR寄存器置0
u32 Pwm5_3_DMABuffer[WS2812_SIZE]={0};

void WS2812_TIM5_3_DMA_Init(void)
{
	NVIC_InitTypeDef nvic;
	DMA_InitTypeDef dma;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);   //使能DMA傳輸
	delay_ms(5);
	
	DMA_Cmd(DMA1_Stream0, DISABLE);
	DMA_DeInit(DMA1_Stream0);
	dma.DMA_Channel = DMA_Channel_6;
	dma.DMA_PeripheralBaseAddr = (uint32_t)(&TIM5->CCR3);  /* DMA外設基位址 *///DMA外設TIM5-CCR3位址/
	dma.DMA_Memory0BaseAddr = (uint32_t)Pwm5_3_DMABuffer; ///* DMA記憶體基位址 */DMA記憶體基位址/
	dma.DMA_DIR = DMA_DIR_MemoryToPeripheral;/* 資料傳輸方向,從記憶體讀取發送到外設 */
	dma.DMA_BufferSize = WS2812_SIZE;/* DMA通道的DMA緩存的大小 */
	dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;/* 外設位址寄存器不變 */
	dma.DMA_MemoryInc = DMA_MemoryInc_Enable;/* 記憶體位址寄存器遞增 */
	dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;/* 資料寬度為32位 */
	dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;/* 資料寬度為32位 */
	dma.DMA_Mode = DMA_Mode_Normal;      ///* 工作在正常模式 */工作在正常緩存模式
	dma.DMA_Priority = DMA_Priority_Medium;   //DMA通道 x擁有中優先級 
	dma.DMA_FIFOMode = DMA_FIFOMode_Disable;   //DMA通道x沒有設定為記憶體到記憶體傳輸
	dma.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;//DMA_FIFOThreshold_HalfFull;//DMA_FIFOThreshold_1QuarterFull;
	dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	
	/* 配置DMA */
	DMA_Init(DMA1_Stream0, &dma);
	
	TIM_DMACmd(TIM5, TIM_DMA_Update, ENABLE);
	
	DMA_Cmd(DMA1_Stream0, DISABLE);  /*不使能DMA */  
	TIM_Cmd(TIM5, ENABLE);  /* 使能TIM5 */
	}
           

調用發送部分

u32 DMAsetcounter=0;
bool PWM5_3_DMA_Enable(void)
{
	static u8 start_flag=0;
	if(DMA_GetFlagStatus(DMA1_Stream0,DMA_FLAG_TCIF0)!= RESET || start_flag!=1) //傳輸完成标志,若在傳輸過程中再次設定将會打亂時序
	{
		start_flag=1;
		DMA_ClearFlag(DMA1_Stream0,DMA_IT_TCIF0);
		DMA_Cmd(DMA1_Stream0, DISABLE );
		DMA_SetCurrDataCounter(DMA1_Stream0,DMAsetcounter); //WS2812_SIZE
		DMA_Cmd(DMA1_Stream0, ENABLE);
		//TIM_Cmd(TIM5, ENABLE);  /* 使能TIM3 */
		TIM5->EGR |= 0x00000001; /* 由于最後一次ARR值為0,這是為了停止定時器對io口的操作,但是不要忽略了一點:CNT并沒有停止計數,而且是不會再停下來,如果沒有手動操作的話,是以需要在每次dma使能時加上一句,将EGR裡的UG位置1,清零計數器 */
		return true;
	}
	else
	{
		return false;
	}
}
	
bool WS2812_UpdateColor(u8 colors[][3],u16 led_nums) //GRB 高位先發
{
	if(led_nums>WS2812_NUMS) return false;
	for(int i=0;i<led_nums;i++)
	{
		for(int channel=WS2812_GREEN;channel<WS2812_BLUE+1;channel++) //這兩個for可合并為一個
		{
			for(int bit=7;bit>=0;bit--)
			{
				Pwm5_3_DMABuffer[i*24+channel*8+(7-bit)]=WS2812BitDef[*(colors[i] + channel)>>bit&0x01];
			}
		}
	}
	Pwm5_3_DMABuffer[led_nums*24]=0;
	DMAsetcounter=led_nums*24+1;
	PWM5_3_DMA_Enable();
	return true;
}
           
STM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGBSTM32F427庫函數配置DMA驅動TIM5 PWM驅動WS2812B單總線全彩RGB

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協定,轉載請附上原文出處連結和本聲明。

本文連結:https://blog.csdn.net/yx19981001/article/details/102457819

繼續閱讀