问题描述:我用stm32f207的PA4(DAC通道1)、PA5(DAC通道2)输出正玄波和余弦波,利用了DMA和TIM2和TIM6分别做触发,TIM2触发DMA1_Stream5,TIM6触发DMA1_Stream6,PA4输出了正弦波,PA5输出了余弦波, TIM2->ARR = 20; TIM6->ARR = 20;输出频率相同,看不出问题,可是问题出在哪里呢?问题在于,当ARR的值小于15的时候,比如 TIM2->ARR = 14; TIM6->ARR = 14; 问题来了,通道1一直是高电平,通道输出余弦波正常。 于是改变用TIM2触发DMA1_Stream6,TIM6触发DMA1_Stream5,ARR都为14(低于15都一样); 同样还是通道一有同样的问题,二通道2却正常输出了波形。于是我想跟TIM没有关系,后来也尝试改变波形数据,发现也不是波形数据的问题。不知道是否有大神帮我分析一下这个问题是怎么回事。我用的是keil工具。数据是用EXCEl计算出来的,应该是没有问题的。代码如下: #include "stm32f2xx_dac.h" #include "stm32f2xx_dma.h" #include "stm32f2xx.h" #define DAC1_DMA_ADDR 0x40007408 // DAC通道1地址 #define DAC2_DMA_ADDR 0x40007414 // DAC通道2地址 uc32 DAC_sinTAB[]={ 2000 ,2098 ,2196 ,2293 ,2390 ,2486 ,2581 ,2674 , 2765 ,2855 ,2943 ,3028 ,3111 ,3191 ,3269 ,3343 , 3414 ,3482 ,3546 ,3606 ,3663 ,3715 ,3764 ,3808 , 3848 ,3883 ,3914 ,3940 ,3962 ,3978 ,3990 ,3998 , 4000 ,3998 ,3990 ,3978 ,3962 ,3940 ,3914 ,3883 , 3848 ,3808 ,3764 ,3715 ,3663 ,3606 ,3546 ,3482 , 3414 ,3343 ,3269 ,3191 ,3111 ,3028 ,2943 ,2855 , 2765 ,2674 ,2581 ,2486 ,2390 ,2293 ,2196 ,2098 , 2000 ,1902 ,1804 ,1707 ,1610 ,1514 ,1419 ,1326 , 1235 ,1145 ,1057 ,972 ,889 ,809 ,731 ,657 , 586 ,518 ,454 ,394 ,337 ,285 ,236 ,192 , 152 ,117 , 86 , 60 , 38 , 22 , 10 , 2 , 0 , 2 , 10 , 22 , 38 , 60 , 86 ,117 , 152 ,192 ,236 ,285 ,337 ,394 ,454 ,518 , 586 ,657 ,731 ,809 ,889 ,972 ,1057 ,1145 , 1235 ,1326 ,1419 ,1514 ,1610 ,1707 ,1804 ,1902 }; uc32 DAC_cosTAB[] = { 4000 ,3998 ,3990 ,3978 ,3962 ,3940 ,3914 ,3883 , 3848 ,3808 ,3764 ,3715 ,3663 ,3606 ,3546 ,3482 , 3414 ,3343 ,3269 ,3191 ,3111 ,3028 ,2943 ,2855 , 2765 ,2674 ,2581 ,2486 ,2390 ,2293 ,2196 ,2098 , 2000 ,1902 ,1804 ,1707 ,1610 ,1514 ,1419 ,1326 , 1235 ,1145 ,1057 ,972 ,889 , 809 ,731 ,657 , 586 ,518 ,454 ,394 ,337 ,285 , 236 ,192 , 152 ,117 , 86 , 60 , 38 , 22 , 10 ,2 , 0 , 2 , 10 , 22 , 38 , 60 , 86 ,117 , 152 ,192 ,236 ,285 ,337 ,394 ,454 ,518 , 586 ,657 ,731 ,809 ,889 ,972 ,1057 ,1145 , 1235 ,1326 ,1419 ,1514 ,1610 ,1707 ,1804 ,1902 , 2000 ,2098 ,2196 ,2293 ,2390 ,2486 ,2581 ,2674 , 2765 ,2855 ,2943 ,3028 ,3111 ,3191 ,3269 ,3343 , 3414 ,3482 ,3546 ,3606 ,3663 ,3715 ,3764 ,3808 , 3848 ,3883 ,3914 ,3940 ,3962 ,3978 ,3990 ,3998 }; void TIM_Int_Init(u16 arr,u16 psc) { RCC->APB1ENR|=1<<0; //TIM2时钟使能 TIM2->ARR=arr; TIM2->PSC=psc; TIM2->CR1|=1<<7; TIM2->CR2 |=0x2<<4; //TIMER更新作为TRGO源 TIM2->CR1 |=TIM_CR1_CEN; TIM6->ARR=arr; //TIM6的时钟在前面系统初始化的时候已经开启了,这里不在重复开启,不影响问题的本质 TIM6->PSC=psc; TIM6->CR1|=1<<7; TIM6->CR2 |=0x2<<4; TIM6->CR1 |=TIM_CR1_CEN; } void DAC_DMA_init(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Stream5); DMA_InitStructure.DMA_Channel = DMA_Channel_7; DMA_InitStructure.DMA_PeripheralBaseAddr = DAC1_DMA_ADDR; DMA_InitStructure.DMA_Memory0BaseAddr = (u32)DAC_cosTAB;// DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize =128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;// DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Word;// DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// ; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_1QuarterFull; DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStructure); DMA_Cmd(DMA1_Stream5, ENABLE); /////////////////////////////////////////////////////////////////////////////////////////////////////// DMA_DeInit(DMA1_Stream6); DMA_InitStructure.DMA_Channel = DMA_Channel_7; DMA_InitStructure.DMA_PeripheralBaseAddr = DAC2_DMA_ADDR; DMA_InitStructure.DMA_Memory0BaseAddr = (u32)DAC_sinTAB;// DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize =128; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;// DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Word;// DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// ; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_1QuarterFull; DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream6, &DMA_InitStructure); DMA_Cmd(DMA1_Stream6, ENABLE); } void Dac1_Init(void) { DAC_InitTypeDef DAC_InitStructure; RCC->APB1ENR|=1<<29; // DAC时钟开启 DAC_DeInit(); DAC_StructInit(&DAC_InitStructure); DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;// DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; // DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;// DAC_Init(DAC_Channel_1, &DAC_InitStructure); // DAC_Cmd(DAC_Channel_1, ENABLE); // DAC_DMACmd(DAC_Channel_1, ENABLE); // DAC_StructInit(&DAC_InitStructure); DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;// DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; // DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;//DAC DAC_Init(DAC_Channel_2, &DAC_InitStructure); // DAC_Cmd(DAC_Channel_2, ENABLE); // DAC_DMACmd(DAC_Channel_2, ENABLE); // DAC_DMA_init(); TIM_Int_Init(100,0); } void DDS_Output_Wave( u16 frq ) { TIM2->ARR = frq; TIM6->ARR = frq; } 说明:在初始化的时候,只要调用Dac1_Init()就OK了,想要改变波形频率,只要调用 DDS_Output_Wave( u16 frq )函数,改变frq的值就OK 了。 问题再重申一下,就是当frq的值低于15的时候,就出问题了,通道一只有高电平,没有波形输出,但是通道2正常。请大神指点帮助。谢谢 |
一个周期128个点是不能少的,现在是想提高频率,或者说是想看看DAC+DMA+TIMER最高能输出多少频率,发现提高频率到比较高的时候,通道貌似是不稳定,有时候可以输出,有时候又不行。会不会是芯片本身的问题?比如当ARR=15的的时候,发现还是临界,低于15就可能出问题,但也不一定。这是什么情况,有时候有波形输出,有时候又没有波形输出,只有高电平了,不知道什么问题
也就是说,这可能是芯片本身的问题咯,不是程序的问题了?我现在也怀疑是这个问题呢
评分
查看全部评分