当前位置:网站首页>Understand the working principle of timer in STM32 in simple terms
Understand the working principle of timer in STM32 in simple terms
2022-07-29 02:05:00 【Just for fun】
Understand in simple terms STM32 Working principle of timer in
One 、 How to realize delay
Time delay It is a common operation in embedded applications , The delay in embedded applications can usually be used as follows 3 Ways of planting Realization :
1 Pure hardware circuit
In early instrumentation , Analog or digital circuits are often used to realize timing / Counting function . for example , Analog electricity
on the way 555 integrated chip , Supplemented by a small amount of resistance and capacitance , You can realize a timer .
Fixed by the hardware circuit structure , By its The resulting delay time cannot be changed .
therefore , The delay realized by pure hardware circuit is mainly used in simple application system without microcontroller or application system with special requirements .
at present , It is less and less to use pure hardware to realize delay in embedded systems .
2 Pure software programming
The microcontroller operates based on certain clock conditions , therefore , The delay operation can be completed according to the clock cycle required for code execution .
Usually , In the embedded system of Jiqian microcontroller , Short time delay for thousands , It can be achieved by executing a certain number of empty instructions , Empty instructions need one cycle each time they are executed ; For long time delay , It can be realized through the loop structure .
for example , You can use circular statements for (nCount = 0xFFFFFF; nCount != 0; nCount--) Realization LED Delay of on and off time .
The pure software method of delay is very simple , but It has the following disadvantages :
(1) For different microcontrollers , The execution time of each instruction is different , It is difficult to achieve accurate delay .
for example , As mentioned above LED Flashing application cases in , If you want to make LED The time of on and off is accurate to 500ms,
The variable that determines the delay time in the loop statement of the corresponding software implementation nCount The specific value of is difficult to be accurately calculated .
(2) During the delay CPU Always occupied , CPU Utilization rate is not high
Although pure software timing / The counting method has the above shortcomings , But it is simple and convenient 、 Easy to implement, etc , In today's embedded applications , Especially in short delay and non Precise delay , Be used frequently . for example , High speed ADC The conversion time of may only need a few clock cycles , In this case , Using software delay is more efficient .
3 Programmable timing / Counter
Current microcontrollers often have one or more built-in timings / Counter , In place of CPU Count , Overcome the shortcomings of pure hardware and pure software , And combine their respective advantages to achieve delay .
also , These timing / Counters are user programmable , The clock source 、 The prescaled coefficients 、 Working mode and startup / Stop and other parameters can be configured by software .
thus it can be seen , Timing built into the microcontroller / The counter has strong versatility 、 User programmable 、 Can be reused 、 Not occupy CPU、 Low cost features , It is currently the most used timing / The implementation of counting .
Two 、 Programmable timing / What functions does the counter have ?
Programmable timing / Counter ( Timer for short ) It is a standard on-chip peripheral and function module for contemporary microcontrollers .
It can not only realize delay , But also complete other functions :
(1) If the clock source comes from the internal system clock , Then programmable timing / The counter can realize accurate timing . At this time, the timer works in normal mode 、 Compare output or PWM The output mode , Usually use thousand delay 、 Output the specified waveform 、 Drive motor and other applications .
(2) If the clock source comes from an external input signal , Then programmable timing / The counter can count the external signals . The timer at this time works in the input capture mode , It is usually used to measure the frequency and duty cycle of the input signal 、 Measuring the number and time interval of external events .
In the application of embedded system , The following functions can be completed by using the timer :
(3) It is used as an interrupt to switch tasks in a multi task time-sharing system .
(4) Perform a task periodically , Such as completing once every fixed time AD collection .
(5) Delay a task for a certain time , Such as traffic light signal changes .
(6) Show real time , Such as the perpetual calendar .
(7) Generate waveforms of different frequencies , Such as MP3 player .
(8) Generate waveforms with different pulse widths , Such as driving servo motor .
(9) Measure the number of swab pulses , Such as measuring County speed .
(10) Measure the pulse width , Such as measuring the maximum frequency .
3、 ... and 、STM32F103 What are your timers

1 Basic timer TIM6 and TIM7
STM32Fl03 Basic timer TIM6 and TIM7 It only has the most basic timing function , namely When the number of cumulative clock pulses exceeds the predetermined value , Generate timer overflow event . If enabled, interrupt or DMA operation , An interrupt or DMA operation .
TIM6 and TIM7 It can also be used as a general timer to provide time benchmark . Specially , They can be digital to analog converters DAC Provide clock . actually , stay STM32Fl03 Inside the microcontroller TIM6 and TIM7 Connect directly to DAC And directly drive... Through trigger output DAC.
(1) internal structure
STM32Fl03 Basic timer TIM6 and TIM7 Its internal structure is relatively simple , from Trigger the controller 、 One 16 Bit prescaler 、 A with automatic reload register 16 Bit counter, etc constitute . among , By programmable 16 position Preassigned frequency counter Driven with automatic reload function 16 Bit counter TIMx_CNT yes STM32Fl03 The core of basic timer .
Little knowledge points : What is prescaler ?
A prescaler is an electronic counting circuit used to reduce a high frequency electrical signal to a lower frequency by integer division.
Prescaler is an electronic counting circuit , Used to reduce high-frequency electrical signals to lower frequencies by integer division .
from STM32Fl03 Basic timer TIM6 and TIM7 We can see the internal structure of , Basic timer TIM6 and TIM7 There is only one clock source Internal clock CK_INT. about STM32Fl03 All timers , Internal clock CK_INT Come from RCC(Reset and Clock Control, Reset and clock control ) Of TIMxCLK.
TIMxCLK according to APBl The prescaler coefficient of is divided into two cases :
(1) if APBl The prescaler coefficient is equal to 1,TIMxCLK be equal to APBl clock frequency PCLK1.
(2) if APBl The prescaler coefficients are different 1,TIMxCLK Equal thousand APBl clock frequency PCLK1X2.
Usually ,STM32Fl03 After power on reset ,APB1 The prescaled coefficient of is 2, APB1 clock frequency PCLKl by 36MHz.
therefore As mentioned above , Basic timer TIM6 and TIM7 The clock of TIMxCLK, yes APBl clock frequency PCLKl Of 2 times , namely 72MHz
STM32F103 In the basic timer 16 Bit counter TIMx_CNT It can only work in the up counting mode , The overflow value of the timer is stored in the automatic reload register .
The basic timer works , Pulse counter TIMx_CNT from 0 open beginning , In the clock CK_CNT Under the trigger, the count is continuously accumulated .
When the pulse counter TIMx_CNT The count value of the register is automatically reloaded TIMx_ARR ( Auto Reload Register) When saving preset values in , Generate overflow Events , Can trigger interrupts or DMA please seek . then , Pulse counter TIMx_CNT The count value of is cleared , Start counting up again .
边栏推荐
- [UE4] replay game playback for ue4.26
- [WesternCTF2018]shrine
- Lm13 morphological quantification momentum period analysis
- Lua log implementation -- print table
- mobile-picker.js
- 动态内存与智能指针
- What is the function of data parsing?
- [golang] use select {}
- How companies make business decisions -- with the help of data-driven marketing
- 忽略微信设置字体
猜你喜欢

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
![[WesternCTF2018]shrine](/img/c1/c099f8930902197590052630281258.png)
[WesternCTF2018]shrine

JS dom2 and dom3

九天后我们一起,聚焦音视频、探秘技术新发展

Nine days later, we are together to focus on the new development of audio and video and mystery technology

使用本地缓存+全局缓存实现小型系统用户权限管理

Tda75610-i2c-determination of I2C address of analog power amplifier

【流放之路-第四章】

druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11

Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
随机推荐
[7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
Planning mathematics final exam simulation II
[云原生]微服务架构是什么
数学建模——派出所选址
What is browser fingerprint recognition
[the road of Exile - Chapter 6]
In depth analysis of C language memory alignment
Why can't Bi software do correlation analysis
Day01 job
数据平台数据接入实践
Leetcode 112: path sum
【流放之路-第七章】
[MySQL] SQL aliases the table
Js DOM2 和 DOM3
Thirty years of MPEG audio coding
What is the function of data parsing?
12.< tag-动态规划和子序列, 子数组>lt.72. 编辑距离
druid. io kill -9 index_ Realtime traceability task
数学建模——仓内拣货优化问题
2022年编程语言排名,官方数据来了,让人大开眼界