本帖最后由 woobruce 于 2018-9-19 12:03 编辑
问题已解决,是在其他地方的疏忽以及自己观察不仔细造成的,属于愚蠢的错误,惭愧惭愧。。。。
无论如何,谢谢回复的朋友。
//==============================================================
大家好,最近在看网上的教程自学STM32,今天在试验高级定时器的互补输出时,遇到了一个问题。
具体是,输出通道和互补输出通道各接上一个LED来直观的观察效果。
按照例程配置和初始化定时器和通道后,CH1和CH1N上接的LED会根据初始化时设定的TIM_Pulse值,有不同的明暗度,CH1和CH1N的明暗度相反,这应该符合互补输出的特性。
但是,如果在main函数里修改TIM1->CCR1的值,主通道LED的亮度会变化,但接互补通道的LED亮度没有变化。
请问这是为什么?
希望大家指点一下。
初始化的代码:
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
-
- /*--------------------时基结构体初始化-------------------------*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Period=(1000-1);
- TIM_TimeBaseStructure.TIM_Prescaler= (72-1);
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
-
- /*--------------------输出比较结构体初始化-------------------*/
- 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();
- }
复制代码
|
请检查输出模式是否正确。
评分
查看全部评分
评分
查看全部评分
TIM_Cmd(TIM1, DIABLE);
然后再修改配置,改完以后:
TIM_Cmd(TIM1, ENABLE);
评分
查看全部评分