cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Output on STM32F2XX (Solved)

th2
Associate
Posted on February 02, 2012 at 14:30

Greetings,

I am currently toying around with the KEIL MCBSTM32F200 Evaluation Board

(the STM32F207VG uC) and have run into some problems with the PWM output.

I want a simple PWM-signal on TIM1, Channel1 (PA8 for STM32F205&207) using

the standard peripheral library for STM32Fxx.

The code below is a mix from the different examples from the standard

peripheral library examples for PWM, and I'm sure something critical is

missing.

After initializing, all TIM1 registers looks ''ok'' (as far as I can tell, the

timer is a bit too advanced for me...) and the TIM1_CNT register is counting

up and resetting.

When connecting the oscilloscope to PA8, all I get is a steady 3.3V signal,

no PWM.

What am I missing here? It's quite frustrating since all other peripherals

I'm using are working fine. Thanks in advance.

(code also at

http://pastebin.com/yUmZbXUL

: http://pastebin.com/yUmZbXUL)

&sharpinclude ''stm32f2xx.h''

&sharpinclude ''stm32f2xx_gpio.h''

&sharpinclude ''stm32f2xx_rcc.h''

&sharpinclude ''stm32f2xx_tim.h''

&sharpinclude ''system_stm32f2xx.h''

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

void main(void) {

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

uint16_t period;

uint16_t pulse;

/* Initialize clock - defined in system_stm32f2xx.h */

SystemInit();

/* Compute the value for the ARR register to have a period of 20 KHz */

period = (SystemCoreClock / 20000 ) - 1;

/* Compute the CCR1 value to generate a PWN signal with 50% duty cycle */

pulse = (uint16_t) (((uint32_t) 5 * (period - 1)) / 10);

/* GPIOA clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* Initialize PA8, Alternative Function, 100Mhz, Output, Push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

/* TIM1 clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

/* Timer Base configuration */

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = period;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

/* Channel 1 output configuration */

TIM_OCStructInit(&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = pulse;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);

/* TIM1 counter enable */

TIM_Cmd(TIM1, ENABLE);

/* TIM1 Main Output Enable */

TIM_CtrlPWMOutputs(TIM1, ENABLE);

/* forever... */

while (1) {

__asm(''nop'');

}

}

#stm32f2xx-pwm-tim1
10 REPLIES 10
Posted on February 02, 2012 at 18:22

Your duty computation seems overly complicated. I'd compute the period value, program the period-1 into the timebase, and period/2 for 50% into the the pulse.

The SystemInit() stuff is called by the C startup code prior to main(), at least in my environment. I don't have your board/chip. Here is an example for TIM4, Channel 1, out of PD.12 on a STM32F4-Discovery. Doing this on a STM32F2 should be a matter of changing the clock prescaler computation.

// STM32 PWM STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
}
/**************************************************************************************/
void TIM4_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = 1000000 / 20000; // 20 KHz for 1MHz prescaled
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = ((SystemCoreClock / 1000000) / 2) - 1; // Get clock to 1 MHz on STM32F4
// TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // Get clock to 1 MHz on STM32F2
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Enable TIM4 Preload register on ARR */
TIM_ARRPreloadConfig(TIM4, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PD.12 */
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM4_Configuration();
while(1); // Don't wait to exit
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 02, 2012 at 18:35

This should be a workable TIM1 CH1 PA.08 implementation

// STM32 PWM (TIM1 CH1 PA.08) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
}
/**************************************************************************************/
void TIM1_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = 1000000 / 20000; // 20 KHz for 1MHz prescaled
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // Get clock to 1 MHz on STM32F2/F4
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Enable TIM1 Preload register on ARR */
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PA.08 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
th2
Associate
Posted on February 03, 2012 at 11:35

Thanks for the reply! Problem solved :)

The missing function call is:

TIM_OCxPreloadConfig(TIMx, TIM_OCPreload_Enable)

Buried in stm32f2xx_tim.c there is a note:

Note2: In case of PWM mode, this function is mandatory:<

br

> TIM_OCxPreloadConfig(TIMx, TIM_OCPreload_ENABLE);

However, in the 7PWM_Output example for the library, this function is not called (and there is no PWM output on any pins). Also, the word ENABLE should not be upper-case.

For posterity, a fully functional snippet for a single PWM on PA8, TIM1 (with a bit

too complicated period/pulse calculation):

#include ''stm32f2xx.h''

#include ''stm32f2xx_gpio.h''

#include ''stm32f2xx_rcc.h''

#include ''stm32f2xx_tim.h''

#include ''system_stm32f2xx.h''

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

void main(void) {

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

uint16_t period;

uint16_t pulse;

/* Initialize clock - defined in system_stm32f2xx.h */

/* May not be needed */

SystemInit();

/* Compute the value for the ARR register to have a period of 20 KHz */

period = (SystemCoreClock / 20000 ) - 1;

/* Compute the CCR1 value to generate a PWN signal with 50% duty cycle */

pulse = (uint16_t) (((uint32_t) 5 * (period - 1)) / 10);

/* GPIOA clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* Initialize PA8, Alternative Function, 100Mhz, Output, Push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

/* TIM1 clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

/* Timer Base configuration */

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = period;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

/* Channel 1 output configuration */

TIM_OCStructInit(&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = pulse;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);

 

/* Very much needed. Enable Preload register on CCR1. */

TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

 

/* TIM1 counter enable */

TIM_Cmd(TIM1, ENABLE);

/* TIM1 Main Output Enable */

TIM_CtrlPWMOutputs(TIM1, ENABLE);

/* forever... */

while

(1) {

__asm(

''nop''

);

}

}

pentti
Associate II
Posted on February 16, 2012 at 08:10

Hi Tomas,

have you tried to using a different pin?  I tried your's code and it works, but when I changed the Pin_8 to Pin_7 it doesn't work.

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; -> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); -> GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1);

And of course I tried also other Pins 12 and 10.  Same case: only pin 8 works.  I don't understand it.  Could you test your's board, please?

pentti
Associate II
Posted on February 16, 2012 at 11:39

Hi Tomas,

forgot about my previous message.  I didn't understand how ports and pins mapping together with timers.

dennisvanelteren
Associate II
Posted on March 22, 2014 at 10:00

Hi Clive1

I'm replying to your very old post because I have a problem with my stm32f4discoveryboard (407vt chip onboard) specific with TIM4. It is mapped to PD11..14. Whatever I try I can not get PWM signals on the 4 channels.

I found your code, it looks almost like mine but I copied it anyway. I get some strange 50Hz like noise signal on my scope and superimposed on it I see (using your code) a very tiny 500mV PWM wave. Now when I configure TIM3 for PWM (same mode pwm1) I get nice pwm signals.

I'm totally puzzled about it....

Do you perhaps have an idea or suggestion what this might cause? The discoveryboard has 500ohm resistors with a LED connected to ground but that cant be it right?

Cheers

Dennis

Posted on March 22, 2014 at 16:21

Yeah, that doesn't sound right, but I don't have a scope to hand right now.

I did build a 4-channel servo demo for the STM32F4-DISCO using the LED pins, you could try that.

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/%5bSTM32f4discovery%5dSome%20clarification%20about%20PWM&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eugenia
Associate II
Posted on July 18, 2016 at 17:10

I have the exact same issue. I have followed your correction steps but I still get just a 3.3V ouput no PWM.  Using stm32f207 Pin E9 and Timer1.  Any suggestions?

Posted on July 18, 2016 at 17:32

Can we please STOP cross-posting into multiple old threads. You should have opened a new one initially.

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