Skip to main content
tranvanduy
Associate
April 6, 2014
Question

[HELP] STM32F4 generate PWM 3 channel shift phase 120 degree!

  • April 6, 2014
  • 40 replies
  • 6117 views
Posted on April 06, 2014 at 06:30

Hi all!

I want to control 3 phase motor (I get HDD motor from old HDD), and now I can generate 3 PWM signal on 3 channels but they are same, not

shift phase

120 degree!

Please help me!

I use STM32F4 discovery board and Keil!

Thanks all
    This topic has been closed for replies.

    40 replies

    hassanbhutta91
    Visitor II
    October 18, 2014
    Posted on October 18, 2014 at 13:53

    hassanbhutta91
    Visitor II
    October 18, 2014
    Posted on October 18, 2014 at 16:22

     #include ''main.h''

    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

    GPIO_InitTypeDef bhutta;

    ..

    .

    .

    .

     Code

    ...

    .

    .

        

    }

    void gpio_function1()

    {

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD,ENABLE);

      bhutta.GPIO_Mode=GPIO_Mode_OUT;

      bhutta.GPIO_OType=GPIO_OType_PP;

      bhutta.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;

        bhutta.GPIO_PuPd=GPIO_PuPd_NOPULL;

        bhutta.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_Init(GPIOD,&bhutta);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1,GPIO_AF_1);

    }

    int c=1804;

    unsigned int val=0;

    float voltage=0.0;

     

    int main(void)

    {

      gpio1();

        gpio();

        timer_init();

        timer_init1();

        gpio_function1();

        gpio_function();

        adc_function();

        while(1)

        {

            

            

            /* Test EOC flag */

        while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

        

        /* Get ADC1 converted data */

        val =ADC_GetConversionValue(ADC1);

            ADC_StartConversion(ADC1);

            voltage = (3.3*(val-500))/4095.0;

        TIM_SetCompare1(TIM3,3814*voltage);// 4500 for 50% duty cycle

                GPIO_WriteBit(GPIOD, GPIO_Pin_1,  (GPIO_ReadOutputDataBit(GPIOC,6)&&GPIO_ReadOutputDataBit(GPIOE,9)) );//  

    }

    }

    #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\r\n'', file, line) */

      /* Infinite loop */

      while (1)

      {

        

      }

    }

    #endif

    hassanbhutta91
    Visitor II
    October 18, 2014
    Posted on October 18, 2014 at 16:30

    sir i am doing in by calling this function in main function , but no output on oscilloscope..

     GPIO_WriteBit(GPIOD, GPIO_Pin_1,  (GPIO_ReadOutputDataBit(GPIOC,6)&&GPIO_ReadOutputDataBit(GPIOE,9)) );//  

    }

    hassanbhutta91
    Visitor II
    October 19, 2014
    Posted on October 19, 2014 at 16:28

    clive sir please reply

    Tesla DeLorean
    Guru
    October 19, 2014
    Posted on October 19, 2014 at 19:38

    The pin parameter here is not numeric. GPIO_ReadOutputDataBit()

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Rogers.Gary
    Senior II
    January 27, 2015
    Posted on January 27, 2015 at 18:49

    One of the easiest, simple and functional code samples for timer functionality that I have seen. I changed from toggle mode to PWM1 and it works great. clive1 = Sir Brilliant

    hassanbhutta91
    Visitor II
    March 7, 2015
    Posted on March 07, 2015 at 16:34

    hye , clive sir 

    i am changing the frequency using variable resistor through adc.but when i vary freq. the output of channel 2 and channel 3 toggles.  how can i get rid of this toggling???

    Tesla DeLorean
    Guru
    March 7, 2015
    Posted on March 07, 2015 at 21:29

    I think you'd probably want to change all the settings at the Update, and avoid the preload/immediate functionality.

    If you pull in the period any when else, you run the risk of triggering the compares, and causing it to toggle.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    christiaan2
    Associate II
    July 21, 2015
    Posted on July 21, 2015 at 14:47

    Hi Mr clive1

    I have worked through this example code but when I measure phase shifts on oscilloscope I get 60º shift instead of 120º shift?

    Tesla DeLorean
    Guru
    July 21, 2015
    Posted on July 21, 2015 at 17:37

    Ok, the toggle only deals with half a cycle.

    You'll need to play with the Pulse settings, and to get to 240 degrees you might need to invert the initial phase/state of things beyond 180

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