stm32定时器1的2路互补PWM

时间:2021-09-15 20:48:01
void TIM1_PWM_Ini(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; //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; //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_High;
//TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性//输出极性和互补极性的有效电平为低
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出//比较输出使能
//下面几个参数(除了pulse)是高级定时器才会用到,通用定时器不用配置
// TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_Low;
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输出使能,一定要记得打开
}