官方例程播放音频代码:
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Start wave playing
* @param None
* @retval None
*/
uint8_t WavePlayerMenu_Start(uint8_t *DirName, uint8_t *FileName, uint32_t *FileLen)
{
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
uint32_t var;
uint8_t tmp, KeyState = NOKEY;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
WaveFileStatus = Unvalid_RIFF_ID;
/* TIM6 Configuration */
TIM_DeInit(TIM6);
/* DMA1 channel3 configuration */
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHLCD_REG_8LCD_REG_1_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) & Wavebuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 512;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
/* Enable DMA1 Channel3 */
DMA_Cmd(DMA1_Channel3, ENABLE);
/* DAC deinitialize */
DAC_DeInit();
DAC_StructInit(&DAC_InitStructure);
/* Fill DAC InitStructure */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
/* DAC Channel1: 8bit right---------------------------------------------------*/
/* DAC Channel1 Init */
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_1, ENABLE);
/* Enable DMA for DAC Channel1 */
DAC_DMACmd(DAC_Channel_1, ENABLE);
/* Read the Speech wave file status */
WaveFileStatus = WavePlayer_WaveParsing(DirName, FileName, &wavelen);
/* TIM6 TRGO selection */
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
TIM_SetAutoreload(TIM6, TIM6ARRValue);
if (WaveFileStatus == Valid_WAVE_File) /* the .WAV file is valid */
{
/* Set WaveDataLenght to the Speech wave length */
WaveDataLength = WAVE_Format.DataSize;
}
else
{
return NOKEY;
}
/* Start TIM6 */
TIM_Cmd(TIM6, ENABLE);
LCD_SetTextColor(LCD_COLOR_MAGENTA);
/* Set the Back Color */
LCD_SetBackColor(LCD_COLOR_BLUE);
LCD_DrawRect(LCD_LINE_7, 310, 16, 300);
while (WaveDataLength)
{
DFS_ReadFile(&fiwave, sector, Wavebuffer2, &var, SECTOR_SIZE);
if (WaveDataLength) WaveDataLength -= 512;
if (WaveDataLength < 512) WaveDataLength = 0;
while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET)
{
}
DMA1->IFCR = DMA1_FLAG_TC3;
DMA1_Channel3->CCR = 0x0;
DMA1_Channel3->CNDTR = 0x200;
DMA1_Channel3->CPAR = 0x40007410;
DMA1_Channel3->CMAR = (uint32_t) & Wavebuffer2;
DMA1_Channel3->CCR = 0x2091;
DFS_ReadFile(&fiwave, sector, Wavebuffer, &var, SECTOR_SIZE);
if (WaveDataLength) WaveDataLength -= 512;
if (WaveDataLength < 512) WaveDataLength = 0;
while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET)
{
}
DMA1->IFCR = DMA1_FLAG_TC3;
DMA1_Channel3->CCR = 0x0;
DMA1_Channel3->CNDTR = 0x200;
DMA1_Channel3->CPAR = 0x40007410;
DMA1_Channel3->CMAR = (uint32_t) & Wavebuffer;
DMA1_Channel3->CCR = 0x2091;
}
DMA1_Channel3->CCR = 0x0;
/* Disable TIM6 */
TIM_Cmd(TIM6, DISABLE);
WaveDataLength = 0;
return NOKEY;
}
|
———————————————— 版权声明:本文为CSDN博主「sudaroot」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/sudaroot/article/details/106337717