1 背景
最近有接触到通过可控硅的方式来控制交流风机或者电烙铁功率,STM32的定时器输出比较模式,刚好可以满足这种需求,借此机会总结一下定时器的几种输出模式。
2 STM32的定时器比较输出
STM32的定时器比较输出一共有8种,记录一下初始化方法和逻辑分析仪的波形。
在官网搜索对应的型号找到用户手册,比如STM32F103ZET6
找到比较模式相关配置的描述
TIMx capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18 Reset value: 0x0000
The channels can be used in input (capture mode) or in output (compare mode). The
direction of a channel is defined by configuring the corresponding CCxS bits. All the other
bits of this register have a different function in input and in output mode. For a given bit,
OCxx describes its function when the channel is configured in output, ICxx describes it
function when the channel is configured in input. Take care that the same bit can have a
different meaning for the input stage and for the output stage。
2.1 OCxM 输出匹配模式
OC1M用于配置通道1,通道2则在OC2M上
2.1.1 TIM_OCMODE_TIMING
000:Frozen冻结模式
TIMx_CCR1和计数器TIMx_CNT之间的比较对输出没有影响
2.1.2 TIM_OCMODE_ACTIVE
001: Set channel 1 to active level on match。
匹配时将输出为有效电平,当TIMx_CNT=TIMx_CCR1时强制输出为高电平
2.1.3 TIM_OCMODE_INACTIVE
010: Set channel 1 to inactive level on match。
匹配时将输出为无效电平,当TIMx_CNT=TIMx_CCR1时强制输出为高低电平
2.1.4 TIM_OCMODE_TOGGLE
011: 当TIMx_CNT=TIMx_CCR1时电平翻转。
2.1.5 TIM_OCMODE_FORCED_INACTIVE
100: Force inactive level,强制输出为低电平(无效电平)
2.1.6 TIM_OCMODE_FORCED_ACTIVE
101: Force active level,强制输出为高电平(有效电平)
2.1.7 TIM_OCMODE_PWM1
110: PWM mode 1
当TIMx_CNT
2.1.8 TIM_OCMODE_PWM2
111: PWM mode 2
当TIMx_CNT
3 实测波形
在上电时默认会有个100ms的高电平,作为一个直观的起始信号,
预分频设置为(72000000/2000)-1,最大计数为415-1,则周期是(1*415/2000)=(0.207)s
3.1 TIM_OCMODE_TIMING
3.1.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_TIMING;;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.1.2 波形
极性是高电平时,上电后100ms后一直保持低电平
极性是低电平时,一直保持高电平
3.2 TIM_OCMODE_ACTIVE
3.2.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_ACTIVE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.2.2 波形
极性是低电平时,会先输出脉宽为计数周期的高电平,当TIMx_CNT=TIMx_CCR2后输出一直为低电平(有效电平)
极性是高电平时,会先输出脉宽为计数周期的低电平,当TIMx_CNT=TIMx_CCR2后输出一直为高电平(有效电平)
3.3 TIM_OCMODE_INACTIVE
3.3.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_INACTIVE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.3.2 波形
极性是低电平时,当TIMx_CNT=TIMx_CCR2时会出现一个低电平,但持续时间很短,然后一直输出一个高电平(无效电平)
极性是高电平时,一直输出为低电平(无效电平)
3.4 TIM_OCMODE_TOGGLE
3.4.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = arr/2;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.4.2 波形
极性是低电平时,会先输出一个脉宽为半个计数周期的高电平,然后一直不停地翻转出一个脉宽为一个计数周期的电平
极性是高电平时,会先输出一个脉宽为半个计数周期的低电平,然后一直不停地翻转出一个脉宽为一个计数周期的电平
3.5 TIM_OCMODE_PWM1
3.5.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = arr*2/3;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.5.2 波形
极性是低电平时,当TIMx_CNT
极性是高电平时,当TIMx_CNT
3.6 TIM_OCMODE_PWM2
3.6.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_PWM2;
sConfigOC.Pulse = arr*2/3;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.6.2 波形
极性是低电平时,当TIMx_CNT
极性是高电平时,当TIMx_CNT
3.7 TIM_OCMODE_FORCED_ACTIVE
3.7.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_FORCED_ACTIVE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.7.2 波形
极性是低电平时,一直输出为低电平(有效电平)。
图片
极性是高电平时,一直输出为高电平(有效电平)。
图片
3.8 TIM_OCMODE_FORCED_INACTIVE
3.8.1初始化代码
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_FORCED_INACTIVE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
3.8.2 波形
极性是低电平时,一直输出为高电平(无效电平)。
极性是高电平时,一直输出为低电平(无效电平)。
4 应用场景
假设可控硅是低电平导通,我们需要在初始化时输出为高电平,在过零时输出一个低电平,电平的时间可控。
4.1 初始化定时器为TIM_OCMODE_INACTIVE模式
void TIM1_PWM_Init(u16 arr,u16 psc)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = psc;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = arr;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.RepetitionCounter = 0;
HAL_TIM_OC_Init(&htim2);
sConfigOC.OCMode = TIM_OCMODE_INACTIVE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
TIM_CCxChannelCmd(TIM2, TIM_CHANNEL_2, TIM_CCx_ENABLE);//
HAL_TIM_Base_Start_IT(&htim2);
}
4.2 使用按键来模拟过零信号,平时输出为高电平(无效电平),当按键按下时,强制输出为低电平,并且脉宽为207.5*360/415=180ms,然后输出持续为高电平(无效电平)
相关文章