stm32f103c8t6 定时器中断的demo
时间: 2023-04-07 14:03:47 浏览: 109
STM32F103标准库开发-定时器中断实验
5星 · 资源好评率100%
可以回答这个问题。以下是一个简单的 stm32f103c8t6 定时器中断的 demo:
```c
#include "stm32f10x.h"
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
// 在这里写中断处理程序
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
int main(void)
{
// 初始化定时器
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 使能定时器中断
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
// 启动定时器
TIM_Cmd(TIM2, ENABLE);
while (1)
{
// 在这里写主程序
}
}
```
阅读全文