cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f100 pwm duty cycle

thierry_baptiste
Associate II
Posted on March 10, 2016 at 19:21

I would like use the stm32f100 for buck loop control.

In first, I reach to configure the timer and gpio to get a fixe PWM.

But I don't know, for the moment, adjust or modify the duty cycle with a command. Before I use arduino, and it wa svery easy just with a command digitalwrite(pin, value).

Thank you if anybody can help me.
9 REPLIES 9
Posted on March 10, 2016 at 19:34

int Period = 5000;

int DutyPercent = 50; // Percentage 0 to 100, 0 Always Off, 100 Always On

// Quick configuration

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_Period = Period - 1;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* TIM PWM1 Mode configuration */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

  /* Output Compare PWM1 Mode configuration: Channel1 */

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

    /* TIM1 Main Output Enable */

    TIM_CtrlPWMOutputs(TIM1, ENABLE);

  /* TIM1 enable counter */

  TIM_Cmd(TIM1, ENABLE);

// Then all require to change duty is

TIM1->CCR1 = (Period * DutyPercent) / 100;

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

Thank you clive1 for your fast reply. I 'll try tomorrow and give you informed as soon as possible.

thierry_baptiste
Associate II
Posted on March 11, 2016 at 08:22

Dear clive1,

it works fine.

thank

thierry_baptiste
Associate II
Posted on March 11, 2016 at 10:21

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6jg&d=%2Fa%2F0X0000000buH%2FSR8KyRqt1GO4KluTKlX8GEICZzhq5sQx31H8Xn4EMFc&asPdf=false
Posted on March 11, 2016 at 12:55

But I'm still stopped by an error of compilation.

Ok, the error says what exactly? The error itself usually provides some specificity. If it is complaining about library functions being missing, those would need to be added to the project/workspace.

Not a big CooCox fan, but standard rules of C should permit you to break things into subroutines, or migrate them into separate files

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thierry_baptiste
Associate II
Posted on March 11, 2016 at 13:30

Hi clive1,

what type of software you use ? free software ?

the errors are :

error: 'ADC_InitStructure_InitStructure' undeclared (first use in this function)

error: 'ADC_InitTypeDef' has no member named 'ADC_ScanMode'

error: 'ADC_InitTypeDef' has no member named 'ADC_continuousConvMode'

thierry_baptiste
Associate II
Posted on March 11, 2016 at 14:06

My troubles are syntax error

the lines involved and corrected are :

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent ;

    ADC_InitStructure.ADC_ScanConvMode = DISABLE;

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

    ADC_InitStructure.ADC_ExternalTrigConv= ADC_ExternalTrigConv_None ;

  From: baptiste.thierry

Posted: Friday, March 11, 2016 1:30 PM

Subject: stm32f100 pwm duty cycle

Hi clive1,

what type of software you use ? free software ?

the errors are :

error: 'ADC_InitStructure_InitStructure' undeclared (first use in this function)

error: 'ADC_InitTypeDef' has no member named 'ADC_ScanMode'

error: 'ADC_InitTypeDef' has no member named 'ADC_continuousConvMode'

Posted on March 11, 2016 at 14:52

what type of software you use ? free software ?

Working properly tends to rank higher on my scale. I mainly use Keil, but use GNU/GCC via makefiles, where appropriate. Rowley is a reasonably priced tool chain.

The SPL really needs you to have some defines passed to the compiler's command line, and the peripheral include files usually get pulled via stm32f1xx_conf.h

Defines like

USE_STDPERIPH_DRIVER, STM32F10X_HD_VL

See also

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thierry_baptiste
Associate II
Posted on March 11, 2016 at 16:47

Thank you clive1 for your help.

I had a look on the Keil website, software  looks interresting.

I'm a beginner in sofware, so I'll certainly comeback in a short delay to make a sos software...

Thierry