当前位置:网站首页>STM32——定时器系列(二)通用定时器
STM32——定时器系列(二)通用定时器
2022-07-17 03:55:00 【*黑心萝卜三条杠*】
在本篇中,小编主要给大家介绍如何使用通用定时器来产生PWM输出。
一、通用定时器的介绍
通用定时器是一个通过可编程预分频器驱动的16 位自动装载计数器构成。它适用于多种场合,包括测量输入信号的脉冲长度(输入采集)或者产生输出波形(输出比较和 PWM)。在使用定时器预分频器和RCC时钟控制器预分频器时,脉冲长度和波形周期可以在几个微秒到几个毫秒间调整。
二、通用定时器的功能
通用TIMx (TIM2、 TIM3、 TIM4和TIM5)定时器功能包括:
■ 16位向上、向下、向上/向下自动装载计数器。
■ 16位可编程(可以实时修改)预分频器。
■ 4个独立通道:
◆ 输入捕获◆ 输出比较◆ PWM生成◆ 单脉冲模式输出
■ 使用外部信号控制定时器和定时器互连的同步电路。
■ 如下事件发生时产生中断/DMA:
◆ 更新:◆ 触发事件◆ 输入捕获◆ 输出比较
■ 支持针对定位的增量(正交)编码器和霍尔传感器电路。
■ 触发输入作为外部时钟或者按周期的电流管理。
三、PWM的介绍
PWM控制即脉冲宽度调制技术,就是通过对一系列脉冲的宽度进行调制,来等效地获得所需要的波形(含形状和幅值), PWM控制技术在逆变电路中应用最广,应用的逆变电路绝大部分是PWM型,广泛应用在从测量、通信到功率控制与变换的许多领域中。
四、PWM的实现方法
实现PWM的方法主要有传统的数字电路、微控制器普通I/O模拟和微控制器的PWM直接输出等。
■ 传统的数字电路方式:用传统的数字电路实现PWM(如555定时器)。
■ 微控制器普通I/O模拟方式:对于微控制器中无PWM输出功能情况(如51单片机),可以通过CPU操控普通I/O口来实现PWM输出。
■ 微控制器的PWM直接输出方式:对于具有PWM输出功能的微控制器,在进行简单的配置后即可在微控制器的指定引脚上输出PWM脉冲。
五、示例
某一天,小明突然想做一个基于STM32F103ZE开发板上的LED6、LED7的呼吸灯,那应该怎么做呢?下面请看小编的实现方法。
1、模块分析
在小明的呼吸灯项目中,我们需要用到的模块不多,仅仅使用LED模块与通用定时器模块就够了。利用LED模块可以控制LED灯状态,利用通用定时器模块来在定长时间后利用LED模块改变LED灯的状态。
2、实现代码
(1)主函数
int main(void)
{
u8 flag = 1;
u32 count = 0;
TIM3_PWMInit();
while(1){
dealy_ms(10);
if(flag==1) {
count++;
if(count>300) flag=0;
}else{
count--;
if(count==0) flag=1;
}
//TIM_SetCompare1函数的参数2 Duty 将TIM3的时间段分成了两个部分,在Duty的时间之前与Duty时间之后两部分的高低电平是不一样的
TIM_SetCompare1(TIM3,count);//设置TIMx捕获比较1寄存器值
TIM_SetCompare2(TIM3,count);//设置TIMx捕获比较2寄存器值
}
}
(2)、通用定时器函数
/* * @brif intialization of generic Timer3 which is used to PAM(初始化用于PWM的通用定时器定时器3) * @para none * @reval none */
void TIM3_PWMInit(void)
{
/* 定义初始化变量 */
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//声明一个结构体变量,用来初始化定时器
TIM_OCInitTypeDef TIM_OCInitStructure;//根据TIM_OCInitStruct中指定的参数初始化外设TIMx
/* 开启时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;//选中LED的引脚
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置LED的频率
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOC,&GPIO_InitStructure);
//TIM3定时器初始化 采用不分频 PWM 频率=72000/900=8Khz
TIM_TimeBaseInitStructure.TIM_Period = 900-1; //设置自动重装载寄存器周期的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1;//设置用来作为TIMx时钟频率预分频值,此处分频系数为1, 即不分频
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseInitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//改变指定管脚的映射 这里选择的是完全重映射
//PWM初始化 根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//设置PWM的输出模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWM输出使能
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//将定时器设置成TIM 输出比较极性低
TIM_OC1Init(TIM3,&TIM_OCInitStructure);//初始换捕获/比较寄存器 1
TIM_OC2Init(TIM3,&TIM_OCInitStructure);//初始化捕获/比较寄存器 2
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能或者失能TIMx在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能或者失能TIMx在CCR2上的预装载寄存器
TIM_Cmd(TIM3,ENABLE);//使能或者失能TIMx外设
}
六、总结
在小明的呼吸灯项目中,由于通用定时器3与PC6、PC7是有重映射关系的,因此,我们在使用的过程中只需要初始化引脚就好,不用刻意的去控制LED灯的亮灭。LED灯的亮灭是伴随通用定时器TIM3通过PWM模式输出而改变的。
边栏推荐
- VS Code 常用快捷键
- Wechat e-book reading applet graduation design of applet completion works (2) applet function
- MAUI 框架入門學習05 MVVM數據模型理解
- [database] must know and be able at the end of the term ----- Chapter 11 concurrency control
- Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
- windows10:vscode下go语言的适配
- What does the project set price mean?
- 如何更有效的过滤病毒/垃圾邮件!
- 企业邮局如何设置SPF记录?
- leetcode977. 有序数组的平方
猜你喜欢

如何更有效的过滤病毒/垃圾邮件!

Wechat e-book reading applet graduation design of applet completion works (3) background function

图形验证码验证
![[seventh issue of notebook series] download and use of openvino pre training model](/img/74/d5958137ddabadef1724afda5656e1.png)
[seventh issue of notebook series] download and use of openvino pre training model

SQL interface switching cannot obtain focus

Small program completion work wechat online education video on demand learning small program graduation design (2) small program function

Record a troubleshooting when overseas pictures cannot be loaded

Wechat e-book reading of applet completion works (7) Interim inspection report

小程序毕设作品之微信电子书阅读小程序毕业设计(4)开题报告

PAC十年:见证HPC从CPU时代走向XPU纪元
随机推荐
Unity Shader - “快速“ 次散射 (Fast SSS : Fast Subsurface Scattering)
Technical writing guide for programmers to leave work early
Use of anti shake debounce and throttling throttle
Wechat online education video on demand learning applet graduation design (3) background function
Wechat e-book reading applet graduation design of applet completion works (3) background function
donet framework4. X==windows form application new project, through system Data. SqlClient connects to sqlserver to query
Data interaction between avframe\avpacket and itself in ffmpeg
Vs Code common shortcut keys
Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino
Leetcode7 DFS + dynamic programming + double pointer
[database] must know at the end of the term ----- Chapter 6 experiment
【数据库】期末必知必会-----第七章 数据库完整性
[database] must know at the end of the term ----- Chapter 2 relational data model
Insert the laptop into the headset and still play it out (the personal test is valid)
[database] must know at the end of the term ----- Chapter VII database integrity
ffmpeg中AVFrame\AVPacket与自己的数据交互
String extension method usage
Chapter 2 performance platform godeye source code analysis - data module
Cocos creator 3.0 Basics - common operations
V4l2 learning materials collection