Skip to main content
mayurr
Associate II
August 7, 2008
Question

issue in STM32F103XX TIM2

  • August 7, 2008
  • 3 replies
  • 724 views
Posted on August 07, 2008 at 02:55

issue in STM32F103XX TIM2

    This topic has been closed for replies.

    3 replies

    16-32micros
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:41

    Hi mayurr,

    Could you advive about your CPU/AHB and PCLK prescaler clocks in your example ? and what did you find in this particular configuration.

    Cheers,

    STOne-32.

    mayurr
    mayurrAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 12:41

    i have to geneate 0.005 ms timer interupt with tim2 .

    any one tell me how to calculate precaler and preload value?

    i have calulated but is it wrong ?

    and what is the use of clock division ?

    TIM_TimeBaseStructure.TIM_Period =160;

    TIM_TimeBaseStructure.TIM_Prescaler =1124;

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    /* Clear TIM2 update pending flag */

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    /* TIM IT enable */

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    /* TIM2 enable counter */

    TIM_Cmd(TIM2, ENABLE);

    mayurr
    mayurrAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 12:41

    sir,

    I have to generate 0.005 ms tim2 interupt for stm32f103xx.

    and i have to on -off LED after every 5s .

    but it is not haappenibng in practical case.

    in practical case

    LED on-off very fast near about after every 2 sec .

    can anyone give an idea where am i wrong?

    my program :

    GPIO_InitTypeDef GPIO_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    #ifdef DEBUG

    debug();

    #endif

    /* Configure the system clocks */

    RCC_Configuration();

    /* NVIC Configuration */

    //NVIC_Configuration();

    /* Configure PC0,PC1,PC2,PC3 output push-pull */

    GPIO_DeInit(GPIOC);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //| GPIO_Pin_1| GPIO_Pin_2;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOC, &GPIO_InitStructure); // PORT C as output

    NVIC_DeInit();

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    /* Time base configuration */

    TIM_DeInit(TIM2);

    TIM_TimeBaseStructure.TIM_Period =0xFFFF;

    TIM_TimeBaseStructure.TIM_Prescaler =0x02;

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    /* Clear TIM2 update pending flag */

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    /* TIM IT enable */

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    /* TIM2 enable counter */

    TIM_Cmd(TIM2, ENABLE);

    while (1)

    {

    }

    }

    interupt timer routine:

    void TIM2_IRQHandler(void)

    {

    static unsigned long Timer_Value=0;

    if(Flag==0)

    {

    GPIO_SetBits(GPIOC,GPIO_Pin_0);

    if(++Timer_Value==1000000)

    {

    Timer_Value=0;

    Flag=1;

    }

    }

    else

    {

    GPIO_ResetBits(GPIOC,GPIO_Pin_0);

    if(++Timer_Value==1000000)

    {

    Timer_Value=0;

    Flag=0;

    }

    }

    }