|
本人正在做stm32的课程设计,其中用到输出pwm波的相关知识,但关于CCR寄存器的值对PWM波形的影响有一些不明白的地方,以下先上tim1的初始化代码
TIM_TimeBaseStructure.TIM_Period = 7199;
TIM_TimeBaseStructure.TIM_Prescaler = 0
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
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 = DIY_Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
其中DIY_Pulse的初始值为3599(即初始占空比为1/2).然后我在主函数中还设置了一个中断服务子程序(主要代码如下):
if(EXTI_GetITStatus(EXTI_Line6)!=RESET)
{DIY_Pulse=DIY_Pulse-200;
TIM1->CCR1=DIY_Pulse;
EXTI_ClearITPendingBit(EXTI_Line6);}
可以看到中断发生时会将DIY_Pulse的值减200并更新到CCR中,根据书上的解释,当cnt的值计数到与ccr相同时电平会发生跳变,这里ccr的值减小,占空比应该是减小的,但实际上却是增大,不明所以,想问下大神们这是为什么。
另外就是,关于这句代码:TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low 书上说这里设置为high就可以实现互补输出,但我却要设成low仿真时才能实现互补,也是不明所以。
附上仿真的波形。
|
|