STM32 TIM 多通道互补PWM波形输出配置快速入门

2024-05-18 18:32

本文主要是介绍STM32 TIM 多通道互补PWM波形输出配置快速入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

platformstm32f10xxx
libSTM32F10x_StdPeriph_Lib_V3.5.0

前言

在做三相逆变的时候,需要软件生成SVPWM波形,具体的算法需要产生三对互补的PWM,这样可以驱动六个开关元件,stm32f103中的TIM1高级定时器支持产生三路互补PWM波形,下面进一步学习。

PWM产生的原理

TIM1OC模块,可以产生PWM波形,具体步骤;

  • 寄存器TIMx CNT每过一个时钟周期就会加1
  • 然后TIMx CNT的值与TIMx CCER进行比较;
  • 最终改变OC上的有效电平;
    以上只需要配置好TIM1的寄存器即可,无需再编程干预,当然可以动态修改TIMx CCER的值,从而改变占空比。可以参考下图;
    在这里插入图片描述

PWM模式

这里主要对PWM模式进行配置的时候做一下区分,存在以下两种情况;

  • TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  • TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

那么TIM_OCMode_PWM1TIM_OCMode_PWM2有什么区别呢?

  • TIM_OCMode_PWM1 PWM模式1
    在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平,否则为无效电平
    在向下计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为无效电平(OC1REF=0),否则为有效 电平(OC1REF=1)。
  • TIM_OCMode_PWM2 PWM模式2
    在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
    在向下计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平,否则为无效电平。

死区插入和刹车功能

互补PWM还支持插入死区时间,最主要的寄存器是TIMx_BDTR,在标准库中把相关的变量封装到TIM_BDTRInitTypeDef结构体中;具体如下;

typedef struct
{uint16_t TIM_OSSRState;        /*!< Specifies the Off-State selection used in Run mode.This parameter can be a value of @ref OSSR_Off_State_Selection_for_Run_mode_state */uint16_t TIM_OSSIState;        /*!< Specifies the Off-State used in Idle state.This parameter can be a value of @ref OSSI_Off_State_Selection_for_Idle_mode_state */uint16_t TIM_LOCKLevel;        /*!< Specifies the LOCK level parameters.This parameter can be a value of @ref Lock_level */ uint16_t TIM_DeadTime;         /*!< Specifies the delay time between the switching-off and theswitching-on of the outputs.This parameter can be a number between 0x00 and 0xFF  */uint16_t TIM_Break;            /*!< Specifies whether the TIM Break input is enabled or not. This parameter can be a value of @ref Break_Input_enable_disable */uint16_t TIM_BreakPolarity;    /*!< Specifies the TIM Break Input pin polarity.This parameter can be a value of @ref Break_Polarity */uint16_t TIM_AutomaticOutput;  /*!< Specifies whether the TIM Automatic Output feature is enabled or not. This parameter can be a value of @ref TIM_AOE_Bit_Set_Reset */
} TIM_BDTRInitTypeDef;

相关的含义看注释可以知道大概意思;这里整理了几个比较重要的变量;

  • 死区时间TIM_DeadTime的计算;TIMx_BDTR的低八位的配置决定了死区的时间;
    UTG[7:0]: 死区发生器设置 (Dead-time generator setup)
    这些位定义了插入互补输出之间的死区持续时间。假设DT表示其持续时间:
DTG[7:5]=0xx => DT=DTG[7:0] × TdtgTdtg = TDTS;
DTG[7:5]=10x => DT=(64+DTG[5:0]) × TdtgTdtg = 2 × TDTS;
DTG[7:5]=110 => DT=(32+DTG[4:0]) × TdtgTdtg = 8 × TDTS;
DTG[7:5]=111 => DT=(32+DTG[4:0])× TdtgTdtg = 16 × TDTS;

例:若TDTS = 125ns(8MHZ),可能的死区时间为:
0到15875ns,若步长时间为125ns;
16us到31750ns,若步长时间为250ns;
32us到63us,若步长时间为1us;
64us到126us,若步长时间为2us;
注:一旦LOCK级别(TIMx_BDTR寄存器中的LOCK位)设为1、 2或3,则不能修改这些位。

  • TIM_Break 则表示是否决定使用刹车,如果发生错误,则可以通过刹车,第一时间停止PWM波形的产生,从而保证了系统的安全性;

代码实现

#include "stm32f10x.h"
#include "svpwm_driver.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_it.h"
#include <stdio.h>#define SVPWM_USE_BDT 	1
#define USE_HARD_PWM 	1
/*** @brief  Configures the different system clocks.* @param  None* @retval None*/
void pwm_rcc_init(void)
{/* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
}void pwm_cnt_irq_init(void)
{}/*** @brief  Configure the TIM1 Pins.* @param  None* @retval None* @note*			PA8 /T1_CH1  ---> HIn3*			PA9 /T1_CH2  ---> HIn2*			PA10/T1_CH3  ---> HIn1*										Out2 ---> PA0/ADC0*										Out3 ---> PA1/ADC1*			PB15/T1_CHN3 ---> LIn1*			PB14/T1_CHN2 ---> LIn2*			PB13/T1_CHN1 ---> LIn3*/
void pwm_pin_init(void)
{GPIO_InitTypeDef GPIO_InitStructure;/* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);/* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;GPIO_Init(GPIOB, &GPIO_InitStructure);
}void pwm_tim_init(void){TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;TIM_BDTRInitTypeDef      TIM_BDTRInitStructure;
//	NVIC_InitTypeDef 	NVIC_InitStructure;uint16_t TimerPeriod = 0;uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0;/* TIM1 Configuration ---------------------------------------------------Generate 7 PWM signals with 4 different duty cycles:TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClockSystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-densityand Connectivity line devices and to 24 MHz for Low-Density Value line andMedium-Density Value line devicesThe objective is to generate 7 PWM signal at 17.57 KHz:- TIM1_Period = (SystemCoreClock / 17570) - 1The channel 1 and channel 1N duty cycle is set to 50%The channel 2 and channel 2N duty cycle is set to 50%The channel 3 and channel 3N duty cycle is set to 50%The Timer pulse is calculated as follows:- ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100----------------------------------------------------------------------- */TimerPeriod = (SYS_FRQ / PWM_FRQ ) - 1;/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */Channel1Pulse = (uint16_t) (((uint32_t) PWM_DUTY * (TimerPeriod - 1)) / 100);/* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */Channel2Pulse = (uint16_t) (((uint32_t) PWM_DUTY * (TimerPeriod - 1)) / 100);/* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */Channel3Pulse = (uint16_t) (((uint32_t) PWM_DUTY * (TimerPeriod - 1)) / 100);//TIM_DeInit(TIM1);/* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSCReloadMode_Update;//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;TIM_TimeBaseStructure.TIM_Period = TimerPeriod;TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);/*TIM_ClearFlag(TIM1, TIM_FLAG_Update);TIM_ITConfig(TIM1,TIM_IT_Update, ENABLE);NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);	*//* Channel 1, 2, 3 Configuration in PWM mode */#if USE_HARD_PWM/**	TIM_OCMode_PWM1	PWM模式1在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平,否则为无效电平在向下计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为无效电平(OC1REF=0),否则为有效 电平(OC1REF=1)。TIM_OCMode_PWM2 PWM模式2在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平在向下计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平,否则为无效电平。*/	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;TIM_OC2Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;TIM_OC3Init(TIM1, &TIM_OCInitStructure);TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);#endif	#if SVPWM_USE_BDTTIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;TIM_BDTRInitStructure.TIM_DeadTime = 30;TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;			   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
#endif	TIM_CCPreloadControl(TIM1,ENABLE);/* TIM1 counter enable */	TIM_Cmd(TIM1, ENABLE);/* TIM1 Main Output Enable */TIM_CtrlPWMOutputs(TIM1, ENABLE);
}void pwm_init(void){pwm_rcc_init();pwm_pin_init();pwm_cnt_irq_init();pwm_tim_init();
}int32_t get_pwm_period(void){return (int32_t)((SYS_FRQ / PWM_FRQ ) - 1);
}void pwm_reset_duty_cnt(uint8_t index, int16_t cnt){if(cnt <= 0){cnt = get_pwm_period()/2;}switch(index){case 1:TIM1->CCR1 = (uint16_t)cnt;break;case 2:TIM1->CCR2 = (uint16_t)cnt;break;case 3:TIM1->CCR3 = (uint16_t)cnt;		break;}	
}void pwm_disable(void){TIM_CtrlPWMOutputs(TIM1, DISABLE);
}void pwm_enable(void){TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

以上代码主要实现以下几个功能;

  • IM1定时器时钟,使用中央对齐模式1,则会产生上溢信号;
  • PWM模式设置为TIM_OCMode_PWM1
  • 加入了死区;
  • 占空比 50%;
    具体如下图所示;
    在这里插入图片描述
    在这里插入图片描述

参考

http://www.stmcu.org.cn/module/forum/thread-613602-1-1.html

这篇关于STM32 TIM 多通道互补PWM波形输出配置快速入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/995652

相关文章

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

springboot security之前后端分离配置方式

《springbootsecurity之前后端分离配置方式》:本文主要介绍springbootsecurity之前后端分离配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录前言自定义配置认证失败自定义处理登录相关接口匿名访问前置文章总结前言spring boot secu

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数