我TIM1配置成一个100us的定时功能
TIM1的3对PWM分别配置成100us的周期,不同占空比的输出
对于互补功能有点不理解,个人认为,只要3对PWM输出口配置PWM输出,那么CHx 和CHxN之间就应该是PWM功能,通过使能或者失能CHx和CHx通道来控制周期内的通断(通则是PWM,断则是低电平)
仿真测试下来好像不是这个样子
如果我同时使能CHx和CHxN,则输出互补的PWM,这个是正常的
如果我失能CHx 但是 使能CHxN,则CHx是低电平(这个符合期望值),但是CHxN此时却是高电平(想配置成PWM的输出的,结果却不对)
拿CH1 和 CH1N来说
互补是CH1 和 CH1N的实际输出互补(CH1N一直和CH1的输出口电平相反)
还是配置意义上的互补(CH1 PWM输出,CH1N 是互补的PWM输出)
上面这个图,初始化部分,是配置的PWM输出,使能了所有的通道,
运行后,则控制通道来开启和关闭PWM,高电平和PWM部分是我使能通道的波形,结果CHxN的电平好像是根据CHx来的一样,在我开启CHxN后,它则根据此时的CHx(低电平)来输出高电平,不是我配置的PWM了,图形中显示的porta和下面的portb成互补通道。总共3个互补通道。
初始化部分
[mw_shl_code=c,true] TIM_TimeBaseInitTypeDef TIM1_TimeBaseInitStructure; //根据 TIM_TimeBaseInitStruct 中指定的参数初始化 TIMx 的时间基数单位
TIM_OCInitTypeDef TIM1_OCInitStructure; //根据 TIM_OCInitStruct 中指定的参数初始化外设 TIMx
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure; //TIM1_BDTRInitStruct:指向结构 TIM1_BDTRInitTypeDef的指针,包含了TIM1的BDTR寄存器的配置信息
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1); //将外设 TIM1 寄存器重设为缺省值;
TIM1_TimeBaseInitStructure.TIM_Period = 1000-1;//TIM1_Period设置了在下一个更新事件装入活动的自动重装载寄存器周期的值——0xFFFF;
TIM1_TimeBaseInitStructure.TIM_Prescaler = 72-1;//TIM1_Prescaler设置了用来作为 TIM1时钟频率除数的预分频值。它的取值必须在 0x0000 和0xFFFF 之间。
TIM1_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM1_CounterMode 选择了计数器模式——向上计数;
//原书中值为0xFFFF,胡扯嘛,f=TIM1CLK/(TIM1_Period+1),如果TIM1的时钟频率为72MHz,则TIM1_Period应为4096左右,即0x1000。
TIM1_TimeBaseInitStructure.TIM_ClockDivision = 0x0;//TIM1_ClockDivision 设置了时钟分割;
TIM1_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseInitStructure);//根据 TIM1_TIM1BaseInitStruct 中指定的参数初始化 TIM1 的时间基数单位
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM1_OCMode 选择定时器模式
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM1_OutputState选择输出比较状态
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //TIM1_OutputNState选择互补输出比较状态
TIM1_OCInitStructure.TIM_Pulse = 200; //TIM1_Pulse设置了待装入捕获比较寄存器的脉冲值——占空比为50%。
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM1_OCPolarity输出极性高;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; //TIM1互补输出极性为高
//原书中OCP和OCNP均设置为Low,看不出互补特性
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //TIM1_OCIdleState选择空闲状态下的非工作状态(MOE=0时设置TIM1输出比较空闲状态)
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //MOE = 0时重置互补输出的输出比较空闲状态
TIM_OC1Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = 500; //设置通道2输出占空比为25%
TIM_OC2Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = 800; //设置通道3输出占空比为12.5%
TIM_OC3Init(TIM1,&TIM1_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //TIM_OSSRState 设置在运行模式下非工作状态选项
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //TIM_OSSIState 设置在运行模式下非工作状态选项
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //TIM_LOCKLevel 设置了锁电平参数——锁电平1
TIM1_BDTRInitStructure.TIM_DeadTime = 1; //死区时间1.625μs
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //TIM1 刹车输入使能
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1 刹车输入管脚极性
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //TIM1_AutomaticOutput 自动输出使能
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure); //设置刹车特性,死区时间,锁电平,OSSI,OSSR 状态和 AOE(自动输出使能)
TIM_Cmd(TIM1,ENABLE); //TIM1 使能
TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能外设 TIM1 的主输出[/mw_shl_code]
控制子程序部分
[mw_shl_code=c,true]//********************************************************************************
/* set phase to "positive with PWM */
void pwm_set_pwm_hi(u16 phase)
{
TIM_SelectOCxM(TIM1, phase, TIM_OCMode_PWM1);
TIM_CCxCmd(TIM1, phase, TIM_CCx_Enable);
TIM_CCxNCmd(TIM1, phase, TIM_CCxN_Disable);
}
//********************************************************************************
/* set phase to "negative with PWM */
void pwm_set_pwm_lo(u16 phase)
{
TIM_SelectOCxM(TIM1, phase, TIM_OCMode_PWM1);
TIM_CCxCmd(TIM1, phase, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, phase, TIM_CCxN_Enable);
}
//********************************************************************************
/* set phase to "not energized" */
void pwm_set____off(u16 phase)
{
TIM_CCxCmd(TIM1, phase, TIM_CCx_Disable);
TIM_CCxNCmd(TIM1, phase, TIM_CCxN_Disable);
}[/mw_shl_code]
一周热门 更多>