当前位置:网站首页>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
边栏推荐
- 【JokerのZYNQ7020】AXI_EMC。
- C Primer Plus Chapter 14 (structure and other data forms)
- stm32F407-------SPI通信
- Address information parsing in one line of code
- Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
- Part IV: STM32 interrupt control programming
- Use mujoco to simulate Cassie robot
- 深入探索编译插桩技术(四、ASM 探秘)
- Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
- 城联优品入股浩柏国际进军国际资本市场,已完成第一步
猜你喜欢
Stm32f407 ------- DAC digital to analog conversion
Data processing of deep learning
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
Interface master v3.9, API low code development tool, build your interface service platform immediately
Idea automatically imports and deletes package settings
集合(泛型 & List & Set & 自定义排序)
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Telerik UI 2022 R2 SP1 Retail-Not Crack
Configuring OSPF basic functions for Huawei devices
Part IV: STM32 interrupt control programming
随机推荐
Model-Free Prediction
stm32F407-------DAC数模转换
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
Advanced learning of MySQL -- basics -- multi table query -- subquery
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
集合(泛型 & List & Set & 自定义排序)
fastDFS数据迁移操作记录
Hero League | King | cross the line of fire BGM AI score competition sharing
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
[C language] dynamic address book
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Use mujoco to simulate Cassie robot
Policy Gradient Methods
Linear algebra of deep learning
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
建立自己的网站(17)
Link sharing of STM32 development materials
第五篇,STM32系统定时器和通用定时器编程
C9高校,博士生一作发Nature!