本帖最后由 洗个菠萝 于 2024-5-16 14:48 编辑
void dac2_dma_config(void)
{
// 使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
// 配置PA4为模拟输出
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// DAC Channel 1 配置
DAC_InitTypeDef DAC_InitStructure;
DAC_DeInit(); // 重置DAC
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; // 软件触发
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DMA_DeInit(DMA1_Stream5); // 先清除之前可能存在的配置
while (DMA_GetCmdStatus(DMA1_Stream5) != DISABLE)
{
} // 等待DMA1_Stream5可配置
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_FEIF5 | DMA_IT_DMEIF5 | DMA_IT_TEIF5 | DMA_IT_HTIF5 | DMA_IT_TCIF5); // 清空DMA1_Stream6上所有中断标志
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_Channel = DMA_Channel_7; // 对应于DAC的DMA通道
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(DAC->DHR12R1)); // DAC通道1的12位右对齐数据保持寄存器地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&dma_buffer[0]; // 数据缓冲区地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // 存储器到外设模式
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE; // 数据传输
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 外设数据长度:16字节
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 存储器数据长度:16字节
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 使用循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; // 高优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; // 不使用FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; // 外设突发单次传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; // 存储器突发单次传输
DMA_Init(DMA1_Stream5, &DMA_InitStructure); // 初始化DMA Stream
// 使能DMA中断
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
// 配置NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 使能DAC和DMA
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
void DMA1_Stream5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5) == SET) // DMA1_Stream5,传输完成标志
{
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
usart_printf("**********DMA1_Stream5_IRQHandler********\r\n");
}
}
使用DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);软件触发 |