Abstract:This article brings you about the convenience and advantages of using STM32 timer.
Use a timer to calculate the time to get one
A preliminary understanding of timer
The stM32 timer clock diagram is as follows:
Timers 2-7: common timers timers 1 and 8: advanced timers
Second, use timers
Below, I use timer 7 (below TIM7) to calculate the elapsed time to obtain an instruction.
1. TIM7 initialization
In the following initialization functions
- Entry parameters: TIMx: TIM7, ARR: overload value, PSC frequency division value
- Function Run the following command to configure the TIM7 timer
- Return parameter: none
- Note: and Timer7 only have update interrupt function
void Timer7_Init(TIM_TypeDef * TIMx, unsigned short arr, unsigned short psc) { TIM_TimeBaseInitTypeDef timer_initstruct; NVIC_InitTypeDef nvic_initstruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); nvic_initstruct.NVIC_IRQChannel = TIM7_IRQn; timer_initstruct.TIM_CounterMode = TIM_CounterMode_Up; timer_initstruct.TIM_Period = arr; timer_initstruct.TIM_Prescaler = psc; TIM_TimeBaseInit(TIMx, &timer_initstruct); TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE); NVIC_IRQChannelCmd = ENABLE; nvic_initstruct.NVIC_IRQChannelCmd = ENABLE; nvic_initstruct.NVIC_IRQChannelPreemptionPriority = 1; nvic_initstruct.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&nvic_initstruct); TIM_Cmd(TIMx, ENABLE); // Enable timer}Copy the code
2. Interrupt service function
Enter when an update interrupt is generated
void TIM7_IRQHandler(void)
{
if(TIM_GetITStatus(TIM7, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM7, TIM_IT_Update); timer_info.timer7Out++; }}Copy the code
Where timer_info is a structure that holds the update number variable (static variable can also be used)
typedef struct
{
unsigned char timer7Out;
} TIM_INFO;
extern TIM_INFO timer_info;
TIM_INFO timer_info = {0};Copy the code
3. Use routines
- Call Timer6_7_Init(TIM7, 49999, 71); // Count once for 1us, interrupt once for 50ms
- TIM_Cmd(TIM7, DISABLE); TIM_SetCounter(TIM7, 0);
- Turn on the timer before the instruction and turn off the timer after the instruction is complete
- The calculation time is judged by the number of interrupts and the residual value of the counter.
- reset
The following code calculates usart1 for sending “Hello Word!” The time of
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include "timer.h"void Hardware_Init(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Interrupt controller group setting Delay_Init(); //Systick initialization for normal delay Usart1_Init(9600); Timer6_7_Init(TIM7, 49999, 71); TIM_Cmd(TIM7, DISABLE); // Close timer TIM_SetCounter(TIM7, 0); } int main(void) {unsigned int time = 0; Hardware_Init(); // Hardware initializationwhile(1) { TIM_Cmd(TIM7, ENABLE); UsartPrintf(USART1,"\r\nhello word!! n\r\n"); TIM_Cmd(TIM7, DISABLE); // Disable timer time = timer_info.timer7out * 50000; Time += TIM_GetCounter(TIM7); TIM_SetCounter(TIM7, 0); Timer_info.timer7out = 0; UsartPrintf(USART1,"Send time: %d microseconds \r\n", time); DelayMs(2500); }}Copy the code
Results the following
Click to follow, the first time to learn about Huawei cloud fresh technology ~