本帖最后由 brucewoo 于 2018-9-19 12:04 编辑
问题已解决,是在其他地方的疏忽以及自己观察不仔细造成的,属于愚蠢的错误,惭愧惭愧。。。。
\\=================================================================
大家好,最近在看网上的教程自学STM32,今天在试验高级定时器的互补输出时,遇到了一个问题。
具体是,输出通道和互补输出通道各接上一个LED来直观的观察效果。
按照例程配置和初始化定时器和通道后,CH1和CH1N上接的LED会根据初始化时设定的TIM_Pulse值,有不同的明暗度,CH1和CH1N的明暗度相反,这应该符合互补输出的特性。
但是,如果在main函数里修改TIM1->CCR1的值,主通道LED的亮度会变化,但接互补通道的LED亮度没有变化。
请问这是为什么?
希望大家指点一下。
初始化的代码:
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //反向通道有效
TIM_OCInitStructure.TIM_Pulse = 4;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(ADVANCE_TIM, TIM_OCPreload_Enable);
/*-------------------刹车和死区结构体初始化-------------------*/
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 11;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(ADVANCE_TIM, &TIM_BDTRInitStructure);
// 使能计数器
TIM_Cmd(ADVANCE_TIM, ENABLE);
TIM_CtrlPWMOutputs(ADVANCE_TIM, ENABLE);
Main函数中的代码:实验结果是,只有主通道的LED出来呼吸灯效果,互补通道的LED一直不会动
if((Millis()-recMillis)<=(PWMLightCycle/2))
{
TIM1->CCR1=PWMmax*((Millis()-recMillis)/((float)PWMLightCycle/2));
}
else if((Millis()-recMillis)<=(PWMLightCycle))
{
TIM1->CCR1=PWMmax*(1-((Millis()-recMillis-(PWMLightCycle/2))/((float)PWMLightCycle/2)));
}
else
{
recMillis=Millis();
}
|