我最近用STM32F103VCT6 的DAC产生两路同频正弦波,通过定时器1延时控制两个波形的相位。但是不知道为什么调节的时候,相位间隔并不是以定时器的分辨率步进,要调节好几个值相位才能动一下,而且相位变动值都不固定。 程序是每次步进一下,重新设置DMA 和DAC ,然后先打开第一路DAC的时钟TIMx1,然后打开定时器,定时中断到就打开第二路的时钟TIMx2,不知道是不是我的程序问题,还是STM32本身启动DMA DAC时间就不确定。 可以确定定时器是没问题,我用IO口翻转做个实验,可以实现很精确的步进。 DMA 和DAC 配置程序如下 NVIC_SETFAULTMASK(); //关闭总中断 SysTick_ITConfig(DISABLE); TIM_Cmd(TIM2, DISABLE); /* TIM2 enable counter */ TIM_Cmd(TIM4, DISABLE); /* TIM4 enable counter */ DMA_Cmd(DMA2_Channel3, DISABLE); /* Enable DMA1 Channel3 */ DAC_Cmd(DAC_Channel_1, DISABLE); /* Enable DAC Channel1 */ DAC_DMACmd(DAC_Channel_1,DISABLE); /* Enable DMA for DAC Channel1 */ DMA_Cmd(DMA2_Channel4, DISABLE); /* Enable DMA2 Channel3 */ DAC_Cmd(DAC_Channel_2, DISABLE); /* Enable DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, DISABLE); /* Enable DMA for DAC Channel2 */ TIM_DeInit(TIM2); /* Resets the TIM2 */ TIM_DeInit(TIM4); /* Resets the TIM4 */ TIM_PrescalerConfig(TIM2, 0x00, TIM_PSCReloadMode_Update); /* TIM2 Configuration */ TIM_SetAutoreload(TIM2, T); //设置定时器2自动重装 TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2 TRGO selection */ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DMA2 channel3 configuration */ DMA_DeInit(DMA2_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&Escalator8bit1; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 256; 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel3, &DMA_InitStructure); DMA_Cmd(DMA2_Channel3, ENABLE); /* Enable DMA1 Channel3 */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ //TIM_Cmd(TIM2, ENABLE); /* TIM2 enable counter */ TIM_PrescalerConfig(TIM4, 0x00, TIM_PSCReloadMode_Update); /* TIM4 Configuration */ TIM_SetAutoreload(TIM4, T); //设置定时器4自动重装 TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Update); /* TIM4 TRGO selection */ /* DAC channel2 Configuration*/ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* DMA2 channel4 configuration */ DMA_DeInit(DMA2_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R2_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&Escalator8bit2; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 256; 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_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel4, &DMA_InitStructure); DMA_Cmd(DMA2_Channel4, ENABLE); /* Enable DMA2 Channel3 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Enable DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* Enable DMA for DAC Channel2 */ TIM_Cmd(TIM2, ENABLE); /* TIM2 enable counter */ // 此次打开第一路DAC NVIC_RESETFAULTMASK();//开放总中断 Time1_Configuration(); //配置定时器1,用来延时产生第二路DAC,在定时器1中断中打开第二路DAC 定时器1中断中启动第二路DAC的程序如下 void TIM1_UP_IRQHandler(void) { TIM_ClearITPendingBit(TIM1, TIM_IT_Update); TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE ); TIM_Cmd(TIM1, DISABLE); TIM_Cmd(TIM4, ENABLE); /* TIM4 enable counter */ //此处 打开第二路DAC SysTick_ITConfig(ENABLE); } |