cancel
Showing results for 
Search instead for 
Did you mean: 

Here's my example code of how to use PWM in stm32F4 Discovery leds ( green, orange, red & blue )

jvilaca
Associate II
Posted on April 06, 2012 at 18:41

Hi hope this example is useful for anyone else in here. Code is adapted from one of the examples on TIM/PWM:

/* leds in the board will fade */

int main(void)

{

   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

   TIM_OCInitTypeDef  TIM_OCInitStructure;

  /* TIM config */

  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM4 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* LEDs are on GPIOD */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  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(GPIOD, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);

  /* pwm set up */

  /* Compute the prescaler value */

  PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 21000000) - 1;

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 665;

  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel2 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0;

  TIM_OC2Init(TIM4, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel3 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0;

  TIM_OC3Init(TIM4, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 0;

  TIM_OC4Init(TIM4, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM4, ENABLE);

  /* TIM4 enable counter */

  TIM_Cmd(TIM4, ENABLE);

  /* DEMO */

      int brightness = 0;

  while(1)  // Do not exit

    {

          brightness++;

          TIM4->CCR3 = 333 - (brightness + 0) % 333; // set brightness

          TIM4->CCR4 = 333 - (brightness + 166/2) % 333; // set brightness

          TIM4->CCR1 = 333 - (brightness + 333/2) % 333; // set brightness

          TIM4->CCR2 = 333 - (brightness + 499/2) % 333; // set brightness

          for(i=0;i<10000;i++);  // delay

          for(i=0;i<10000;i++);  // delay

          for(i=0;i<10000;i++);  // delay

    }

}

#stm32f4-pwm-led
4 REPLIES 4
0owarrioro0
Associate II
Posted on April 19, 2012 at 19:36

haha..lu guo yi hou xue xi  ,xie xie fen xiang !!!

bmwael1
Associate II
Posted on December 20, 2012 at 11:33

What is this operator '%' (brightness + 0) % 333 ?

if

brightness

= 1     =>

(brightness + 0) % 333 = 1

if

brightness

= 332 =>

(brightness + 0) % 333 = 332

if

brightness

= 333 =>

(brightness + 0) % 333 = 0

if

brightness

= 334 =>

(brightness + 0) % 333 = 1

if

brightness

= 665 =>

(brightness + 0) % 333 = 332

if

brightness

= 666 =>

(brightness + 0) % 333 =

0

if

brightness

= 667 =>

(brightness + 0) % 333 =

1

Posted on December 20, 2012 at 13:34

What is this operator '%'

That would be the C language's modulus (remainder) operator.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sandyxiongjing
Associate II
Posted on December 27, 2012 at 03:40

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=0680X000006I6hh&d=%2Fa%2F0X0000000btb%2Ft3X3j5hv7zf46qJiW0x363Kn4rpK1QPuqEwtctt3tFc&asPdf=false