STM32高级定时器TIM1输出六路带死区互补PWM波形
- 格式:docx
- 大小:898.02 KB
- 文档页数:7
STM32一定时器产生不同频率的PWM(转载)2011-05-05 12:07平时记性实在太差,调试完的程序,过两天又忘了,往往需要一阵子才能想起来,有时以前的资料找不到了,更是恼火,不得不重复到网上搜索。
刚刚调试成功了一个类型的程序,立刻记下来,呵呵,不要又忘记了。
STM32产生PWM是非常的方便的,要需要简单的设置定时器,即刻产生!当然,简单的设置对于新手来产,也是麻烦的,主要包括:(1)使能定时器时钟:RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);(2)定义相应的GPIO:/* PA2,3,4,5,6输出->Key_Up,Key_Down,Key_Left,Key_Right,Key_Ctrl */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_ Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //下拉接地,检测输入的高电平GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度GPIO_Init(GPIOA, &GPIO_InitStructure);/* PA7用于发出PWM波,即无线数据传送 */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度GPIO_Init(GPIOA, &GPIO_InitStructure);(3)如果是产生PWM(频率不变,占空比可变),记得打开PWM控制,在TIM_Configurat ion()中。
STM32F103系列单片机中的定时器工作原理解析
STM32F103系列的单片机一共有11个定时器,其中:
2个高级定时器
4个普通定时器
2个基本定时器
2个看门狗定时器
1个系统嘀嗒定时器
出去看门狗定时器和系统滴答定时器的八个定时器列表;
8个定时器分成3个组;
TIM1和TIM8是高级定时器
TIM2-TIM5是通用定时器
TIM6和TIM7是基本的定时器
这8个定时器都是16位的,它们的计数器的类型除了基本定时器TIM6和TIM7都支持向上,向下,向上/向下这3种计数模式
计数器三种计数模式
向上计数模式:从0开始,计到arr预设值,产生溢出事件,返回重新计时
向下计数模式:从arr预设值开始,计到0,产生溢出事件,返回重新计时
中央对齐模式:从0开始向上计数,计到arr产生溢出事件,然后向下计数,计数到1以后,又产生溢出,然后再从0开始向上计数。
(此种技术方法也可叫向上/向下计数)
基本定时器(TIM6,TIM7)的主要功能:
只有最基本的定时功能,。
基本定时器TIM6和TIM7各包含一个16位自动装载计数器,由各自的可编程预分频器驱动
通用定时器(TIM2~TIM5)的主要功能:
除了基本的定时器的功能外,还具有测量输入信号的脉冲长度(输入捕获)或者产生输出波形(输出比较和PWM)。
高级定时器带死区的互补pwm输出1.引言1.1 概述概述部分(1.1)概述部分是这篇文章的引言,将向读者介绍文章的主题和内容。
本篇文章将探讨高级定时器带死区的互补PWM输出。
在汽车、电机控制和电力电子等领域,PWM(Pulse Width Modulation,脉宽调制)技术被广泛应用于控制电压信号的频率和占空比,以实现对输出功率的精确控制。
高级定时器是一种可编程的硬件设备,用于精确控制时序和周期性事件的发生。
它具有较高的灵活性和可配置性,因此成为实现PWM技术的理想选择。
本篇文章的主要关注点是在高级定时器的基础上,结合死区和互补输出技术实现PWM输出。
死区是指在两个晶体管的开关过程中引入的时间延迟,用于防止两个晶体管同时导通,从而避免短路。
互补输出则是指使用两个相位差为180度的PWM信号,通过分别控制上下半周期的占空比来实现更加精确的电压调节,提高系统的效率和稳定性。
在2.1节中,我们将介绍高级定时器的基本原理,包括其内部结构和工作原理。
2.2节将详细讨论带死区的互补PWM输出的概念、作用以及原理与应用。
最后,在3.1节中,我们将对整篇文章进行总结,并在3.2节中展望高级定时器带死区的互补PWM输出的未来发展方向。
通过本文的阅读,读者将获得对高级定时器带死区的互补PWM输出技术的全面了解,并理解其在各个领域中的重要性和应用前景。
接下来,我们将深入探讨2.1节的高级定时器的基本原理。
1.2 文章结构:本文主要包括两个主题,分别是高级定时器和带死区的互补PWM输出。
文章的结构如下:第一部分是引言部分,包括概述、文章结构和目的。
在概述部分,我们将介绍高级定时器带死区的互补PWM输出的背景和重要性。
在文章结构部分,我们将说明本文的整体组织结构,明确各个章节的内容。
在目的部分,我们将明确本文的写作目的和意义。
第二部分是正文部分,主要包括两个主题的讨论。
首先是高级定时器的介绍,我们将详细讲解定时器的基本原理以及高级定时器的特点。
STM32之TIM1高级定时器展开全文定时器应用之PWM输出1.1 TIM1_CH1N 与 TIM1_CH1 的区别在刚准备使用定时器的时候,我看了下原理图,发现对于定时器1,它的每一个输出通道都是成对的,即TIM1_CH1N与TIM1_CH1两个一组,通过网络查询后,明白了芯片这样设计的原因。
TIM1是一个完整的电机控制用定时器外设,TIM1_CH1和TIM1_CH1N,用于驱动上下两个功率管。
如果Deadtime为0,则TIM1_CH1N是TIM1_CH1的反相,如果Deadtime不为0,则在TIM1_CH1N上插入了Deadtime,防止上下功率管同时导通。
另外的两类管脚定义:TIM1_ETR是外部触发输入管脚;TIM1_BKIN是故障信号,用来关闭TIM1的输出。
1.2 定时器的配置及 PWM 的设置 1.2.1 定时器相关结构体从固件库里的教程CHM获取到的定时器相关的结构体。
TIM_BDTRInitTypeDefBDTR structure definitionTIM_ICInitTypeDefTIM Input Capture Init structure definitionTIM_OCInitTypeDefTIM Output Compare Init structure definitionTIM_TimeBaseInitTypeDefTIM Time Base Init structure definitionTIM_TypeDefTIM其中与PWM输出有关的结构体主要为:TIM_TimeBaseInitTypeDef:定时器初始化配置结构体TIM_OCInitTypeDef:定时器输出比较结构体1.2.2 定时器的三个速度在刚开始学习定时器的时候,我对定时器的速度、技术速度都很迷糊,通过前面对STM32时钟系统的学习,以及RCC库里面几个函数的学习,总算明白了,定时器的这三个速度。
STM32笔记(二)TIM模块产生PWM这个是STM32的PWM输出模式,STM32的TIM1模块是增强型的定时器模块,天生就是为电机控制而生,可以产生3组6路PWM,同时每组2路PW M为互补,并可以带有死区,可以用来驱动H桥。
下面的代码,是利用TIM1模块的1、2通道产生一共4路PWM的代码例子,类似代码也可以参考ST的固件库中相应exampleC语言: TIM1模块产生PWM,带死区//Step1.开启TIM和相应端口时钟//启动GPIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GP IOB | \RCC_APB2Periph_GPIOC | RCC_APB2Periph_GP IOD,\ENABLE);//启动AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//启动TIM1RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//Step2. GPIO做相应设置,为AF输出//PA.8/9口设置为TIM1的OC1输出口GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);//PB.13/14口设置为TIM1_CH1N和TIM1_CH2N输出口GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);//Step3. TIM模块初始化void TIM_Configuration(void){TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;TIM_OCInitTypeDef TIM_OCInitStructure;TIM_BDTRInitTypeDef TIM_BDTRInitStructure;//TIM1基本计数器设置(设置PWM频率)//频率=TIM1_CLK/(ARR+1)TIM_BaseInitStructure.TIM_Period = 1000-1;TIM_BaseInitStructure.TIM_Prescaler = 72-1;TIM_BaseInitStructure.TIM_ClockDivision = 0;TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseInitStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);//启用ARR的影子寄存器(直到产生更新事件才更改设置)TIM_ARRPreloadConfig(TIM1, ENABLE);//TIM1_OC1模块设置(设置1通道占空比)TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enabl e;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Ena ble;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_Pulse = 120;TIM_OC1Init(TIM1, &TIM_OCInitStructure);//启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//TIM2_OC2模块设置(设置2通道占空比)TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enabl e;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Ena ble;TIM_OCInitStructure.TIM_Pulse = 680;TIM_OC2Init(TIM1, &TIM_OCInitStructure);//启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);//死区设置TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;TIM_BDTRInitStructure.TIM_DeadTime = 0x90; //这里调整死区大小0-0xffTIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity _High;TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOu tput_Enable;TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);//TIM1开启TIM_Cmd(TIM1, ENABLE);//TIM1_OC通道输出PWM(一定要加)TIM_CtrlPWMOutputs(TIM1, ENABLE);}其实,PWM模块还可以有很多花样可以玩,比方在异常时(如CPU时钟有问题),可以紧急关闭输出,以免发生电路烧毁等严重事故。
STM32一定时器产生不同频率的PWM(转载)2011-05-05 12:07平时记性实在太差,调试完的程序,过两天又忘了,往往需要一阵子才能想起来,有时以前的资料找不到了,更是恼火,不得不重复到网上搜索。
刚刚调试成功了一个类型的程序,立刻记下来,呵呵,不要又忘记了。
STM32产生PWM是非常的方便的,要需要简单的设置定时器,即刻产生!当然,简单的设置对于新手来产,也是麻烦的,主要包括:(1)使能定时器时钟:RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);(2)定义相应的GPIO:/* PA2,3,4,5,6输出->Key_Up,Key_Down,Key_Left,Key_Right,Key_Ctrl */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_ Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //下拉接地,检测输入的高电平GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度GPIO_Init(GPIOA, &GPIO_InitStructure);/* PA7用于发出PWM波,即无线数据传送 */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度GPIO_Init(GPIOA, &GPIO_InitStructure);(3)如果是产生PWM(频率不变,占空比可变),记得打开PWM控制,在TIM_Configurat ion()中。
STM32之PWM波形输出配置总结1.TIMER分类STM32中一共有11个定时器,其中TIM6、TIM7是基本定时器;TIM2、TIM3、TIM4、TIM5是通用定时器;TIM1和TIM8是高级定时器,以及2个看门狗定时器和1个系统嘀嗒定时器。
其中系统嘀嗒定时器是前文中所描述的SysTick。
其中TIM1和TIM8是能够产生3对PWM互补输出,常用于三相电机的驱动,时钟由APB2的输出产生。
TIM2-TIM5是普通定时器,TIM6和TIM7是基本定时器,其时钟由APB1输出产生。
2.PWM波形产生的原理通用定时器可以利用GPIO引脚进行脉冲输出,在配置为比较输出、PWM输出功能时,捕获/比较寄存器TIMx_CCR被用作比较功能,下面把它简称为比较寄存器。
举例说明定时器的PWM输出工作过程:若配置脉冲计数器TIMx_CNT为向上计数,而重载寄存器TIMx_ARR被配置为N,即TIMx_CNT的当前计数值数值X 在TIMxCLK时钟源的驱动下不断累加,当TIMx_CNT的数值X大于N时,会重置TIMx_CNT数值为0重新计数。
而在TIMxCNT计数的同时,TIMxCNT的计数值X会与比较寄存器TIMx_CCR 预先存储了的数值A进行比较,当脉冲计数器TIMx_CNT的数值X小于比较寄存器TIMx_CCR的值A时,输出高电平(或低电平),相反地,当脉冲计数器的数值X大于或等于比较寄存器的值A时,输出低电平(或高电平)。
如此循环,得到的输出脉冲周期就为重载寄存器TIMx_ARR存储的数值(N+1)乘以触发脉冲的时钟周期,其脉冲宽度则为比较寄存器TIMx_CCR的值A 乘以触发脉冲的时钟周期,即输出PWM的占空比为 A/(N+1) 。
3.STM32产生PWM的配置方法1)配置GPIO口不是每一个IO引脚都可以直接使用于PWM输出,下面是定时器的引脚重映像,其实就是引脚的复用功能选择:表3-1 定时器1的引脚复用功能映像表3-2 定时器2的引脚复用功能映像表3-3 定时器3的引脚复用功能映像表3-4 定时器4的引脚复用功能映像根据以上重映像表,我们使用定时器3的通道2作为PWM的输出引脚,所以需要对PB5引脚进行配置,对IO口操作代码:2)初始化定时器3)设置TIM3_CH2的PWM模式、使能TIM3的CH2输出4)使能定时器3经过以上的操作,定时器3的第二通道已经可以正常工作并输出PWM波了,只是其占空比和频率都是固定的,我们可以通过改变TIM3_CCR2,则可以控制它的占空比。
/******************************************************************************** * @file TIM/6Steps/main.c* @author MCD Application Team* @version V3.5.0* @date 08-April-2011* @brief Main program body****************************************************************************** * @attention** THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONL Y AIMS AT PROVIDING CUSTOMERS* WITH CODING INFORMA TION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SA VE* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.** <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>****************************************************************************** *//* Includes ------------------------------------------------------------------*/#include "stm32f10x.h"/** @addtogroup STM32F10x_StdPeriph_Examples* @{*//** @addtogroup TIM_6Steps* @{*//* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;TIM_BDTRInitTypeDef TIM_BDTRInitStructure;uint16_t CCR1_Val = 32767;uint16_t CCR2_Val = 24575;uint16_t CCR3_Val = 16383;uint16_t CCR4_Val = 8191;/* Private function prototypes -----------------------------------------------*/void RCC_Configuration(void);void GPIO_Configuration(void);void SysTick_Configuration(void);void NVIC_Configuration(void);/* Private functions ---------------------------------------------------------*//*** @brief Main program* @param None* @retval None*/int main(void){/*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startupfile (startup_stm32f10x_xx.s) before to branch to application main.To reconfigure the default setting of SystemInit() function, refer tosystem_stm32f10x.c file*//* System Clocks Configuration */RCC_Configuration();/* NVIC Configuration */NVIC_Configuration();/* GPIO Configuration */GPIO_Configuration();/* SysTick Configuration */SysTick_Configuration();/*-----------------------------------------------------------------------------The STM32F10x TIM1 peripheral offers the possibility to program in advance the configuration for the next TIM1 outputs behaviour (step) and change the configuration of all the channels at the same time. This operation is possible when the COM (commutation) event is used.The COM event can be generated by software by setting the COM bit in the TIM1_EGR register or by hardware (on TRC rising edge).In this example, a software COM event is generated each 100 ms: using the Systick interrupt.The TIM1 is configured in Timing Mode, each time a COM event occurs,a new TIM1 configuration will be set in advance.The following Table describes the TIM1 Channels states:-----------------------------------------------| Step1 | Step2 | Step3 | Step4 | Step5 | Step6 |----------------------------------------------------------|Channel1 | 1 | 0 | 0 | 0 | 0 | 1 |----------------------------------------------------------|Channel1N | 0 | 0 | 1 | 1 | 0 | 0 |----------------------------------------------------------|Channel2 | 0 | 0 | 0 | 1 | 1 | 0 |----------------------------------------------------------|Channel2N | 1 | 1 | 0 | 0 | 0 | 0 |----------------------------------------------------------|Channel3 | 0 | 1 | 1 | 0 | 0 | 0 |----------------------------------------------------------|Channel3N | 0 | 0 | 0 | 0 | 1 | 1 |---------------------------------------------------------------------------------------------------------------------------------------*//* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = 0;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseStructure.TIM_Period = 4095;TIM_TimeBaseStructure.TIM_ClockDivision = 0;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);/* Channel 1, 2,3 and 4 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = 2047;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;TIM_OC1Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = 1023;TIM_OC2Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = 511;TIM_OC3Init(TIM1, &TIM_OCInitStructure);/* Automatic Output enable, Break, dead time and lock configuration*/TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;TIM_BDTRInitStructure.TIM_DeadTime = 1;TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);TIM_CCPreloadControl(TIM1, ENABLE);TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);/* TIM1 counter enable */TIM_Cmd(TIM1, ENABLE);/* Main Output Enable */TIM_CtrlPWMOutputs(TIM1, ENABLE);while (1){}}/*** @brief Configures the different system clocks.* @param None* @retval None*/void RCC_Configuration(void){/* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE); }/*** @brief Configure the TIM1 Pins.* @param None* @retval None*/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;/* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N as alternate function push-pull */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE, &GPIO_InitStructure);/* GPIOE Configuration: BKIN pin */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE, &GPIO_InitStructure);/* TIM1 Full remapping pins */GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);}/*** @brief Configures the SysTick.* @param None* @retval None*/void SysTick_Configuration(void){/* Setup SysTick Timer for 100 msec interrupts */if (SysTick_Config((SystemCoreClock) / 10)){/* Capture error */while (1);}NVIC_SetPriority(SysTick_IRQn, 0x0);}/*** @brief Configures the nested vectored interrupt controller.* @param None* @retval None*/void NVIC_Configuration(void){NVIC_InitTypeDef NVIC_InitStructure;/* Enable the TIM1 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}#ifdef USE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/void assert_failed(uint8_t* file, uint32_t line){/* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */while (1){}}#endif/*** @}*//*** @}*//******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ /******************************************************************************** * @file TIM/6Steps/stm32f10x_it.c* @author MCD Application Team* @version V3.5.0* @date 08-April-2011* @brief Main Interrupt Service Routines.* This file provides template for all exceptions handler and peripherals* interrupt service routine.****************************************************************************** * @attention** THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONL Y AIMS AT PROVIDING CUSTOMERS* WITH CODING INFORMA TION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SA VE* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.** <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>****************************************************************************** *//* Includes ------------------------------------------------------------------*/#include "stm32f10x_it.h"/** @addtogroup STM32F10x_StdPeriph_Examples* @{*//** @addtogroup TIM_6Steps* @{*//* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/__IO uint32_t step = 1;/* Private function prototypes -----------------------------------------------*//* Private functions ---------------------------------------------------------*//****************************************************************************** //* Cortex-M3 Processor Exceptions Handlers *//****************************************************************************** //*** @brief This function handles NMI exception.* @param None* @retval None*/void NMI_Handler(void){}/*** @brief This function handles Hard Fault exception.* @param None* @retval None*/void HardFault_Handler(void){/* Go to infinite loop when Hard Fault exception occurs */ while (1){}}/*** @brief This function handles Memory Manage exception.* @param None* @retval None*/void MemManage_Handler(void){/* Go to infinite loop when Memory Manage exception occurs */ while (1){}}/*** @brief This function handles Bus Fault exception.* @param None* @retval None*/void BusFault_Handler(void){/* Go to infinite loop when Bus Fault exception occurs */while (1){}}/*** @brief This function handles Usage Fault exception.* @param None* @retval None*/void UsageFault_Handler(void){/* Go to infinite loop when Usage Fault exception occurs */ while (1){}}/*** @brief This function handles Debug Monitor exception.* @param None* @retval None*/void DebugMon_Handler(void){}/*** @brief This function handles SVCall exception.* @param None* @retval None*/void SVC_Handler(void){}/*** @brief This function handles PendSV_Handler exception.* @param None* @retval None*/void PendSV_Handler(void){}/*** @brief This function handles SysTick Handler.* @param None* @retval None*/void SysTick_Handler(void){/* Generate TIM1 COM event by software */TIM_GenerateEvent(TIM1, TIM_EventSource_COM);}/****************************************************************************** //* STM32F10x Peripherals Interrupt Handlers *//****************************************************************************** //*** @brief This function handles TIM1 Trigger and commutation interrupts* requests.* @param None* @retval None*/void TIM1_TRG_COM_IRQHandler(void){/* Clear TIM1 COM pending bit */TIM_ClearITPendingBit(TIM1, TIM_IT_COM);if (step == 1){/* Next step: Step 2 Configuration ---------------------------- *//* Channel3 configuration */TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);/* Channel1 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);/* Channel2 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);step++;}else if (step == 2){/* Next step: Step 3 Configuration ---------------------------- *//* Channel2 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);/* Channel3 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);/* Channel1 configuration */TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);step++;}else if (step == 3){/* Next step: Step 4 Configuration ---------------------------- *//* Channel3 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);/* Channel2 configuration */TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);/* Channel1 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable); step++;}else if (step == 4){/* Next step: Step 5 Configuration ---------------------------- *//* Channel3 configuration */TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);/* Channel1 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);/* Channel2 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable); step++;}else if (step == 5){/* Next step: Step 6 Configuration ---------------------------- *//* Channel3 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1); TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);/* Channel1 configuration */TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);/* Channel2 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);step++;}else{/* Next step: Step 1 Configuration ---------------------------- *//* Channel1 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);/* Channel3 configuration */TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);/* Channel2 configuration */TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);step = 1;}}/****************************************************************************** //* STM32F10x Peripherals Interrupt Handlers *//* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the *//* available peripheral interrupt handler's name please refer to the startup *//* file (startup_stm32f10x_xx.s). *//****************************************************************************** //*** @brief This function handles PPP interrupt request.* @param None* @retval None*//*void PPP_IRQHandler(void){}*//*** @}*//*** @}*//******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/。
STM32的PWM闲不下~看看STM32的PWM(2011-07-06 21:21:21)STM32的TIMx 是TIMx_ARR 寄存器确定频率(周期)、由TIMx_CCRx 寄存器确定占空比的信号。
使用定时器3。
而TIM2、3、4的时钟源是APB1 即是PCLK1 ( APB1 对应 PCLK1 )PCLK1 = APB1 = HCLK/2 = SYSCLK/2 = 36MHZ (36,000,000 HZ)但是注意:倍频器会自动倍2,即是【72MHZ】!万历版程序功能:要求:在万利的开发板 EK-STM32F 上产生周期为1秒,占空比分别为 50% 10%的 PWM并且点亮板上的 LD1,LD2 灯闪烁。
voidSTM32_PWM_GPIO_Configuration(void){// 11:完全映像STM32_Afio_Regs->mapr.bit.TIM3_REMAP=3;// LD1 =P7 LD2=PC6STM32_Gpioc_Regs->F6=Output_Af_push_pull; // PC.06 复用功能推挽输出模式STM32_Gpioc_Regs->crl.bit.MODE6=Output_Mode_50mhz; // PC.06 输出模式,最大速度50MHzSTM32_Gpioc_Regs->F7=Output_Af_push_pull; // PC.07 复用功能推挽输出模式STM32_Gpioc_Regs->crl.bit.MODE7=Output_Mode_50mhz; // PC.07 输出模式,最大速度50MHz}//end subvoidSTM32_TIM3_Configuration(void){// TIM_DeInit( TIM3);//复位TIM3定时器STM32_Rcc_Regs->apb1rstr.all |= RCC_TIM3RST;STM32_Rcc_Regs->apb1rstr.all &= ~RCC_TIM3RST;//时钟使能STM32_Rcc_Regs->apb1enr.all |=RCC_TIM3EN;//TIM_TimeBaseStructure.TIM_Period = 9999;//TIM_TimeBaseStructure.TIM_Prescaler = 7200;//TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);STM32_Tim3_Regs->arr.all=9999; // 定时周期,PWM频率!10毫秒*100=1秒STM32_Tim3_Regs->psc.all=720; // 7200分频 72MHZ/72,00 72,000,000/72,00=10,000STM32_Tim3_Regs->cr1.bit.CKD=0; // 时钟分频因子STM32_Tim3_Regs->cr1.bit.DIR=0; // 0:计数器向上计数//TIM_ClearFlag(TIM3, TIM_FLAG_Update);STM32_Tim3_Regs->sr.bit.UIF=0; //更新中断标记由软件清0 ,例如当上溢或下溢时,软件对CNT重新初始化//TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OC1Init(TIM3, &TIM_OCInitStructure);// timer3 的通道1 是 PC6 引脚, AFIO完全映射STM32_Tim3_Regs->1P=0; // 输入/捕获1输出极性 0:OC1高电平有效 1:OC1低电平有效STM32_Tim3_Regs->1E=1; // 输入/捕获1输出使能 1:开启- OC1信号输出到对应的输出引脚。
本文讲述如何配置单片机STM32F407VET6高级定时器TIM1输出六路带死区互补PWM波形。
一、高级定时器TIM1介绍
高级定时器TIM1有5种计数模式:
TIM_CounterMode_Up、TIM_CounterMode_Down
TIM_CounterMode_CenterAligned1
TIM_CounterMode_CenterAligned2
TIM_CounterMode_CenterAligned3
PWM输出有2种模式:
TIM_OCMode_PWM1、TIM_OCMode_PWM2
查看ST官方RM0090参考手册,高级定时器TIM1框图如下:
本文以高级定时器TIM1工作在TIM_CounterMode_Up模式,PWM工作在TIM_OCMode_PWM1为例,讲述如何配置输出六路带死区互补PWM波形。
二、配置代码示例
1.IO配置
硬件IO说明
TIM1_CH1---->PE9 TIM1_CH1N---->PE8
TIM1_CH2---->PE11 TIM1_CH2N---->PE10
TIM1_CH3---->PE13 TIM1_CH3N---->PE12
IO配置具体代码如下:
2. 定时器配置
三、实验测试
1. 测试硬件平台接线
2.测试结果
由测试结果可以看到高级定时器TIM1可以输出六路带死区互补PWM波形。
具体死区时间如何计算,下篇文章会讲解,敬请关注!。