请教各位:
想输出固定占空比频率可调的PWM波
我是这么做的,设了两个变量ARR_Val和CCR4_Val分别给ARR 和CCR4
占空比为50%,我想频率实时变化,ARR_Val就实时变化,CCR4_Val设为ARR_Val的一半
在主循环中,实时初始化
TIM_BaseInitStructure.TIM_Period = ARR_Val;
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
这样是可行,但初始化函数TIM_TimeBaseInit()和TIM_OC4Init()在主循环中一直运行,总感觉不妥,各位大侠有什么好的办法吗?
TIM配置如下:
/* Time base configuration */
TIM_BaseInitStructure.TIM_Period = ARR_Val;
TIM_BaseInitStructure.TIM_Prescaler = 11;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
/* TIM3 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure); |