能正常运行的代码如下:
//DAC初始化配置函数
void DAC_OUT_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*DAC1 is used to generate sweep signal */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
// /*DAC2 is used to set laser temperature*/
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 2000-1; // 1ms*500点
TIM_TimeBaseStructure.TIM_Prescaler = 42-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
/* TIM6 TRGO selection */
TIM_SelectOutputTrigger(TIM5, TIM_TRGOSource_Update);
// /*TIM6 IT_Update */
// TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
/* TIM6 enable counter */
TIM_Cmd(TIM5, ENABLE);
/* DMA1_Stream5 channel7 configuration DAC1 DMA request*/
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DAC_DataTable;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = DAC_DATA_SIZE;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
/* Enable the DMA Stream5 IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA_ITConfig(DMA1_Stream5, DMA_IT_TC|DMA_IT_HT, ENABLE);//传输完成和半传输完成中断
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);//传输完成和半传输完成中断
DAC_Cmd(DAC_Channel_1,ENABLE);
DAC_Cmd(DAC_Channel_2,ENABLE);
// DAC_DMACmd(DAC_Channel_1, ENABLE);
}
//主函数中调用
DAC_OUT_Init();//**************************DAC初始化配置
Init_Laser();//****************************初始化激光器,使电流缓慢上升
DAC_DMACmd(DAC_Channel_1, ENABLE); //2021-6-5从 DAC_OUT_Init() 函数中提取出来,放在此处,程序运行正常
DMA_Cmd(DMA1_Stream5, ENABLE);//***********DMA通道使能,输出扫描信号波形
|