Skip to main content
berkay
Associate
July 30, 2013
Question

STM32F107 Tim1&Tim8 PWM Generation

  • July 30, 2013
  • 4 replies
  • 1167 views
Posted on July 30, 2013 at 16:10

I am trying to generate a  pwm signal from timer1, Pin_E12. I found some helpful posts about stm32f4xx but i could not manage to apply it to stm32f107. Can anyone help me about this subject?

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    July 30, 2013
    Posted on July 30, 2013 at 17:16

    PE12 is the negated output, is that what you were looking for?

    // STM32F107 PWM1 TIM1_CH3N(PE12) TIM1_CH3(PE13) - sourcer32@gmail.com
    #include ''stm32f10x.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* TIM1, GPIOE and AFIO clocks enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* GPIOE Configuration: Channel 3/3N as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    /* TIM1 Full remapping pins (to PE pin bank) */
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
    }
    /**************************************************************************************/
    void TIM1_Configuration(void)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    /* Time Base configuration */
    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 1MHz timebase from 72 MHz bus
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = 10000 - 1; // 100 Hz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    /* Channel 3 Configuration in PWM mode */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_Pulse = 2500; // 25/75 for PE13, 75/25 for PE12
    TIM_OC3Init(TIM1, &TIM_OCInitStructure); // Channel 3
    /* TIM1 counter enable */
    TIM_Cmd(TIM1, ENABLE);
    /* TIM1 Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    TIM1_Configuration();
    while (1); /* Infinite loop */
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    berkay
    berkayAuthor
    Associate
    August 1, 2013
    Posted on August 01, 2013 at 19:04

    Thank you very much for your help.

    yoann
    Visitor II
    December 17, 2013
    Posted on December 17, 2013 at 15:21

    Thanks Clive, very helpful snippet.

    Is it possible to create 3 PWM signals at the same time?

    Thanks in advance

    Tesla DeLorean
    Guru
    December 17, 2013
    Posted on December 17, 2013 at 15:36

    Yes, four are also possible, all the channels on the same timer must share the same frequency. I've posted a couple of 4-Channel servo examples for various STM32 parts.

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