调试定时器1的通道1输出PWM波形,发现断电重启时偶尔无PWM输出,而且烧写程序自动运行时,占空比控制也是反的。
搜索网上的经验,发现是我的初始化有问题,TIM1是高级定时器,功能多样,初始时没有正确初始,就会出现莫名其妙的问题
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2; //脉冲宽度调制模式1
//输出极性
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set; //在空闲时输出
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //PWM输出使能(正向通道)
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable; //PWM输出失能(互补通道)
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High; //必须
TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCIdleState_Reset; //必须
TIM_OCInitStruct.TIM_Pulse = 0; //捕获比较的脉冲值
TIM_OC1Init(TIM1,&TIM_OCInitStruct); |