cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3Discovery and PWM

opiswahn
Associate II
Posted on October 04, 2013 at 22:58

Hi,

i've been looking around for a solution to this for some days now, but nothing, maybe someone of you can help me. I have the following code:

TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
TIM_OCInitTypeDef TIM_OC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = 20000 - 1; // 20000
TIM_TimeBase_InitStructure.TIM_Prescaler = 72 - 1; // 72
TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC_InitStructure.TIM_Pulse = BaseValue + CCR2_Val; //min = 500, max = 2500, BASE = 600
TIM_OC1Init(TIM1, &TIM_OC_InitStructure);
TIM_OC1PreloadConfig (TIM1, TIM_OCPreload_Disable);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);

with normal GPIO-Initialization of Pin PA8 as GPIO_Mode_AF and

GPIO_PinAFConfig.

There are 2 Problems with that: First, I have no output current on Pin PA8. I have an almost similar example for the STM32F0Discovery, which works fine, but on the F3 nothing... i simply can't see why. Second, what i'm thying to do is provide a PWM Signal for a BLDC, wich doesn't work, not on the F3(clearly ;) ) and not on the F0, which at least seems to give some kind of output. I've searched the Net up and down for some examples or hints, but everything i find looks almost like the code above. Can PLEASE someone of you guys help me?? Greets, **Jürgen** #stm32 #discovery #stm32f3discovery #selective-cut-n-paste
6 REPLIES 6
Posted on October 04, 2013 at 23:13

i simply can't see why.

Probably because you're not looking at the right code, try again with a more complete example.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
opiswahn
Associate II
Posted on October 04, 2013 at 23:37

This is my working F0-Code: (well working in terms of having some kind of output, but nothing that drives a BLDC)

void PWM_Demo::Init()
{
/* GPIO Configuration */
Config();
/* TIM1 Configuration */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Prescaler = 48 - 1;
TIM_TimeBaseStructure.TIM_Period = 20000 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
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_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Wichtig
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 1100;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 1600;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 2100;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void PWM_Demo::Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Clocks enable */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
/* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
}

and here my F3 code: (no output in Pin whatsoever)

void Pin::Initialize(GPIOMode_TypeDef mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(this->Periphery, ENABLE);
GPIO_InitStructure.GPIO_Pin = this->Number;
if(mode != GPIO_Mode_AN)
{
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
}
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = mode;
GPIO_Init(this->GPIOx, &GPIO_InitStructure);
GPIO_ResetBits(this->GPIOx, this->Number); // nötig?
switch(mode)
{
case GPIO_Mode_AN:
InitializeADC();
break;
case GPIO_Mode_AF:
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_12);
InitializePWM();
break;
}
}
void Pin::InitializePWM()
{
TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
TIM_OCInitTypeDef TIM_OC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = 20000 - 1; // 20000
TIM_TimeBase_InitStructure.TIM_Prescaler = 72 - 1; // 72
TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC_InitStructure.TIM_Pulse = BaseValue + CCR2_Val; //min = 500, max = 2500, BASE = 600
TIM_OC1Init(TIM1, &TIM_OC_InitStructure);
TIM_OC1PreloadConfig (TIM1, TIM_OCPreload_Disable);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
STM_EVAL_LEDToggle(LED4);
CaptureValue = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1, BaseValue + CCR2_Val);
}
}

Posted on October 05, 2013 at 00:52

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_12);

AF6 not AF12
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
opiswahn
Associate II
Posted on October 05, 2013 at 02:44

ok... that worked, now i've got a signal similar to the one on the F0. THX!

But whats the Difference between 

#define

 

GPIO_AF_6

            ((

uint8_t

)0x06) 

/*  IR_OUT, I2S2, I2S3, SPI2, SPI3, TIM1, TIM8 */

 and 

#define

 

GPIO_AF_12

            ((

uint8_t

)0x0E) 

/* TIM1 */

or

#define

 

GPIO_AF_2

            ((

uint8_t

)0x02) 

/* COMP1_OUT, TIM1, TIM2, TIM3, TIM4, TIM8, TIM15 */

or any other GPIO_AF_X with TIM1 in it? There seems to be no other documentation like the one in the code, or i haven't found it yet.

But that still won't drive a brushless motor on a BLDC. Any ideas on that?

Posted on October 05, 2013 at 03:04

You would want the

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00058181.pdf

for your

http://www.st.com/web/catalog/mmc/FM141/SC1169/SS1576/LN1531/PF252054

0690X00000605VwQAI.png

I'm not a motor guy, you'll need to express your request as a specific signal output.

The[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/TIM4%20CH3%2c%20CH2%2c%20CH1%20now%20working&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=63] post immediately below this relates to driving servos on an STM32F3-Discovery
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
opiswahn
Associate II
Posted on October 05, 2013 at 13:29

Ah, thats the table i was expecting to find in the F3Discovery manual ;)

Thx dude!

Still no success with driving the BLDC. Should work with those numbers, but it doesn't. It's the right cycle-length of 20ms, tried the full range from 0,6ms - 2,4ms for the period, nothing. Kinda frustrating...