AnsweredAssumed Answered

TIM2 in STM32F100C8

Question asked by shankar_k.gowri on Jun 30, 2011
Latest reply on Jul 4, 2011 by Clive One
I m trying to use TIM2 as a general purpose timer to generate an interrupt at every 10mS.

void Timer2_Init(void)
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 10;               
    TIM_TimeBaseStructure.TIM_Prescaler = 36000;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);            /* TIM IT disable */

    TIM_Cmd(TIM2, DISABLE);                                /* TIM2 disable counter */

void RCC_Configuration(void)
    RCC_HSEConfig(RCC_HSE_ON);                                // Enable HSE
    while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);               

    RCC_PCLK2Config(RCC_HCLK_Div1);                         // PCLK2 = HCLK
    RCC_PCLK1Config(RCC_HCLK_Div2);                        // PCLK1 = HCLK/2
    RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3); 
    RCC_PLLCmd(ENABLE);                                    // Enable PLL
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);       
    while(RCC_GetSYSCLKSource() != 0x08);                  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |      
             RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 , ENABLE);   

    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 |
         RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2, ENABLE);   

I have enabled the timer interrupt also.

The above routine doesnt generate an interupt exactly at 10mS.

My external crystal connected is 8MHz. And the SYSCLK is configured as PLLCLK which is operating at 24MHz.

I am not clear how the TIM_PERIOD and TIM_Prescaler are calculated.

As the above example is provided by the STM FAE.

And what is procedure to set a timer as GP Timer?