stm32f103 1ms定时器
时间: 2023-09-18 09:11:12 浏览: 70
在STM32F103中,可以使用TIM4定时器来实现1ms的定时功能。通过以下代码可以配置TIM4定时器和中断:
```c
void TIM4_1ms_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = 1000-1; //1ms
TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; //72分频=1MHz
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
//定时器中断
void TIM4_IRQHandler(void) //定时器中断 1ms
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
//根据需要添加相关操作
}
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
```
通过这段代码,可以实现一个1ms的定时器。在定时器中断函数中,可以添加需要执行的操作。<span class="em">1</span>
阅读全文