cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 - Timer issue after reset

tarikku
Associate II
Posted on March 14, 2011 at 01:28

STM32F103 - Timer issue after reset

5 REPLIES 5
Posted on May 17, 2011 at 14:27

Does anyone have an idea of what might be causing this?

Should we just guess at what you are doing wrong? or are you going to post the code?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tarikku
Associate II
Posted on May 17, 2011 at 14:27

Guessing is good, code might be better...

void RCC_Configuration(void) {

      RCC_DeInit();

      RCC_HSEConfig(RCC_HSE_ON);

      HSEStartUpStatus = RCC_WaitForHSEStartUp();

      if (HSEStartUpStatus == SUCCESS)

      {

        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        FLASH_SetLatency(FLASH_Latency_2);

        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        RCC_PCLK2Config(RCC_HCLK_Div1);

        RCC_PCLK1Config(RCC_HCLK_Div2);

        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

        RCC_PLLCmd(ENABLE);

        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

        {

        }

        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        while(RCC_GetSYSCLKSource() != 0x08)

        {

        }

      }

      else

      {

        while (1)

        {

        }

}

      /* Enable USART1, GPIOA, GPIOB, GPIOC and AFIO clocks */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |

                             RCC_APB2Periph_GPIOB |

                             RCC_APB2Periph_GPIOC |

                             RCC_APB2Periph_AFIO, ENABLE);

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

      RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA |

                             RCC_APB2Periph_GPIOB |

                             RCC_APB2Periph_GPIOC,

                             DISABLE);

}

void initTimer(void)

{

    TIM_TimeBaseInitTypeDef TimeBaseInit;

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TimeBaseInit.TIM_Period = 35999;

    TimeBaseInit.TIM_Prescaler = 999;

    TimeBaseInit.TIM_ClockDivision = TIM_CKD_DIV1;

    TimeBaseInit.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TimeBaseInit);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 6000;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM2, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_ClearFlag(TIM2, TIM_IT_CC1);

    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);

    TIM_Cmd(TIM2, ENABLE);

    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

}

void TIM2_IRQHandler()

{

    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) {

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

        GPIO_WriteBit(GPIOC, GPIO_Pin_12, (BitAction) 1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_12));

    }

}
ColdWeather
Senior
Posted on May 17, 2011 at 14:27

In void initTimer(void) try to call 

 

 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

prior to init calls like

    TIM_OC1Init(TIM2, &TIM_OCInitStructure);

 

    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

 

Posted on May 17, 2011 at 14:27

Yes, you need to move

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

to earlier in the init routine, so that the peripheral is functional while you are programming it's internal registers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tarikku
Associate II
Posted on May 17, 2011 at 14:27

That's done it, thanks a lot