定时器1输出互补PWM,不管怎么改配置,波形除了占空比以外都没变化

2019-07-21 06:14发布

用定时器1的3路通道,输出互补PWM。
按我的理解,互补就是CH1输出高的时候,CH1N输出低,是这样嘛?
同时我还插入了死区,在死区切换过程中,CH1和CH1N都为高。
但是不管怎么改PWM的配置,波形都没变过



[mw_shl_code=c,true]#include <WM.h> //主函数初始化中,需要做delay_init(72);和TIM1_PWM_Init(3600-1,0); //预分频数为0 PWM频率=72000/(3600-1+1)=20Khz //这几个占空比更改参数,需要配合TIM1_PWM_Init中的arr使用 u16 CCR1_Val=1800; //设置TIM1通道1输出占空比50% u16 CCR2_Val=900; //设置TIM1通道1输出占空比25% u16 CCR3_Val=450; //设置TIM1通道1输出占空比12.5% void TIM1_PWM_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //开启时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器3时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //使能GPIO和服用功能时钟 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //引脚重映射要开这个时钟 //初始化GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; //PWM输出在PA8,9,10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //PWM输出在PA7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化GPIO //初始化定时器 TIM1 TIM_TimeBaseStructure.TIM_Period = arr; //设置在自动重装载周期值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式 // TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复寄存器,用于自动更新pwm占空比 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //pwm输出配置,TIM_OCInitStructure共七项配置 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //设置为pwm2输出模式 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性//输出极性和互补极性的有效电平为低 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出//比较输出使能 //下面几个参数(除了pulse)是高级定时器才会用到,通用定时器不用配置 TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High; //设置互补端输出极性//输出极性和互补极性的有效电平为低 TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable; //使能互补端输出 TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //死区后输出状态 TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset; //死区后互补端输出状态 //设置通道1捕获比较寄存器的脉冲值-占空比为50% TIM_OCInitStructure.TIM_Pulse=CCR1_Val; TIM_OC1Init(TIM1,&TIM_OCInitStructure); //设置通道1 //设置通道2捕获比较寄存器的脉冲值-占空比为25% TIM_OCInitStructure.TIM_Pulse=CCR2_Val; TIM_OC2Init(TIM1,&TIM_OCInitStructure); //设置通道3捕获比较寄存器的脉冲值-占空比为12.5% TIM_OCInitStructure.TIM_Pulse=CCR3_Val; TIM_OC3Init(TIM1,&TIM_OCInitStructure); //死区和刹车功能配置,高级定时器才有的,通用定时器不用配置TIM_BDTRInitStructure,共七项配置 TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Disable; //运行模式下输出选择//运行模式下“关闭状态”使能 TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Disable; //空闲模式下输出选择//关闭模式下“关闭状态”使能 TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF; //锁定设置 //锁定关闭 TIM_BDTRInitStructure.TIM_DeadTime=0x90; //死区时间设置 TIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable; //刹车功能使能 TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High; //刹车输入极性 //刹车输入高电平有效 TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable; //自动输出使能 TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //使能端的打开 TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器? TIM_ARRPreloadConfig(TIM1,ENABLE); //使能TIMx在ARR上的预装载寄存器? TIM_Cmd(TIM1,ENABLE); //打开TIM2? //下面这句是高级定时器才有的,输出pwm必须打开? TIM_CtrlPWMOutputs(TIM1,ENABLE); //pwm输出使能,一定要记得打开 } [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。