STM32定时器的几种输出模式

描述

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,然后输出持续为高电平(无效电平)

key = KEY_Scan(0);
if(key == KEY0_PRES) 
{
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);  
    TIM2->CCMR1 = (TIM_OCMODE_FORCED_ACTIVE<<8);    
    TIM2->CNT=0;
    TIM2->CCR2 = (90*4-1); 
    TIM2->CCMR1 = (TIM_OCMODE_INACTIVE<<8);  
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
    LED0 =!LED0;           
}

4.3 实测波形

波形

5 总结
在工作中需要学习很多新的东西,这就难免会有困惑,当我们束手无策的时候,我们可以借助一些工具,例如逻辑分析仪、示波器来看看数据到底是什么样的,所有的算法都是基于数据来写的,以实际数据为导向,结合理论与实践,只有这样我们才能真正的学到新的东西。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分