網頁

2011年5月16日 星期一

STM32F103 Timer - base operation

Using Timer2 of STM32F103 to trigger Interrupt every 1us, and the initiation is shown as follow.
void init_timer(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
    TIM_TimeBaseStructure.TIM_Period = 0xA;     // Counter 12 = 1Mhz = 1us        
    TIM_TimeBaseStructure.TIM_Prescaler = 0x5;  // 72Mhz/6 = 12Mhz       
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    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_ClearFlag(TIM2, TIM_FLAG_Update);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}

The program counter goes into TIM2_IRQHandler function when the timer2 trigger interrupt flag.

void TIM2_IRQHandler(void)
{
    // Timer2 Interrupt Handler
}

沒有留言:

張貼留言