当前位置:网站首页>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
边栏推荐
- 如何判断一个数组中的元素包含一个对象的所有属性值
- Address information parsing in one line of code
- 5种不同的代码相似性检测,以及代码相似性检测的发展趋势
- Article management system based on SSM framework
- pyflink的安装和测试
- Cross-entrpy Method
- [yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
- A brief history of deep learning (II)
- Win10 startup error, press F9 to enter how to repair?
- QT tutorial: creating the first QT program
猜你喜欢
做微服务研发工程师的一年来的总结
Service asynchronous communication
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
集合(泛型 & List & Set & 自定义排序)
Telerik UI 2022 R2 SP1 Retail-Not Crack
Deep understanding of distributed cache design
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Linear algebra of deep learning
Basic information of mujoco
如何判断一个数组中的元素包含一个对象的所有属性值
随机推荐
How to set encoding in idea
threejs图片变形放大全屏动画js特效
Advanced learning of MySQL -- basics -- multi table query -- self join
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
pyflink的安装和测试
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Tencent cloud webshell experience
fastDFS数据迁移操作记录
JS+SVG爱心扩散动画js特效
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
Threejs image deformation enlarge full screen animation JS special effect
C9高校,博士生一作发Nature!
学习使用代码生成美观的接口文档!!!
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
Value Function Approximation
Set (generic & list & Set & custom sort)
深入探索编译插桩技术(四、ASM 探秘)
筑梦数字时代,城链科技战略峰会西安站顺利落幕