STM32播放WAV程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Page11/**2******************************************************************************3*FileName:TIM.c4*Description:Thisfileprovidescodefortheconfiguration5*oftheTIMinstancesforWAVPlayer.6*Auther:Dhg7*Data:2017-07-068*9******************************************************************************1011/*Includes------------------------------------------------------------------*/12#includetim.h13#includedac.h1415uint16_tData_tmp;16uint16_tData1;17uint32_tNumber=44;181920TIM_HandleTypeDefhtim6;21TIM_HandleTypeDefhtim9;2223/*TIM6initfunction*/24voidMX_TIM6_Init(void)25{26TIM_MasterConfigTypeDefsMasterConfig;2728htim6.Instance=TIM6;29htim6.Init.Prescaler=4-1;30htim6.Init.CounterMode=TIM_COUNTERMODE_UP;31htim6.Init.Period=1000-1;32if(HAL_TIM_Base_Init(&htim6)!=HAL_OK)33{34_Error_Handler(__FILE__,__LINE__);35}3637sMasterConfig.MasterOutputTrigger=TIM_TRGO_RESET;38sMasterConfig.MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE;39if(HAL_TIMEx_MasterConfigSynchronization(&htim6,&sMasterConfig)!=HAL_OK)40{41_Error_Handler(__FILE__,__LINE__);42}4344}45/*TIM9initfunction*/46voidMX_TIM9_Init(void)47{48TIM_OC_InitTypeDefsConfigOC;4950htim9.Instance=TIM9;51htim9.Init.Prescaler=0;52htim9.Init.CounterMode=TIM_COUNTERMODE_UP;53htim9.Init.Period=500-1;54htim9.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;55if(HAL_TIM_PWM_Init(&htim9)!=HAL_OK)56{57_Error_Handler(__FILE__,__LINE__);58}5960sConfigOC.OCMode=TIM_OCMODE_PWM1;61sConfigOC.Pulse=125*0.5;62sConfigOC.OCPolarity=TIM_OCPOLARITY_HIGH;63sConfigOC.OCFastMode=TIM_OCFAST_DISABLE;64if(HAL_TIM_PWM_ConfigChannel(&htim9,&sConfigOC,TIM_CHANNEL_1)!=HAL_OK)65{66_Error_Handler(__FILE__,__LINE__);67}6869HAL_TIM_MspPostInit(&htim9);7071}7273voidHAL_TIM_Base_MspInit(TIM_HandleTypeDef*tim_baseHandle)74{7576if(tim_baseHandle-Instance==TIM6)77{78此程序配置的采样率为(TIM6)16k,PWM频率(TIM9_CH1)144k;为何是这个比例(自己探索出来音质较佳,暂时无法理论解释)此代码可以实现16位单声道的WAV文件解码,通过PWM或者DAC输出音频,如何解码双声道可以自己探索!Page279/*Peripheralclockenable*/80__HAL_RCC_TIM6_CLK_ENABLE();8182/*TIM6interruptInit*/83HAL_NVIC_SetPriority(TIM6_DAC_IRQn,0,0);84HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);8586}87}8889voidHAL_TIM_PWM_MspInit(TIM_HandleTypeDef*tim_pwmHandle)90{9192if(tim_pwmHandle-Instance==TIM9)93{9495/*Peripheralclockenable*/96__HAL_RCC_TIM9_CLK_ENABLE();9798}99}100voidHAL_TIM_MspPostInit(TIM_HandleTypeDef*timHandle)101{102103GPIO_InitTypeDefGPIO_InitStruct;104if(timHandle-Instance==TIM9)105{106107/**TIM9GPIOConfiguration108PE5------TIM9_CH1109*/110GPIO_InitStruct.Pin=GPIO_PIN_5;111GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;112GPIO_InitStruct.Pull=GPIO_NOPULL;113GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_LOW;114GPIO_InitStruct.Alternate=GPIO_AF3_TIM9;115HAL_GPIO_Init(GPIOE,&GPIO_InitStruct);116117}118119}120121voidHAL_TIM_Base_MspDeInit(TIM_HandleTypeDef*tim_baseHandle)122{123124if(tim_baseHandle-Instance==TIM6)125{126127/*Peripheralclockdisable*/128__HAL_RCC_TIM6_CLK_DISABLE();129130}131}132133voidHAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef*tim_pwmHandle)134{135136if(tim_pwmHandle-Instance==TIM9)137{138139/*Peripheralclockdisable*/140__HAL_RCC_TIM9_CLK_DISABLE();141142}143}144145146voidHAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef*htim)147{148if(htim-Instance==htim6.Instance)149{150151//*****************************PWMOUTPUT********************************************************152153Data_tmp=(SOUND_DATA[Number+1]8)|(SOUND_DATA[Number]);154if(Data_tmp&0x8000)155{156Page3157Data_tmp=~Data_tmp+1;158Data_tmp&=0x7fff;159160Data_tmp=0x7FFF-Data_tmp;161}162else163{164Data_tmp=Data_tmp+0x8000;165}166167168Data_tmp=((float)Data_tmp/65535)*100;169170if(Data_tmp==Data1){}171else172{173USER_PWM_SetDutyRatio(&htim9,TIM_CHANNEL_1,Data_tmp,SET);174Data1=Data_tmp;175}176177if(Number712013)178{179Number=44;180HAL_TIM_Base_Stop_IT(&htim6);181}182else183{184Number+=4;185}186187188//*****************************DACOUTPUT********************************************************189190Data_tmp=(SOUND_DATA[Number+1]8)|(SOUND_DATA[Number]);191if(Data_tmp&0x8000)192{193194Data_tmp=~Data_tmp+1;195Data_tmp&=0x7fff;196197Data_tmp=0x7FFF-Data_tmp;198}199else200{201Data_tmp=Data_tmp+0x8000;202}203204205Data_tmp=((float)Data_tmp/65535)*4096;206207if(Data_tmp==Data1){}208else209{210HAL_DAC_SetValue(&hdac,DAC_CHANNEL_2,DAC_ALIGN_12B_R,Data_tmp);211HAL_DAC_Start(&hdac,DAC_CHANNEL_2);212Data1=Data_tmp;213}214215if(Number712013)216{217Number=44;218HAL_TIM_Base_Stop_IT(&htim6);219}220else221{222Number+=4;223}224225}226}227228voidUSER_PWM_SetDutyRatio(TIM_HandleTypeDef*htim,uint32_tChannel,uint8_tvalue,uint8_tPWM_Status)229{230TIM_OC_InitTypeDefsConfigOC;231232uint32_tperiod;233uint32_tpluse;Page4234235period=htim-Init.Period+1;236pluse=(value*period)/100;237238sConfigOC.OCMode=TIM_OCMODE_PWM1;239sConfigOC.Pulse=pluse;240sConfigOC.OCPolarity=TIM_OCPOLARITY_HIGH;241sConfigOC.OCFastMode=TIM_OCFAST_DISABLE;242HAL_TIM_PWM_ConfigChannel(htim,&sConfigOC,Channel);243if(PWM_Status==SET)244{245HAL_TIM_PWM_Start(htim,Channel);246}247else248{249;250}251252}253254constuint8_tSOUND_DATA[712014]={2550x52,0x49,0x46

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功