Anda di halaman 1dari 6

Forum- [HELP] STM32F4 generatePWM 3 cha...

- STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]
This List: STM32 Evaluation To
Home Products Applications Support Sample & Buy My ST Login Parametric Search
Home STe2eCommunities >Microcontrollers >STM32 Evaluation Tools >[HELP] STM32F4 generate PWM 3 channel shift phase 120 degree!
STM32 Evaluation Tools
STM32 Discovery Kit, STM32 Nucleo, STM32 Eval Board
Actions Share Discussion View: Flat
Post
Started: 4/6/2014 6:30 AM View Properties/Attachments Reply
duy.tran_van
Posts : 3
[HELP] STM32F4 generate PWM 3 channel shift phase 120 degree!
Hi all!
I want to control 3 phase motor (I get HDD motor fromold 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
Posted: 4/6/2014 6:48 AM View Properties/Attachments Reply
clive1
Posts :
8781
If you need 3 Phase at 50/50 duty, you could use toggle mode, programming the phase shift into the "Pulse"
field
Posted: 4/6/2014 7:21 AM 2 View Properties/Attachments Reply
clive1
Posts :
8781
/ / STM32F4- Di scover y TI M4 Tr i - Phase Out put PD. 12, PD. 13 and PD. 14 -
sour cer 32@gmai l . com

#i ncl ude " st m32f 4xx. h"

/ / ******************************************************************************

voi d RCC_Conf i gur at i on( voi d)
{
/ * enabl e per i pher al cl ock f or TI M4 */
RCC_APB1Per i phCl ockCmd( RCC_APB1Per i ph_TI M4, ENABLE) ;

/ * GPI OD cl ock enabl e */
RCC_AHB1Per i phCl ockCmd( RCC_AHB1Per i ph_GPI OD, ENABLE) ;
}

My Forum Links
Sign in to Forum
Communities Home
Code Of Conduct
Browse Public Forums
(NOTE these forums are open to everybody!)
Analog and Audio
Embedded Microprocessors
EMI Filtering and Signal Conditioning
Interface and Connectivity ICs
Memories
MEMS and Sensors
Microcontrollers
PSD
iNEMO MEMS/STM32 Board
SPC5 Studio
SPC56
ST10
ST6
ST7
ST9
STM32
STM32 Evaluation Tools
STM32 Software Tools and Firmware
STM8
STM8 Discovery
STMTouch
STPC
STR7
STR9
Motor Control
Online Tools
Posted By
This List: STM32 Evaluation Tools
Forum- [HELP] STM32F4 generatePWM 3 cha... - STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]
/ / ******************************************************************************

voi d GPI O_Conf i gur at i on( voi d)
{
GPI O_I ni t TypeDef GPI O_I ni t St r uct ur e;

/ * GPI OD Conf i gur at i on: TI M4 on PD12/ PD13/ PD14 LED */
GPI O_I ni t St r uct ur e. GPI O_Pi n = GPI O_Pi n_12 | GPI O_Pi n_13 | GPI O_Pi n_14;
GPI O_I ni t St r uct ur e. GPI O_Mode = GPI O_Mode_AF;
GPI O_I ni t St r uct ur e. GPI O_Speed = GPI O_Speed_2MHz;
GPI O_I ni t St r uct ur e. GPI O_OType = GPI O_OType_PP;
GPI O_I ni t St r uct ur e. GPI O_PuPd = GPI O_PuPd_NOPULL;
GPI O_I ni t ( GPI OD, &GPI O_I ni t St r uct ur e) ;

/ * Connect TI M4 pi n */
GPI O_Pi nAFConf i g( GPI OD, GPI O_Pi nSour ce12, GPI O_AF_TI M4) ; / / PD12 TI M4_CH1
GPI O_Pi nAFConf i g( GPI OD, GPI O_Pi nSour ce13, GPI O_AF_TI M4) ; / / PD13 TI M4_CH2
GPI O_Pi nAFConf i g( GPI OD, GPI O_Pi nSour ce14, GPI O_AF_TI M4) ; / / PD14 TI M4_CH3
}

/ / ******************************************************************************

voi d TI M4_Conf i gur at i on( voi d)
{
TI M_Ti meBaseI ni t TypeDef TI M_Ti meBaseSt r uct ur e;
TI M_OCI ni t TypeDef TI M_OCI ni t St r uct ur e;
i nt Pr escal er , Per i od;

Pr escal er = ( ( Syst emCor eCl ock / 2) / 20000) ; / / 20 KHz t i mebase, assumes APB1
H/ 4 TI MCLK4 H/ 2

Per i od = 20000 / 1; / / 1 Hz - 1 second on, 1 second of f dut y

/ / The t oggl e hal ves t he f r equency, a phase shi f t of 90 degr ees ( 1/ 4) i s 180
degr ees ( 1/ 2)

/ * Ti me base conf i gur at i on */
TI M_Ti meBaseSt r uct ur e. TI M_Per i od = Per i od - 1;
TI M_Ti meBaseSt r uct ur e. TI M_Pr escal er = Pr escal er - 1;
TI M_Ti meBaseSt r uct ur e. TI M_Cl ockDi vi si on = 0;
TI M_Ti meBaseSt r uct ur e. TI M_Count er Mode = TI M_Count er Mode_Up;
TI M_Ti meBaseI ni t ( TI M4, &TI M_Ti meBaseSt r uct ur e) ;

/ * Out put Compar e Toggl e Mode conf i gur at i on: Channel 1, 2 & 3 */
TI M_OCI ni t St r uct ur e. TI M_OCMode = TI M_OCMode_Toggl e;
TI M_OCI ni t St r uct ur e. TI M_Out put St at e = TI M_Out put St at e_Enabl e;
TI M_OCI ni t St r uct ur e. TI M_OCPol ar i t y = TI M_OCPol ar i t y_Hi gh;

TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) ; / / CH1 60 + 0 Degr ees
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) + ( Per i od / 3) ; / / CH2 60 + 120
Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) + ( ( Per i od * 2) / 3) ; / / CH3 60
+ 240 Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

/ * TI M4 enabl e count er */
TI M_Cmd( TI M4, ENABLE) ;
}

/ / ******************************************************************************

i nt mai n( voi d)
{
RCC_Conf i gur at i on( ) ;

GPI O_Conf i gur at i on( ) ;

TI M4_Conf i gur at i on( ) ;

whi l e( 1) ; / * I nf i ni t e l oop */
}

/ **************************************************************************/

#i f def USE_FULL_ASSERT

/ **
* @br i ef Repor t s t he name of t he sour ce f i l e and t he sour ce l i ne number
* wher e t he asser t _par amer r or has occur r ed.
* @par am f i l e: poi nt er t o t he sour ce f i l e name
* @par am l i ne: asser t _par amer r or l i ne sour ce number
* @r et val None
*/
voi d asser t _f ai l ed( ui nt 8_t * f i l e, ui nt 32_t l i ne)
Power Conversion
ST e2e Communities Support
Forum- [HELP] STM32F4 generatePWM 3 cha... - STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]
{
/ * User can add hi s own i mpl ement at i on t o r epor t t he f i l e name and l i ne
number ,
ex: pr i nt f ( " Wr ong par amet er s val ue: f i l e %s on l i ne %d\ r \ n" , f i l e, l i ne)
*/

whi l e ( 1)
{}
}
#endi f

/ **************************************************************************/
Posted: 4/16/2014 10:30 AM View Properties/Attachments Reply
duy.tran_van
Posts : 3
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!
*****************************************
TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) ; / / CH1 60 + 0 Degr ees
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) + ( Per i od / 3) ; / / CH2 60 + 120
Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = ( Per i od / 6) + ( ( Per i od * 2) / 3) ; / / CH3 60
+ 240 Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;
*****************************************
Show Quoted Messages
Posted: 4/16/2014 4:12 PM View Properties/Attachments Reply
clive1
Posts :
8781
The period relates to the speed. To reverse the phase relationship, you'd presumably swap ch 1 & 3 settings.
Edited: 4/16/2014 6:57 PM View Properties/Attachments Reply
duy.tran_van
Posts : 3
oh, I'msorry. 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:
*****************************************
TI M_OCI ni t St r uct ur e. TI M_Pul se = 0; / / CH1 0 + 0 Degr ees
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( Per i od / 3) ; / / CH2 0 + 120 Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( ( Per i od * 2) / 3) ; / / CH3 0 + 240 Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;
*****************************************
Right:
*****************************************
TI M_OCI ni t St r uct ur e. TI M_Pul se = 0+ ( ( Per i od * 2) / 3) ; / / CH1 0 + 240 Degr ees
Forum- [HELP] STM32F4 generatePWM 3 cha... - STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( Per i od / 3) ; / / CH2 0 + 120 Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0; / / CH3 0 + 0 Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;
*****************************************
But I don't know how to update it in while loop, so now I write 2 function TIM4_Configuration_left()
voi d TI M4_Conf i gur at i on_l ef t ( voi d)
{
TI M_Ti meBaseI ni t TypeDef TI M_Ti meBaseSt r uct ur e;
TI M_OCI ni t TypeDef TI M_OCI ni t St r uct ur e; / / dung cho PWM
i nt Pr escal er , Per i od;

Pr escal er = ( ( Syst emCor eCl ock / 2) / 20000) ; / / 20 KHz t i mebase, assumes APB1
H/ 4 TI MCLK4 H/ 2

Per i od = 20000 / 1; / / 1 Hz
/ *
Pr escal er = ( ( Syst emCor eCl ock / 2) / 360000) ; / / ~360 KHz t i mebase,
assumes APB1 H/ 4 TI MCLK4 H/ 2
Per i od = 36000; / / ~10 Hz - > ~ 5 Hz
*/


/ * Ti me base conf i gur at i on */
TI M_Ti meBaseSt r uct ur e. TI M_Per i od = Per i od - 1;
TI M_Ti meBaseSt r uct ur e. TI M_Pr escal er = ( Pr escal er - 1) / 20; / / 10 Hz
TI M_Ti meBaseSt r uct ur e. TI M_Cl ockDi vi si on = 0;
TI M_Ti meBaseSt r uct ur e. TI M_Count er Mode = TI M_Count er Mode_Up;
TI M_Ti meBaseI ni t ( TI M4, &TI M_Ti meBaseSt r uct ur e) ;

/ * Out put Compar e Toggl e Mode conf i gur at i on: Channel 1, 2 & 3 */
TI M_OCI ni t St r uct ur e. TI M_OCMode = TI M_OCMode_Toggl e;

TI M_OCI ni t St r uct ur e. TI M_Out put St at e = TI M_Out put St at e_Enabl e;
TI M_OCI ni t St r uct ur e. TI M_OCPol ar i t y = TI M_OCPol ar i t y_Hi gh;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0; / / CH1 0 + 0 Degr ees
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( Per i od / 3) ; / / CH2 0 + 120 Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( ( Per i od * 2) / 3) ; / / CH3 0 + 240
Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_ARRPr el oadConf i g( TI M4, ENABLE) ;

/ * TI M4 enabl e count er */
TI M_Cmd( TI M4, ENABLE) ;
}
and TIM4_Configuration_right()
voi d TI M4_Conf i gur at i on_r i ght ( voi d)
{
TI M_Ti meBaseI ni t TypeDef TI M_Ti meBaseSt r uct ur e;
TI M_OCI ni t TypeDef TI M_OCI ni t St r uct ur e; / / dung cho PWM
i nt Pr escal er , Per i od;

Pr escal er = ( ( Syst emCor eCl ock / 2) / 20000) ; / / 20 KHz t i mebase, assumes APB1
H/ 4 TI MCLK4 H/ 2

Per i od = 20000 / 1; / / 1 Hz
/ *
Pr escal er = ( ( Syst emCor eCl ock / 2) / 360000) ; / / ~360 KHz t i mebase,
assumes APB1 H/ 4 TI MCLK4 H/ 2
Per i od = 36000; / / ~10 Hz - > ~ 5 Hz
*/


/ * Ti me base conf i gur at i on */
TI M_Ti meBaseSt r uct ur e. TI M_Per i od = Per i od - 1;
TI M_Ti meBaseSt r uct ur e. TI M_Pr escal er = ( Pr escal er - 1) / 20; / / 10 Hz
TI M_Ti meBaseSt r uct ur e. TI M_Cl ockDi vi si on = 0;
TI M_Ti meBaseSt r uct ur e. TI M_Count er Mode = TI M_Count er Mode_Up;
TI M_Ti meBaseI ni t ( TI M4, &TI M_Ti meBaseSt r uct ur e) ;

/ * Out put Compar e Toggl e Mode conf i gur at i on: Channel 1, 2 & 3 */
TI M_OCI ni t St r uct ur e. TI M_OCMode = TI M_OCMode_Toggl e;

TI M_OCI ni t St r uct ur e. TI M_Out put St at e = TI M_Out put St at e_Enabl e;
TI M_OCI ni t St r uct ur e. TI M_OCPol ar i t y = TI M_OCPol ar i t y_Hi gh;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0+ ( ( Per i od * 2) / 3) ; / / CH1 0 + 240 Degr ees
TI M_OC1I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;
Forum- [HELP] STM32F4 generatePWM 3 cha... - STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0 + ( Per i od / 3) ; / / CH2 0 + 120 Degr ees
TI M_OC2I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_OCI ni t St r uct ur e. TI M_Pul se = 0; / / CH3 0 + 0 Degr ees
TI M_OC3I ni t ( TI M4, &TI M_OCI ni t St r uct ur e) ;

TI M_ARRPr el oadConf i g( TI M4, ENABLE) ;

/ * TI M4 enabl e count er */
TI M_Cmd( TI M4, ENABLE) ;
}
And the main program:
i nt mai n( voi d)
{
RCC_Conf i gur at i on( ) ;
GPI O_Conf i gur at i on( ) ;

whi l e( 1) / * I nf i ni t e l oop */
{
TI M4_Conf i gur at i on_l ef t ( ) ;
del ay( ) ;
TI M4_Conf i gur at i on_r i ght ( ) ;
del ay( ) ;
}
}
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: 4/16/2014 7:38 PM View Properties/Attachments Reply
clive1
Posts :
8781
The total reconfiguration seems rather unnecessary.
Why wouldn't you simply reprogramthe 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.



















Forum- [HELP] STM32F4 generatePWM 3 cha... - STMicroelectronics
https://my.st.com/...32F4%20generate%20PWM%203%20channel%20shift%20phase%20120%20degree!&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=59[23/04/2014 18:00:48]














All rights reserved 2014 STMicroelectronics Terms of use Privacy Policy Follow Us

Anda mungkin juga menyukai