Skip to main content
nirmalmallick0
Visitor II
November 8, 2014
Question

STM32f103-I am try to blinking led through timer1 with 50% duty cycle

  • November 8, 2014
  • 2 replies
  • 806 views
Posted on November 08, 2014 at 08:17

Dear frnd,

    i am recently started using this micro and iar.so plz help me 

How to blinking my led though timer1 and 50% duty cycle...the led is connected to PA8 pin of the micro please share any progrram..
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    November 8, 2014
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    November 8, 2014
    Posted on November 08, 2014 at 17:23

    A quick blind port

    // STM32F1xx TIM1_CH1 (PA8) PWM1 1Hz 50/50 Duty - sourcer32@gmail.com
    #include ''stm32f10x.h''
    int main(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    uint32_t Prescaler, Period;
    /*!< 
    At
    this stage the microcontroller clock setting is already configured,
    this is done through SystemInit() function which is called from startup
    file (startup_stm32f10x_xx.s) before to branch to application main.
    To reconfigure the default setting of SystemInit() function, refer to
    system_stm32f10x.c file
    */
    /* Enable GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* Enable TIM clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    /* Configure TIM1_CH1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Mode
    = 
    GPIO_Mode_AF_PP
    ;
    GPIO_InitStructure.GPIO_Pin
    = 
    GPIO_Pin_8
    ;
    GPIO_InitStructure.GPIO_Speed
    = 
    GPIO_Speed_2MHz
    ; // No point in overdriving
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Both these must ultimately fit in 16-bit, ie 1..65536 */
    Prescaler = (SystemCoreClock / 20000); // System -> 20 KHz
    Period = 20000; // 20 KHz -> 1 Hz
    /* Extra caution required with TIM1/TIM8 full function timers, to initialize ALL fields */
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(Prescaler - 1);
    TIM_TimeBaseStructure.TIM_Period = (uint16_t)(Period - 1);
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Where do those stairs go? They go up!
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Not used
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // Not used
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_Pulse = (uint16_t)(Period / 2); // 50%
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    /* TIM1 enable counter */
    TIM_Cmd(TIM1, ENABLE);
    /* TIM1 Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
    /* Infinite loop */
    while(1);
    }
    #ifdef USE_FULL_ASSERT
    /**
    * @brief Reports the name of the source file and the source line number
    * where the assert_param error has occurred.
    * @param file: pointer to the source file name
    * @param line: assert_param error line source number
    * @retval None
    */
    void assert_failed(uint8_t* file, uint32_t line)
    {
    /* User can add his own implementation to report the file name and line number,
    ex: printf(''Wrong parameters value: file %s on line %d
    
    '', file, line) */
    /* Infinite loop */
    while (1)
    {
    }
    }
    #endif

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..