TIM-timer - STM32

TIM-timer - STM32

TIM (Timer) timer
The timer can count the input clock and trigger an interrupt when the count value reaches the set value. The time base
unit of the 16-bit counter, prescaler, and automatic reload register, under the 72MHz count clock It can achieve a maximum timing of 59.65s.
Not only has the basic timing interrupt function, but also includes internal and external clock source selection, input capture, output comparison, encoder interface, master-slave trigger mode and other functions. According to the complexity and application scenarios, it is divided into advanced There are three types of timers, general timers, and basic timers.
insert image description here
insert image description here
insert image description here

Even if I change the prescaler value in the middle of counting, the counting frequency will still remain at the original frequency until the current round of counting is completed, and the changed frequency division value will take effect in the next round of counting.
insert image description here
Registers with a black shadow all have buffer mechanisms such as shadow registers.
insert image description here
The purpose of introducing this shadow register is actually for synchronization, that is, to make the value change and update event occur synchronously, to prevent errors caused by changes during operation, and to interrupt the
insert image description here
timer configuration steps.
The first step is to start the RCC clock
. The second step is to select the time base The clock source of the unit
The third step, configure the time base unit
The fourth step, configure the output interrupt control, allow the update interrupt output to the NMIC
The fifth step, configure the NVIC, open the timer interrupt channel in the NMIC, and assign a
priority Six steps, is the operation control
insert image description here
insert image description here
insert image description here
insert image description here

#include "stm32f10x.h"                  // Device header

//exten声明变量,就是告诉编译器,现在有Num这个变量,e
//它在别的文件里定义了,用户跨文件使用变量
extern uint16_t Num;

void Timer_Intial(void)
{
	//第一步开启时钟,初始化RCC,选择TIM2
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
	//第二步选择时基单元的时钟,选择内部时钟,选择TIM2
	TIM_InternalClockConfig(TIM2);
	
	//第三步配置时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分频系数 1分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数的模式 向上计数
	//计数器溢出频率:CK CNT_OV = CKCNT /(ARR + 1)= CK PSC /(PSC + 1)/(ARR +1)
	//配置定时为1s,则CK CNT_OV=1;CKCNT=72MHz=72000000;
	TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;//周期,ARR自动重装器的值 范围0~65536
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;//PSC预分频器的值 范围0~65536
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; //重复计数器的值 高级计数器才有
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	
	//第四步配置输出中断
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//更新中断
	
	//清除中断标志位,能够避免刚初始化完就进中断的问题
	TIM_ClearFlag(TIM2,TIM_IT_Update);
	
	//第五步,配置NVIC
	//注意:分组方式整个芯片只能用一种,因此,分组的代码只需要执行一次
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择中断分组
	//初始化NVIC
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStruct);

	//第六步,运行控制
	TIM_Cmd(TIM2,ENABLE);
}

//定时器的中断函数
void TIM2_IRQHandler(void)
{
	//首先检查一下中断标志位
	if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
	{
		Num++;
		//清除标志位
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
	}
}


#ifndef _TIMER_H
#define _TIMER_H

void Timer_Intial(void);

#endif

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"
#include "Timer.h"

uint16_t Num;

int main(void)
{

	
	while(1)
	{

	}
}

How to use variables across files, used in the interrupt function

The first solution is to use cross-file variables, which can be declared with extern.
The second solution is to cut the interrupt function to the main function, which is in the same folder as the variable.

Guess you like

Origin blog.csdn.net/qq_45159887/article/details/130462229