完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
GD32F405VG 调试过程关于DAC/DMA/TIMER等问题的解决
GD32F405与STM32F405外设完全兼容,外设的地址映射也是完全兼容。ST的标准库中的名字与GD32的外设名字不一致,比如STM32中的TIMER1的地址映射,对应的是GD32的TIMER0,STM32的DAC1对应GD32的DAC0,等等诸如此类。 STM32F405的程序直接烧写到GD32F405中,程序能够运行但部分功能异常,在STM32F405中使用TIMER6触发DAC1转换,使用DAC1的数字量通过DMA传输,程序一切正常,但是GD32运行此程序,DAC无波形产生,DAC的DMA全传输完成中断无法进入,最后发现问题所在: STM32中配置的TIMER6对应GD32的TIMER5(暂时还是称作TIMER6),但是TIMER6始终无法触发DAC产生波形,开启TIMER6的更新中断,DEBUG模式,程序可以进入TIMER6的更新中断,DAC依然没有模拟量输出,最后抱着试一试的心态使用TIMER5触发DAC,DAC波形出现,但是DMA依然无法正常运行,刚开始认为是硬件配置有问题,最后发现是初始化顺序有问题,DAC_OUT_INIT();后面增添了一段是DAC缓慢输出到所需电压的一段代码,没有此代码时,DMA运行正常,加上此代码段后,DMA不运行,原来是DAC外设初始化后紧接着初始化了DAC的DMA功能,最后使能了DAC输出以及DAC_DMACmd(DAC_Channel_1, ENABLE)就是这一句DAC_DMACmd(DAC_Channel_1, ENABLE)使能早了,如果没有Init_Laser()这一句,使能后没有问题,有了Init_Laser()这一句,DAC的DMA功能使能了但是DMA相应的通道没有使能,使程序异常。 运行不正常的代码如下: //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();//********************************初始化激光器,使电流缓慢上升 DMA_Cmd(DMA1_Stream5, ENABLE);//***********DMA通道使能,输出扫描信号波形: 当更换了TIMER以后,DAC工作正常,DMA工作不正常,最后根据实验现象发现是初始化顺序出现了问题,将AC_DMACmd(DAC_Channel_1, ENABLE); 从DAC_OUT_Init();中拿出来,放在Init_Laser();之后,程序运行正常,也就是,DAC初始化完之后没有立马使用DMA传输的话,不要使能DMA传输功能,放在最后使能。 能正常运行的代码如下: //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通道使能,输出扫描信号波形 |
|
|
|
只有小组成员才能发言,加入小组>>
3294 浏览 9 评论
2970 浏览 16 评论
3473 浏览 1 评论
9023 浏览 16 评论
4061 浏览 18 评论
1140浏览 3评论
589浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
579浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2313浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1876浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-4 02:52 , Processed in 1.151766 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号