Skip to main content
stm3223
Associate II
October 5, 2013
Question

TIM_OCMode_Toggle PWM Duty Controll

  • October 5, 2013
  • 15 replies
  • 3695 views
Posted on October 05, 2013 at 08:36

I'm an absolutely beginner in microcontrollers.

I want to create a PWM signal wave signal  to drive main power switch at 100Khz

TIM1CH1&TIM1CH1N of   STM32F103VC.

I change duty (CCR1_Val) but the duty stay same at 50%.

Can anybody tell me what I'm doing wrong?

Thank You

Photo to Attachments

vu16 CCR1_Val = 90 ;

  TIM_DeInit(TIM1);

  /* Time Base configuration */

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_Period = 360 - 1 ; // 72 MHz / 360 = 200 kHz

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

  /* Channel 1, 2,3 and 4 Configuration in PWM mode */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;

  TIM_OCMode_PWM1

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_Low;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCNIdleState_Set;

    

    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  // 25%

    TIM_OC1Init(TIM1,&TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

 

  /* TIM1 counter enable */

  TIM_Cmd(TIM1,ENABLE);

  /* Main Output Enable */

  TIM_CtrlPWMOutputs(TIM1,ENABLE);

    This topic has been closed for replies.

    15 replies

    waclawek.jan
    Super User
    October 5, 2013
    Posted on October 05, 2013 at 11:02

    Is this a copy/paste from source code which actually compiles?

      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;

      TIM_OCMode_PWM1

      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    JW
    Tesla DeLorean
    Guru
    October 5, 2013
    Posted on October 05, 2013 at 13:53

    TOGGLE mode would tend to do that as the Period is effectively doubled, and will be even, ie 50/50

    If you want PWM mode then used that, and not TOGGLE mode
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    stm3223
    stm3223Author
    Associate II
    October 6, 2013
    Posted on October 06, 2013 at 09:22

    thanks for the reply

    waclawek

    yes

    stm3223
    stm3223Author
    Associate II
    October 6, 2013
    Tesla DeLorean
    Guru
    October 6, 2013
    Posted on October 06, 2013 at 10:43

    But that's not a negated signal, I think here you'd need to use two channels and modulate the pulse width between 0 (OFF) and the 25% on alternating channels at the Update interrupt. 100 KHz should be possible. Alternatively you could use two timers.

    I seem to recall posting an elegant solution to a similar A B timing diagram before, but a quick search isn't pulling it up. I'll need to view my own source trees.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    waclawek.jan
    Super User
    October 7, 2013
    Posted on October 07, 2013 at 09:24

    Isn't this what dead-time insertion is to accomplish in the advanced timers (TIM1 and TIM8)? Refer to the TIMx_BDTR register and ''Complementary outputs and dead-time insertion'' chapter in manual.

    JW
    stm3223
    stm3223Author
    Associate II
    October 7, 2013
    Posted on October 07, 2013 at 18:16

    Thank You  JW

    I am a first step in STM32 & is difficult for understand the switch register from documentation.

    I more efficient The Samples.

    Thanks again

     

     

    Tesla DeLorean
    Guru
    October 7, 2013
    Posted on October 07, 2013 at 18:19

    One might suppose, but I've never had much joy with the dead-time settings, so an example doing this would be welcome.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    October 7, 2013
    Posted on October 07, 2013 at 18:27

    For what it's worth this was the other example I was thinking about.

    // STM32 PWM 1 KHz (TIM1 CH1 PA.08, CH3 PA.10) STM32F4 Discovery - sourcer32@gmail.com
    #include ''stm32f4_discovery.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    /* --------------------------- System Clocks Configuration -----------------*/
    /* TIM1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    /* GPIOA clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /*-------------------------- GPIO Configuration ----------------------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Connect TIM1 pins to AF */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); // CH1
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1); // CH3
    }
    /**************************************************************************************/
    #define ONTIME 800 // 800 us, 80%
    int State = 0;
    void TIM1_UP_TIM10_IRQHandler(void)
    {
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {
    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
    if (State == 0)
    {
    TIM1->CCR1 = ONTIME; // On 80 or 90% typical
    TIM1->CCR3 = 0; // Off
    }
    else
    {
    TIM1->CCR1 = 0; // Off
    TIM1->CCR3 = ONTIME; // On 80 or 90% typical
    }
    State ^= 1;
    }
    }
    /**************************************************************************************/
    void TIM1_Configuration(void)
    {
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
    TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 KHz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    /* Enable TIM1 Preload register on ARR */
    TIM_ARRPreloadConfig(TIM1, ENABLE);
    /* TIM PWM1 Mode configuration */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // ?? 1 / 2
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
    TIM_OCInitStructure.TIM_Pulse = 0; // Off
    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_OCNIdleState_Set;
    /* Output Compare PWM1 Mode configuration: Channel1 PA.08, Channel3 PA.10 */
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);

    /* TIM Interrupts enable */
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    /* TIM1 Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
    /* TIM1 enable counter */
    TIM_Cmd(TIM1, ENABLE);
    }
    /**************************************************************************************/
    void NVIC_Configuration(void)
    {
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Enable the TIM1 update Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    NVIC_Configuration();
    TIM1_Configuration();
    while(1); // Don't want to exit
    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    stm3223
    stm3223Author
    Associate II
    October 7, 2013
    Posted on October 07, 2013 at 21:03

    Thank You very much but I have only TIM1CH1 pin free

    I tried

    to set TIM8 ( pin CH1 & CH2 free) (photo pin) but error comp.

    main.c(357): error: #20: identifier ''TIM1_UP_TIM10_IRQn'' is undefined

    ________________

    Attachments :

    pin.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0dj&d=%2Fa%2F0X0000000bbm%2FmgyuHEhwGLe.R2F8scOCvx0Rj8bUP_oPj9X0KrcIEgg&asPdf=false