定时器中断

Time_Nvic.h

#ifndef __Time_Nvic_h
#define __Time_Nvic_h

void Tim2_Init(void);
void Tim2_Nvic_Init(void);
void Tim2_Base_Init(void);
void TIM2_IRQHandler(void);

#endif

Time_Nvic.c

#include 
#include "Time_Nvic.h"
#include "stm32f10x.h"
void Tim2_Init(void)
{
    Tim2_Nvic_Init();
    Tim2_Base_Init();
}
void Tim2_Nvic_Init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8;//响应优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void Tim2_Base_Init(void) // 1ms
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    TIM_TimeBaseStructure.TIM_Period = 99;  // 装载
    TIM_TimeBaseStructure.TIM_Prescaler = 719; //时钟系数
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //时钟模式
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //打开定时器2
    TIM_Cmd(TIM2,DISABLE); //开启中断
}

void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
     //中断内容
   }
}

你可能感兴趣的:(STM32)