当前位置:网站首页>STM32 - timer series (II) general timer
STM32 - timer series (II) general timer
2022-07-19 04:24:00 【*Black heart radish three bars*】
In this paper , Xiaobian mainly introduces how to use general timer to generate PWM Output .
One 、 Introduction of general timer
The universal timer is driven by a programmable prescaler 16 Bit auto loading counter . It is suitable for many occasions , Including measuring the pulse length of the input signal ( Input acquisition ) Or produce an output waveform ( Output comparison and PWM). When using timer prescaler and RCC Clock controller prescaler time , Pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds .
Two 、 Function of general timer
Universal TIMx (TIM2、 TIM3、 TIM4 and TIM5) Timer functions include :
■ 16 Position up 、 Down 、 Up / Auto load counter down .
■ 16 Bit programmable ( It can be modified in real time ) Preassigned frequency counter .
■ 4 A separate channel :
◆ Input capture ◆ Output comparison ◆ PWM Generate ◆ Monopulse mode output
■ Use external signal to control timer and timer interconnection synchronization circuit .
■ Interrupt occurs when the following events occur /DMA:
◆ to update :◆ Triggering event ◆ Input capture ◆ Output comparison
■ Support for incremental positioning ( orthogonal ) Encoder and Hall sensor circuits .
■ Trigger input as an external clock or current management by cycle .
3、 ... and 、PWM Introduction to
PWM Control is pulse width modulation technology , By modulating the width of a series of pulses , To equivalently obtain the required waveform ( Including shape and amplitude ), PWM Control technology is most widely used in inverter circuit , Most of the applied inverter circuits are PWM type , It is widely used in measurement 、 Communication to many fields of power control and conversion .
Four 、PWM Implementation method
Realization PWM There are mainly traditional digital circuits 、 Microcontroller common I/O Analog and microcontroller PWM Direct output, etc .
■ Traditional digital circuit : Realize with traditional digital circuit PWM( Such as 555 Timer ).
■ Microcontroller common I/O Simulation mode : For microcontrollers, there is no PWM Output function ( Such as 51 Single chip microcomputer ), Can pass CPU The control is ordinary I/O Mouth to achieve PWM Output .
■ Micro controller PWM Direct output mode : Those who have PWM Microcontroller with output function , After simple configuration, it can be output on the designated pin of the microcontroller PWM pulse .
5、 ... and 、 Example
One day , Xiao Ming suddenly wants to do a project based on STM32F103ZE On the development board LED6、LED7 The breathing lamp of , So how do you do that ? Now let's see the implementation method of Xiaobian .
1、 Module analysis
In Xiao Ming's breathing lamp project , We don't need many modules , Only use LED Module and general timer module are enough . utilize LED The module can control LED Light status , Use the general timer module to use LED Module change LED The state of the light .
2、 Implementation code
(1) The main function
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 The parameters of the function 2 Duty take TIM3 The time period of is divided into two parts , stay Duty Before the time of Duty After time, the high and low levels of the two parts are different
TIM_SetCompare1(TIM3,count);// Set up TIMx Capture comparison 1 Register values
TIM_SetCompare2(TIM3,count);// Set up TIMx Capture comparison 2 Register values
}
}
(2)、 General timer function
/* * @brif intialization of generic Timer3 which is used to PAM( Initialization is used for PWM General timer timer 3) * @para none * @reval none */
void TIM3_PWMInit(void)
{
/* Define initialization variables */
GPIO_InitTypeDef GPIO_InitStructure; // Declare a structure variable , Used to initialize GPIO
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;// Declare a structure variable , Used to initialize the timer
TIM_OCInitTypeDef TIM_OCInitStructure;// according to TIM_OCInitStruct The parameter specified in TIMx
/* Turn on the clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* To configure GPIO Mode and IO mouth */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;// Choose LED The pin of
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;// Set up LED The frequency of
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;// Multiplexing push pull output
GPIO_Init(GPIOC,&GPIO_InitStructure);
//TIM3 Timer initialization Adopt no frequency division PWM frequency =72000/900=8Khz
TIM_TimeBaseInitStructure.TIM_Period = 900-1; // Set the value of the automatic reload register cycle
TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1;// Set as TIMx Clock frequency prescaled value , Here the frequency division coefficient is 1, No frequency division
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;// Set the clock split :TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM Upcount mode
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseInitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);// Change the mapping of the specified pin Here we choose complete remapping
//PWM initialization according to TIM_OCInitStruct The parameter specified in TIMx
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;// Set up PWM The output mode of
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWM Output enable
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;// Set the timer to TIM The output is less polar
TIM_OC1Init(TIM3,&TIM_OCInitStructure);// Initial swap capture / Compare register 1
TIM_OC2Init(TIM3,&TIM_OCInitStructure);// Initialize capture / Compare register 2
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);// To enable or disable TIMx stay CCR1 Pre loaded registers on
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);// To enable or disable TIMx stay CCR2 Pre loaded registers on
TIM_Cmd(TIM3,ENABLE);// To enable or disable TIMx peripherals
}
6、 ... and 、 summary
In Xiao Ming's breathing lamp project , Because of the general timer 3 And PC6、PC7 There is a remapping relationship , therefore , We only need to initialize the pin in the process of using , Don't deliberately control LED The light goes on and off .LED The light turns on and off with a general timer TIM3 adopt PWM Mode output .
边栏推荐
- Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute
- 英特尔助力开立医疗推动超声产检智能化
- Leetcode7 DFS + dynamic programming + double pointer
- Wechat e-book reading applet graduation project (8) graduation project thesis template
- 【数据库】期末必知必会-----第十一章 并发控制
- [seventh issue of notebook series] download and use of openvino pre training model
- Avplayer adds playback progress monitoring
- Build a portrait matting server based on openvino model server
- Machine learning 10: Integrated Learning
- Introduction to PLC OPC information model (DI, PLCopen nodesets)
猜你喜欢

Graphic verification code verification

Chapter 6 performance platform godeye source code analysis - Custom expansion module

Wechat online education video on demand learning applet graduation design (3) background function

SQL interface switching cannot obtain focus

小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书

Wechat e-book reading applet graduation project (6) opening defense ppt

Chapter 5 performance platform godeye source code analysis - third party module

英特尔助力开立医疗推动超声产检智能化

Xcode11 add a boot page (the launch images source option is missing after the upgrade)

C# 字符串(string)常用方法
随机推荐
Intensive reading series of papers
[seventh issue of notebook series] download and use of openvino pre training model
Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
Codeforces Round #807 (Div. 2) A~D
PAC Decade: witness HPC from CPU era to XPU Era
How does the enterprise post office set up SPF records?
51单片机一究到底输入模式
V4l2 learning materials collection
基于STM32的SG90舵机实验含代码(HAL库)
leetcode7-dfs+动态规划+双指针
结构体通过成员变量获取主结构体地址(struct)
Laravel's file upload
How to open the applet for people near wechat (the way to open the applet near wechat)
Wechat e-book reading applet graduation design of applet completion works (1) development outline
Wechat e-book reading applet graduation project (8) graduation project thesis template
小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能
Introduction to PLC OPC information model (DI, PLCopen nodesets)
[database] must know and know at the end of the period ----- Chapter 12 database recovery
Openresty 做静态资源服务器
模拟服务器进行请求