STM32f103 定时器配置和中断处理函数

// IAR 7.1   定时器中断时间依赖系统外部时钟

#include "stm32_timer.h"

uint8_t tick = 0;
void stm32_timer_init(void)
{
    uint16_t PrescalerValue = 0;
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) (SystemCoreClock  / COUNT_CLK) - 1;

    /* TIM3 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = COUNT_MAX;   //count max number 50000, time 50ms
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* Prescaler configuration */
    TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
    /* TIM Interrupts enable */
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    /* Enable the TIM3 gloabal Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


void stm32_timer_enable(void)
{
    /* Clear all the flags */
    TIM_ClearITPendingBit(TIM3, 0x00ff);
    /* TIM3 enable counter */
    TIM_Cmd(TIM3, ENABLE);
}


void stm32_timer_disable(void)
{
    /* Clear all the flags */
    TIM_ClearITPendingBit(TIM3, 0x00ff);
    /* TIM3 disable counter*/
    TIM_Cmd(TIM3, DISABLE);
}


void TIM3_IRQHandler(void) //50ms
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
        tick++;
    }
}

你可能感兴趣的:(STM32f103 定时器配置和中断处理函数)