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 April 06, 2014 at 06:48

If you need 3 Phase at 50/50 duty, you could use toggle mode, programming the phase shift into the ''Pulse'' field

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 06, 2014 at 07:21

// STM32F4-Discovery TIM4 Tri-Phase Output PD.12, PD.13 and PD.14 - sourcer32@gmail.com
#include ''stm32f4xx.h''
//******************************************************************************
void RCC_Configuration(void)
{
/* enable peripheral clock for TIM4 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
//******************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOD Configuration: TIM4 on PD12/PD13/PD14 LED */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect TIM4 pin */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4); // PD12 TIM4_CH1
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4); // PD13 TIM4_CH2
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4); // PD14 TIM4_CH3
}
//******************************************************************************
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
int Prescaler, Period;
Prescaler = ((SystemCoreClock / 2) / 20000); // 20 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
Period = 20000 / 1; // 1 Hz - 1 second on, 1 second off duty
// The toggle halves the frequency, a phase shift of 90 degrees (1/4) is 180 degrees (1/2)
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel 1, 2 & 3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = (Period / 6); // CH1 60 + 0 Degrees
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = (Period / 6) + (Period / 3); // CH2 60 + 120 Degrees
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = (Period / 6) + ((Period * 2) / 3); // CH3 60 + 240 Degrees
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
//******************************************************************************
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM4_Configuration();
while(1); /* Infinite loop */
}
/**************************************************************************/
#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) */
while (1)
{}
}
#endif
/**************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tranvanduy
Associate II
Posted on April 16, 2014 at 10:30

Hi Clive, I tested and your code is ok, and now I want to invert direction of motor, I know I have to update period to Ch1 = period*2/3 and Ch3 = period/3, but I don't know how to update period value.

Now i change TIM4_Configuration() function to 2 function: TIM4_Configuration_left() TIM4_Configuration_right() Would you have another way for me update the period to invert direction?! Thanks you very much!

*****************************************
 TIM_OCInitStructure.TIM_Pulse = (Period / 6); // CH1 60 + 0 Degrees
 TIM_OC1Init(TIM4, &TIM_OCInitStructure);
 
 TIM_OCInitStructure.TIM_Pulse = (Period / 6) + (Period / 3); // CH2 60 + 120 Degrees
 TIM_OC2Init(TIM4, &TIM_OCInitStructure);
 
 TIM_OCInitStructure.TIM_Pulse = (Period / 6) + ((Period * 2) / 3); // CH3 60 + 240 Degrees
 TIM_OC3Init(TIM4, &TIM_OCInitStructure);
 *****************************************

Posted on April 16, 2014 at 16:12

The period relates to the speed. To reverse the phase relationship, you'd presumably swap ch 1 & 3 settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tranvanduy
Associate II
Posted on April 16, 2014 at 18:30

oh, I'm sorry. I want to talk about the pulse.

The pulse is set in TIM4_Configuration() function, and I want update the pulse for 3 channel: Left:

*****************************************
TIM_OCInitStructure.TIM_Pulse = 0; // CH1 0 + 0 Degrees
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + (Period / 3); // CH2 0 + 120 Degrees
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + ((Period * 2) / 3); // CH3 0 + 240 Degrees
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
*****************************************

Right:

*****************************************
TIM_OCInitStructure.TIM_Pulse = 0+ ((Period * 2) / 3); // CH1 0 + 240 Degrees
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + (Period / 3); // CH2 0 + 120 Degrees
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0; // CH3 0 + 0 Degrees
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
*****************************************

But I don't know how to update it in while loop, so now I write 2 function TIM4_Configuration_left()

void TIM4_Configuration_left(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; //dung cho PWM
int Prescaler, Period;
Prescaler = ((SystemCoreClock / 2) / 20000); // 20 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
Period = 20000 / 1; // 1 Hz
/*
Prescaler = ((SystemCoreClock / 2) / 360000); // ~360 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
Period = 36000; // ~10 Hz -> ~ 5 Hz
*/
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = Period - 1; 
TIM_TimeBaseStructure.TIM_Prescaler = (Prescaler - 1)/20; //10 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel 1, 2 & 3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0; // CH1 0 + 0 Degrees
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + (Period / 3); // CH2 0 + 120 Degrees
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + ((Period * 2) / 3); // CH3 0 + 240 Degrees
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM4, ENABLE); 
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}

and TIM4_Configuration_right()

void TIM4_Configuration_right(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; //dung cho PWM
int Prescaler, Period;
Prescaler = ((SystemCoreClock / 2) / 20000); // 20 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
Period = 20000 / 1; // 1 Hz
/*
Prescaler = ((SystemCoreClock / 2) / 360000); // ~360 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
Period = 36000; // ~10 Hz -> ~ 5 Hz
*/
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = Period - 1; 
TIM_TimeBaseStructure.TIM_Prescaler = (Prescaler - 1)/20; //10 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel 1, 2 & 3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0+ ((Period * 2) / 3); // CH1 0 + 240 Degrees
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0 + (Period / 3); // CH2 0 + 120 Degrees
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0; // CH3 0 + 0 Degrees
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM4, ENABLE); 
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}

And the main program:

int main(void)
{
RCC_Configuration();
GPIO_Configuration();
while(1) /* Infinite loop */
{
TIM4_Configuration_left();
delay();
TIM4_Configuration_right();
delay();
}
}

But I think it 's not good, I think to have another way to invert direction without reconfig the Timer. Hope you see what I want to say! hihi Thank you!
Posted on April 16, 2014 at 19:38

The total reconfiguration seems rather unnecessary.

Why wouldn't you simply reprogram the TIMx->CCR1, TIMx->CCR2, TIMx->CCR3 in the Update interrupt?

I would recommend you read the chapter(s) on the TIM peripherals in the reference manual.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aspire
Associate II
Posted on April 24, 2014 at 02:30

Hi Mr Clive1,,how to update n generate PWM for delay 120 degree with ADC input..?like as Voltage divider or etc...

Posted on April 24, 2014 at 04:19

Yes, I'm not completely sure what the question is, when language fails consider a diagram.

It should be possible to trigger ADC conversions on various ADC units/channels using TIM channels/updates. The associativity between ADC and TIM triggers can be found in the Reference Manuals for the parts.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aspire
Associate II
Posted on May 06, 2014 at 18:24

hmm,thanks Mr Clive.

by the way, what is other technique if we will generated 3 phasa PWM but it is not use toggle mode..? this PWM use for trigger dc - dc converter.