参考网上ST的例子,在注释掉TIM_TrgoInit里的TIM2或者TIM4,只使用DAC1在PA4上能得到正确的56.25K或者只使用DAC2在PA5上也能得到正确的102.27K正弦波出来,但是只要将同时使用TIM2和TIM4让DAC1和DAC2同时输出正弦波,那么PA4还是正确的56.25K波,而PA5就只有80K多点跳动范围大,而且也不是正确的102.27K,求问帮忙指点指点下面是哪里不正确搞成这样的错误结果?
另外还有个问题是输出电压峰峰值不是3.3V,官方例子出来的电压是约1.6V,输出频率不一样电压峰峰值也会不一样,我这频率大概是2.7V,这又是为什么?按说打点是0~4095那么输出电压基本应该就是基准上的0~3.3V这样,实际上怎么差那么远还随频率的变化电压还不同,奇了怪了。
单片机源程序如下:
- #include "n32g45x.h"
- /** @addtogroup DAC_DualModeDMA_SineWave
- * @{
- */
- //#define DAC_DHR12RD_Address 0x40007420
- #define DAC_DR12CH1_Address 0x40007408
- #define DAC_DR12CH2_Address 0x40007414
- /* Init Structure definition */
- DAC_InitType DAC_InitStructure;
- uint32_t Idx = 0;
- const uint16_t Sine12bit[32] = {2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056, 3939,
- 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, 599, 344,
- 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
- uint32_t DualSine12bit[32];
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void TIM_TrgoInit(void);
- void DAC_SineWaveInit(void);
- void DMA_Config(void);
- /**
- * @brief Main program.
- */
- int main(void)
- {
-
- GPIO_Configuration();
- TIM_TrgoInit();
- DAC_SineWaveInit();
- /* Fill Sine32bit table */
- // for (Idx = 0; Idx < 32; Idx++)
- // {
- // DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
- // }
- /* DMA Config */
- DMA_Config();
- TIM_Enable(TIM2, ENABLE);
- TIM_Enable(TIM4, ENABLE);
- while (1)
- {
- }
- }
- /**
- * @brief Configures the different system clocks.
- */
- void RCC_Configuration(void)
- {
- /* DAC Periph clock enable */
- RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
- }
- /**
- * @brief Configures the different GPIO ports.
- */
- void GPIO_Configuration(void)
- {
- GPIO_InitType GPIO_InitStructure;
- /* GPIOA Periph clock enable */
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
- GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
- }
- /**
- * @brief TIM4 Init.
- */
- void TIM_TrgoInit(void)
- {
- TIM_TimeBaseInitType TIM_TimeBaseStructure;
- /* TIM4 Periph clock enable */
- RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2 , ENABLE); // RCC_APB2_PERIPH_TIM8
- RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4 , ENABLE);
- TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.Period =39;
- TIM_TimeBaseStructure.Prescaler = 0x0;
- TIM_TimeBaseStructure.ClkDiv = 0x0;
- TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
- TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
-
- TIM_TimeBaseStructure.Period =21;
- TIM_TimeBaseStructure.Prescaler = 0x0;
- TIM_TimeBaseStructure.ClkDiv = 0x0;
- TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
- TIM_InitTimeBase(TIM4, &TIM_TimeBaseStructure);
- TIM_SelectOutputTrig(TIM2, TIM_TRGO_SRC_UPDATE);
- TIM_SelectOutputTrig(TIM4, TIM_TRGO_SRC_UPDATE);
- }
- /**
- * @brief DAC SineWave Config.
- */
- void DAC_SineWaveInit(void)
- {
- /* DAC Periph clock enable */
- RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
- DAC_InitStructure.Trigger = DAC_TRG_T2_TRGO;
- DAC_InitStructure.WaveGen = DAC_WAVEGEN_NONE;
- DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_DISABLE;
- DAC_Init(DAC_CHANNEL_1, &DAC_InitStructure);
-
- DAC_InitStructure.Trigger = DAC_TRG_T4_TRGO;
- DAC_InitStructure.WaveGen = DAC_WAVEGEN_NONE;
- DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_DISABLE;
- DAC_Init(DAC_CHANNEL_2, &DAC_InitStructure);
- DAC_Enable(DAC_CHANNEL_1, ENABLE);
- DAC_Enable(DAC_CHANNEL_2, ENABLE);
- DAC_DmaEnable(DAC_CHANNEL_1, ENABLE);
- DAC_DmaEnable(DAC_CHANNEL_2, ENABLE);
-
- }
- /**
- * @brief DMA Config.
- */
- void DMA_Config(void)
- {
- DMA_InitType DMA_InitStructure;
- /* DMA2 clock enable */
- RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA2, ENABLE);
- // DMA_DeInit(DMA2_CH3);
- DMA_InitStructure.PeriphAddr = DAC_DR12CH1_Address;
- DMA_InitStructure.MemAddr = (uint32_t)&Sine12bit;
- DMA_InitStructure.Direction = DMA_DIR_PERIPH_DST;
- DMA_InitStructure.BufSize = 32;
- DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
- DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
- DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
- DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord ;
- DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
- DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
- DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
- DMA_Init(DMA2_CH3, &DMA_InitStructure);
- DMA_EnableChannel(DMA2_CH3, ENABLE);
- // DMA_DeInit(DMA2_CH4);
- DMA_InitStructure.PeriphAddr =DAC_DR12CH2_Address;
- DMA_InitStructure.MemAddr = (uint32_t)&Sine12bit;
- DMA_InitStructure.Direction = DMA_DIR_PERIPH_DST;
- DMA_InitStructure.BufSize = 32;
- DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
- DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
- DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
- DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord ;
- DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
- DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
- DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
- DMA_Init(DMA2_CH4, &DMA_InitStructure);
- DMA_EnableChannel(DMA2_CH4, ENABLE);
-
- }
复制代码
|