关于纯硬件实现可变频率可变占空比的方法,使用DMA对TIM1_DMAR寄存器操作,进行对TIM1_ARR、TIM1_RCR、TIM1_CCR1三个寄存器进行赋值,达到一个输出可变频率可变占空比波形的目的,
但是测试发现DMA只能设置为DMA_Mode_Normal模式,只要DMA_Mode_Circular模式则TIM1_ARR、TIM1_RCR、TIM1_CCR1三个寄存器的值在进行一段输出后就乱掉了(比如对应赋给CCR1的值,结果给了ARR)。
但如果用DMA_Mode_Normal模式又没有办法进行连续的波形输出,所以请帮忙看下该如何进行设置以达到可变频率可变占空比输出的目的。谢谢!
另外,设置为DMA_Mode_Normal模式后,在传输完成后关DMA,再设置DMA计数器值,再开DMA,结果还是会乱掉,
怎么操作都是只有第一次DMA循环是正确的。不知道是不是bug
下面是初始化设置,大家帮看看是否有问题
DMA_InitTypeDef DMA_InitStructure;
//TIM1 DMA
DMA_DeInit(TIM1_DMA_Channel);
DMA_InitStructure.DMA_PeripheralBaseAddr = TIM1_DMAR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)data_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 6*3;
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_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(TIM1_DMA_Channel, &DMA_InitStructure);
DMA_Cmd(TIM1_DMA_Channel, ENABLE);
/* Enable DMA1 Channel6 Transfer Complete interrupt */
//DMA_ITConfig(TIM1_DMA_Channel, DMA_IT_TC, ENABLE);
//TIM1
TIM_TimeBaseStructure.TIM_Period = 4608-1;
TIM_TimeBaseStructure.TIM_Prescaler = 0; //72M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 5-1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
//TIM_ITConfig(OSD_TIMER, TIM_IT_Update, ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE);//DISABLE
TIM_Cmd(TIM1, ENABLE);
/* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCMode_Active
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period*0.073;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//TIM_ITConfig(OSD_TIMER, TIM1_CC_IRQn, ENABLE);//
/* TIM1 DMAR Base register and DMA Burst Length Config */
TIM_DMAConfig(TIM1,TIM_DMABase_ARR , TIM_DMABurstLength_3Transfers);//
/* TIM1 Update DMA Request enable */
TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
|