#include "stm32f10x.h" // Device header
extern uint16_t Num;
void Timer_Init(void)
{
//1、RCC开启时钟 通用定时器:TIM6
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
//2、选择时基单元时钟
TIM_InternalClockConfig(TIM6); //使用内部时钟(默认)
//3、配置时基单元
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波器分频
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式:向上计数
TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1; //ARR(0~65535) CK_CNT_OV = CK_PSC/(PSC+1)/(ARR+1); 1S : 1HZ = 72,000,000/(7200)/(10000)
TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1; //PSC(0~65535)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器,高级计数器才有
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM6, TIM_FLAG_Update);
//4、使能中断
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
//5、NVIC配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //优先级分组 2位响应,2位抢占
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn ; //配置通道
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_Init(&NVIC_InitStructure);
//6、启动定时器
TIM_Cmd(TIM6, ENABLE);
}
void TIM6_IRQHandler(void)
{
if (TIM_GetITStatus(TIM6, TIM_IT_Update) == SET) //更新中断
{
Num++;
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
}
}