STM32F103定时10s中断
时间: 2023-05-19 13:03:14 浏览: 73
STM32F103实现定时器中断【支持STM32F10X系列单片机】
可以使用以下代码实现:
#include "stm32f10x.h"
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
// 在这里执行需要定时的操作
// ...
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
}
int main(void)
{
// 初始化定时器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 10000 - 1; // 定时10s
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; // 时钟预分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// 使能定时器中断
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 启动定时器
TIM_Cmd(TIM3, ENABLE);
while (1)
{
// 主循环
// ...
}
}
阅读全文