最后的实现如下:
初始化函数:
void TIM14_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 系统中TIM14时钟用的是APB1,TIM1用的是APB2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); //tim14时钟使能,APB1时钟8M
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV2; //分频系数为2 //是对APB1的2倍频进行分频,分频系数为2,所以频率还是8M
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数设置 //对于TIM1是必须设置的
// 计算定时周期: t=(9+1)*1/f=10/(8M/(799+1))=10/10k(s)=1ms
TIM_TimeBaseInitStructure.TIM_Period = 9; //定时1000us - 1ms //最大65536
TIM_TimeBaseInitStructure.TIM_Prescaler = 799; //时钟8M
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseInitStructure);
TIM_ClearITPendingBit(TIM14,TIM_IT_Update);//清除TIM14的中断待处理位:TIM 中断源
TIM_ITConfig(TIM14,TIM_IT_Update,ENABLE); //允许定时器14更新中断
TIM_Cmd(TIM14,ENABLE); //使能定时器14
// 设置中断优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;//定时器14中断
NVIC_InitStructure.NVIC_IRQChannelPriority = 0; //优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
|
———————————————— 版权声明:本文为CSDN博主「lintax」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/lintax/article/details/84918791