高级定时器和普通定时器使用的区别疑惑?
求大神相助!
上面的TIM1和TIM16用了 TIM_CtrlPWMOutputs(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM16, ENABLE)这两句才可以出来PWM的
高级定时器需要加上这句 TIM_CtrlPWMOutputs(TIM1, ENABLE); 你看看你加了没,如果不加出不来PWM,你网上查下这句的用途,这样深刻一些。
下面的这个函数是我写的,是可以产生PWM的,GPIO你自己配置下,还有我没有使能定时器,因为我在需要的时候才打开定时器,可以餐卡下
static void TIMInit(uint16_t PWM_Frequency, uint16_t Duty)
{
uint16_t PeriodValue;
uint16_t ChannelPulse;
if(PWM_Frequency <1)
PWM_Frequency = 1;
if(PWM_Frequency >100)
PWM_Frequency = 100;
if(Duty > 99)
Duty = 99;
PeriodValue = (uint16_t)((8000000/(((uint16_t)TIMClockPrescaler + 1)*PWM_Frequency)) - 1u);
ChannelPulse = (uint16_t) (((uint32_t)( Duty*100) * (PeriodValue + 1)) / 10000u);
/* Time Base configuration */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = TIMClockPrescaler; //设置预分频,8M/(799+1)=10000HZ (TIM3的时钟为8MHZ)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Period = PeriodValue; //计数器从0开始计数到999,即1000次为一个定时周期(设置PWM周期)
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置滤波分频为不分频
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置PWM为模式1
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_Pulse = ChannelPulse; //当计数器计数到这个值时电平发生跳变(设置空比)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //当计数值小于上面的值时的电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //TIM1通道CH1N
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //TIM2通道1
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能TIM2 在 CCR1 上的预装载寄存器
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //通道1
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能TIM3 在 CCR1 上的预装载寄存器
TIM_OC1Init(TIM16, &TIM_OCInitStructure); //通道1
TIM_OC1PreloadConfig(TIM16,TIM_OCPreload_Enable); //使能TIM3 在 CCR1 上的预装载寄存器
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //TIM1通道CH2N
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //TIM2通道2
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能TIM2 在 CCR2 上的预装载寄存器
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //通道2
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能TIM3 在 CCR2 上的预装载寄存器
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //TIM1通道CH3N
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //TIM2通道3
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能TIM3 在 CCR3 上的预装载寄存器
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //通道3
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能TIM3 在 CCR3 上的预装载寄存器
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //TIM2通道4
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能TIM3 在 CCR4 上的预装载寄存器
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //通道4
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能TIM3 在 CCR4 上的预装载寄存器
TIM_CtrlPWMOutputs(TIM1, ENABLE); //高级定时器需要加上这句
TIM_CtrlPWMOutputs(TIM16, ENABLE);
TIM_ARRPreloadConfig(TIM2,ENABLE); //使能TIM2计数值自动重装
TIM_ARRPreloadConfig(TIM3,ENABLE); //使能TIM3计数值自动重装
TIM_ARRPreloadConfig(TIM16,ENABLE); //使能TIM3计数值自动重装
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );
}