cancel
Showing results for 
Search instead for 
Did you mean: 

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

tranvanduy
Associate II
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
40 REPLIES 40
Posted on October 17, 2014 at 21:13

how we can perform logic AND operation of PWM Channel 1 of TIM1 and pwm Channel 1 of TIM3 internally (using programming)?

Does a reading of the manual even suggest that's possible?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hassanbhutta91
Associate II
Posted on October 18, 2014 at 13:53

hassanbhutta91
Associate II
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
Associate II
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
Associate II
Posted on October 19, 2014 at 16:28

clive sir please reply

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
Up vote any posts that you find helpful, it shows what's working..
Rogers.Gary
Senior II
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
Associate II
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???

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
Up vote any posts that you find helpful, it shows what's working..
christiaan2
Associate II
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?