AnsweredAssumed Answered

PWM Output

Question asked by kim.andy on Jul 5, 2013
Latest reply on Jul 6, 2013 by kim.andy
Hello.

I want to use 3 channels for pwm output, but I have checked that only 1 channel(Channel 1) outputs the pwm signal, 2 channels(Channel 2/3) don't output pwm signals.

Anybody could check why does this problem happen in below code.

void bsp_init_timer1(void)
{
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

 /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
 TimerPeriod = (SystemCoreClock / 17570 ) - 1;
 /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
 Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
 /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
 Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
 /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
 Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);

 /* Time Base configuration */
 TIM_TimeBaseStructure.TIM_Prescaler = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 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_PWM2;
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
 TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

 TIM_OC1Init(TIM1, &TIM_OCInitStructure);

 TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
 TIM_OC2Init(TIM1, &TIM_OCInitStructure);

 TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
 TIM_OC3Init(TIM1, &TIM_OCInitStructure);

 /* TIM1 counter enable */
 TIM_Cmd(TIM1, ENABLE);

 /* TIM1 Main Output Enable */
 TIM_CtrlPWMOutputs(TIM1, ENABLE);

 while (1)
 {}
}

void bsp_init_gpio(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure the GPIO ports( leds ) */
 GPIO_InitStructure.GPIO_Pin =  LED_USER_PIN;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(LED_USER_PORT, &GPIO_InitStructure); 

 /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically
  connected to the DAC converter. In order to avoid parasitic consumption,
  the GPIO pin should be configured in analog */
 GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* GPIOA Configuration(PWM) : Channel 1, 2 and 3 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_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* GPIOB Configuration(PWM) : Channel 1N, 2N and 3N as alternate function push-pull */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;      //pin15 : RB
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;      //pin14 : INT
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void bsp_init_rcc(void)

 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/  
 /* RCC system reset(for debug purpose) */
 RCC_DeInit();

 /* Enable HSE */
 RCC_HSEConfig(RCC_HSE_ON);

 /* Wait till HSE is ready */
 HSEStartUpStatus = RCC_WaitForHSEStartUp();

 if (HSEStartUpStatus == SUCCESS)
 {
  /* Enable Prefetch Buffer */
  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  /* Flash 2 wait state */
  FLASH_SetLatency(FLASH_Latency_2);

  /* HCLK = SYSCLK */
  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  /* PCLK2 = HCLK */
  RCC_PCLK2Config(RCC_HCLK_Div1);
  /* PCLK2 = HCLK/2 */
//  RCC_PCLK2Config(RCC_HCLK_Div2);

  /* PCLK1 = HCLK/2 */
  RCC_PCLK1Config(RCC_HCLK_Div2);

#if defined( STM32F10X_CL )
  /* Configure PLLs *********************************************************/
  /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
  RCC_PREDIV2Config(RCC_PREDIV2_Div5);
  RCC_PLL2Config(RCC_PLL2Mul_8);

  /* Enable PLL2 */
  RCC_PLL2Cmd(ENABLE);

  /* Wait till PLL2 is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
  {}

 #if defined (HSE_8MHZ)
  /* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
  RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);  
  RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
 #endif
#else

 #if defined (HSE_12MHZ)
  /* PLLCLK = 12MHz * 6 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
 #elif defined( HSE_16MHZ )
  /* PLLCLK = 16MHz /2 * 9 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
 #else  // 8MHZ  
  /* PLLCLK = 8MHz * 9 = 72 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
 #endif
#endif

  /* Enable PLL */
  RCC_PLLCmd(ENABLE);

  /* Wait till PLL is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  {
  }

  /* Select PLL as system clock source */
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Wait till PLL is used as system clock source */
  while(RCC_GetSYSCLKSource() != 0x08)
  {
  }
  }
  else
  { /* If HSE fails to start-up, the application will have wrong clock configuration.
     User can add here some code to deal with this error */   

  /* Go to infinite loop */
  while (1)
  {
  }
 }
 
 /* enable the PWR clock */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);

 /* USART1 Periph clock enable */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

 /* TIM1 Periph clock enable */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

 /* TIM2 clock enable */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 

 /* DAC Periph clock enable */      
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

}

Please help me.


Outcomes