keil中使用C语言编写Stm32滴答定时器初始化函数例程
时间: 2024-05-26 08:12:55 浏览: 117
很高兴回答您的问题!关于Keil中使用C语言编写Stm32滴答定时器初始化函数的例程,以下是一个参考代码,仅供参考:
#include "stdio.h"
#include "stm32f4xx.h"
void TIM4_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
TIM_TimeBaseInitStruct.TIM_Period = 999; //计数周期为1000
TIM_TimeBaseInitStruct.TIM_Prescaler = 83; //预分频系数为84
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频为1
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct); //初始化TIM4
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //清除TIM4更新中断标志
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //使能TIM4更新中断
NVIC_EnableIRQ(TIM4_IRQn); //使能TIM4中断
TIM_Cmd(TIM4, ENABLE); //使能TIM4
}
//TIM4中断处理函数
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //判断是否为TIM4更新中断
{
//do something
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //清除TIM4更新中断标志
}
}
阅读全文
相关推荐














