之前使用的是stm32,单位采购替换了贵公司的CH32F103R8T6,使用HAL库配置DAC_CH2和DMA circle模式,如何正确修改?
原来的程序如下:
主函数中语句
HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_2,DAC1_Value,1,DAC_ALIGN_12B_R);
dac.c中语句
void MX_DAC_Init(void)
{
DAC_ChannelConfTypeDef sConfig = {0};
hdac.Instance = DAC;
if (HAL_DAC_Init(&hdac) != HAL_OK)
{
Error_Handler();
}
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
}
void HAL_DAC_MspInit(DAC_HandleTypeDef* dacHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(dacHandle->Instance==DAC)
{
__HAL_RCC_DAC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = DAC_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(DAC_GPIO_Port, &GPIO_InitStruct);
hdma_dac_ch2.Instance = DMA2_Channel4;
hdma_dac_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_dac_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_dac_ch2.Init.MemInc = DMA_MINC_ENABLE;
hdma_dac_ch2.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_dac_ch2.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_dac_ch2.Init.Mode = DMA_CIRCULAR;
hdma_dac_ch2.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_dac_ch2) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(dacHandle,DMA_Handle2,hdma_dac_ch2);
}
}
尝试将DMA2_Channel4修改为DMA1_Channel4,无输出;不使用DMA可以正常输出