STM32 TIM8 PWM输出
扫描二维码
随时随地手机看文章
STM32 TIM8初始化的配置与通用定时器的配置不同,我最开始时按照通用定时器配置,无论是配置一个通道,还是配置所有通道,死活不出来pwm。接着查资料说要添加这一句代码:TIM_CtrlPWMOutputs(TIM8,ENABLE),添加后只有通道4有输出,其它三路没有输出,但是还是有点小激动啊。后来看到一个网友说要添加下面几行代码:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
添加后果然四路PWM均正常输出。我测试后其实只要添加:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;就可正常输出pwm,感谢万能度娘。
我还发现输出的pwm波形与TIM_OCIdleState设置的状态成和TIM_OCPolarity设置的极性相关,例如:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; 时波形的占空比若为25%;那么:TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High ; 时波形的占空比为75%;
TIM8还有很多没有搞明白的地方,需要继续学习哥只是个传说。最后附上TIM8的初始化代码(注释忽略):
void TIM8_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIO
//初始化TIM8
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM8 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM8 OC2
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_SetCompare1(TIM8,474);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM8 OC2
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_SetCompare2(TIM8,700);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //输出极性:TIM输出比较极性高
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM8 OC2
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_SetCompare3(TIM8,900);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low ; //输出极性:TIM输出比较极性高
TIM_OC4Init(TIM8, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM8 OC2
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_SetCompare4(TIM8,1200);
TIM_Cmd(TIM8, ENABLE); //不使能TIM8
TIM_CtrlPWMOutputs(TIM8,ENABLE);
}