当前位置:网站首页>Part V: STM32 system timer and general timer programming
Part V: STM32 system timer and general timer programming
2022-07-07 00:56:00 【Heavy vehicle water】
1. Timer concept
Timer is essentially a counter , Count from a certain value , When the count value reaches a certain value, the timing time is . The timing time is determined by the counting range ( Initial count ) And the speed of counting ( Current count ) To decide .
Use a timer to make a delay or heartbeat clock , The main steps include starting , Determine the reference clock frequency , Determine the initial count , Query timeout flag , As shown in the figure below .
———————————————————————————————————————————
2.systick ---------------------- System timer
systick It's a simple timer , Belong to ARM nucleus , It is often used for precise delay , It can also be used as the system heartbeat clock .
(1)cortex-M4 System timer of kernel ( Refer to authoritative guidelines 9.5 section )
systick Is a count down ,24 position (0xffffff), With automatic reload function ( After a fixed time , Automatically write an initial count value , There is no need for developers to re initialize the assignment ) Simple timer . The clock source is provided externally , You can choose 8 Frequency division AHB The clock (21MHz) perhaps HCLK(168MHz).
Systick The register organization is as follows , Generally, the registers that need to be configured are control and status registers (CTRL), Reload the load value register (LOAD), Current value register (VAL) These three .
———————————————————————————————————————————
3. Use systick Achieve accurate delay
First step , Select the reference clock (21MHzAHB Clock or HLCK The clock );
The second step , Set the initial count value to determine the timer cycle ( Delay Time );
The third step , Enable timer ;
Step four , The query timeout flag waits for the delay time to .
———————————————————————————————————————————
4.Sydtick Timer code implementation
void delay_init(void)
{
// choice 21MAHB Clock as clock source
//SYSTICK Control and status register second position 0
//SysTick->CTRL &= ~(0x01<<2);
//SysTick->CTRL &= ~SysTick_CTRL_CLKSOURCE_Msk;
// Library function writing
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
}
// choice 21MHz, 1um=21, 1ms=21000
// Millisecond delay 1ms = 21000 n<798 ------ (2^24)/21000
void delay_ms(unsigned int nms)
{
// Calculate the initial value and put it into the initial value register , Calculate from the initial value
SysTick->LOAD = 21000*nms-1;
SysTick->VAL = 0;// The current count value is 0
// start-up systick Start timing
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
// The wait count is 0, Control register 16 Displacement 1
while(!(SysTick->CTRL&SysTick_CTRL_COUNTFLAG_Msk));
// close systick
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}
——————————————————————————————————————————————————————————————————————————————————————5. Universal timer
(1) Introduce
The basic function of timer is timing , General timer belongs to peripheral , Usually used with other hardware , For example, periodically collect sensor data , periodic ADC transformation , Periodically report data , Combine the general timer with GPIO Combine to produce more functions (PWM, Pulse detection .....).
stm32f407 Yes 14 Peripheral timers ,TIM2~TIM5 TIM9~TIM14 It belongs to general timer ,TIM1 and TIM8 It belongs to advanced timer ,TIM6 and TIM7 It belongs to basic timer .
With TIM2~TIM5 For example , Understand the use of general timer
Three counting modes :
Count up ( Increasing ) 0 ~ Initial count
Count down ( Decline ) Initial count ~ 0
Two way counting ( Increasing / Decline ) Initial count ~ 0 ~ Initial count
The original clock frequency of the general timer depends on the bus and the frequency division coefficient of the bus
———————————————————————————————————————————
6. Timer (TIM2) Library function programming implementation
Add the source code of timer library function in the project
(1) Turn on timer 2 The clock of
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
(2) Initialize the timer
void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct); Parameters : TIMx - Which timer TIM_TimeBaseInitStruct - Initialization structure typedef struct { uint16_t TIM_Prescaler; /*!< The prescaled coefficients 0x0000 and 0xFFFF */ uint16_t TIM_CounterMode; /*!< Count mode @ref TIM_Counter_Mode */ uint32_t TIM_Period; /*!< Initial count 0x0000 and 0xFFFF. */ uint16_t TIM_ClockDivision; /*!< Clock factor @ref TIM_Clock_Division_CKD */ uint8_t TIM_RepetitionCounter; /*!< Advanced timer function , Ignore */ } TIM_TimeBaseInitTypeDef;
(3) initialization NVIC
NVIC_Init(...);
(4) Enable timer interrupt
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState); Parameters : TIMx - Which timer TIM_IT - Which interrupt TIM_IT_Update NewState - Can make / prohibit ENABLE
(5) Implement timer interrupt processing function
void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2, TIM_IT_Update)==SET){ //...... // Clear interrupt flag TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } }
(6) Start timer
TIM_Cmd(TIM2,ENABLE);
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Heavy vehicle water 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_44651073/article/details/125576286
边栏推荐
- 新手如何入门学习PostgreSQL?
- build. How to configure the dependent version number in the gradle file
- 【JokerのZYNQ7020】AXI_EMC。
- 【软件逆向-自动化】逆向工具大全
- 如何判断一个数组中的元素包含一个对象的所有属性值
- 第七篇,STM32串口通信编程
- Learn self 3D representation like ray tracing ego3rt
- threejs图片变形放大全屏动画js特效
- String comparison in batch file - string comparison in batch file
- C Primer Plus Chapter 14 (structure and other data forms)
猜你喜欢
Data processing of deep learning
Dr selection of OSPF configuration for Huawei devices
Are you ready to automate continuous deployment in ci/cd?
Deeply explore the compilation and pile insertion technology (IV. ASM exploration)
第五篇,STM32系统定时器和通用定时器编程
Part IV: STM32 interrupt control programming
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
[force buckle]41 Missing first positive number
windows安装mysql8(5分钟)
Attention slam: a visual monocular slam that learns from human attention
随机推荐
threejs图片变形放大全屏动画js特效
Configuring OSPF basic functions for Huawei devices
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
Telerik UI 2022 R2 SP1 Retail-Not Crack
C9 colleges and universities, doctoral students make a statement of nature!
在jupyter中实现实时协同是一种什么体验
Set (generic & list & Set & custom sort)
腾讯云 WebShell 体验
Js+svg love diffusion animation JS special effects
基于SSM框架的文章管理系统
Data processing of deep learning
Advanced learning of MySQL -- basics -- basic operation of transactions
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
Attention slam: a visual monocular slam that learns from human attention
How to set encoding in idea
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
深度学习之环境配置 jupyter notebook
Article management system based on SSM framework