我想实现角度改变,波形上整体后移。
后面一幅图是我的代码的波形,请问要改什么参数变成第一幅图
void TIM1_PWM_Inti(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); //时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //使能GPIO和服用功能时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
//pwm输出配置,TIM_OCInitStructure共七项配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //设置为pwm2输出模式
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //设置输出极性//输出极性和互补极性的有效电平为低
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=500;
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //设置通道1
//设置通道2捕获比较寄存器的脉冲值-占空比为50%
TIM_OCInitStructure.TIM_Pulse=500;
TIM_OC2Init(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=0x00; //死区时间设置
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);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_Cmd(TIM1,ENABLE);
//下面这句是高级定时器才有的,输出pwm必须打开?
TIM_CtrlPWMOutputs(TIM1,ENABLE); //pwm输出使能,一定要记得打开
}
一周热门 更多>