STM32F103 DMA DAC 播放W25Q中的WAV音频代码
时间: 2024-04-24 21:24:38 浏览: 106
SPI.rar_STM32F103_spi_w25q128
以下是使用STM32F103的DMA和DAC模块播放W25Q中WAV音频的示例代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_spi.h"
#define SPI1_DMA_Channel DMA1_Channel3
#define SPI1_DR_Address ((uint32_t)0x4001300C)
#define DAC_DHR12L1_Address ((uint32_t)0x40007408)
void SPI_Configuration(void);
void DMA_Configuration(void);
void DAC_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
uint16_t audio_buffer[1024]; // 音频缓冲区
uint32_t audio_size = 0; // 音频大小
// 初始化SPI、DMA和DAC
SPI_Configuration();
DMA_Configuration();
DAC_Configuration();
// 从W25Q中读取WAV音频数据到缓冲区
// 这里假设你已经实现了从W25Q读取数据的函数,并将数据存储在audio_buffer中
// 将音频大小存储在audio_size变量中
// 启动DMA传输
DMA_Cmd(SPI1_DMA_Channel, ENABLE);
// 循环播放音频
while (1)
{
// 等待DMA传输完成
while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET)
{
}
// 将音频数据写入DAC寄存器
DAC_SetChannel1Data(DAC_Align_12b_L, audio_buffer, 1024);
// 清除DMA传输完成标志位
DMA_ClearFlag(DMA1_FLAG_TC3);
}
}
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
// 打开SPI1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
// SPI1配置
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
// 打开SPI1
SPI_Cmd(SPI1, ENABLE);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
// 打开DMA1时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// DMA1通道3配置(SPI1发送)
DMA_DeInit(SPI1_DMA_Channel);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)audio_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 1024; // 数据大小为1024个16位数
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(SPI1_DMA_Channel, &DMA_InitStructure);
// 打开DMA1通道3中断
DMA_ITConfig(SPI1_DMA_Channel, DMA_IT_TC, ENABLE);
// 打开DMA1通道3
DMA_Cmd(SPI1_DMA_Channel, ENABLE);
}
void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 打开GPIOA和DAC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_DAC, ENABLE);
// 配置GPIOA.4为模拟输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 打开DAC
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
// 打开DAC通道1
DAC_Cmd(DAC_Channel_1, ENABLE);
}
void Delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--)
{
}
}
```
请注意,此代码仅提供基本框架和配置,你需要根据你的具体硬件和需求进行适当的修改和调整。此外,你还需要实现从W25Q读取数据的函数,并将数据存储在`audio_buffer`中。这段代码假设音频数据为16位,采样率为44.1kHz。
希望对你有所帮助!如有任何疑问,请随时提问。
阅读全文