AnsweredAssumed Answered

Pb generating 2 PMW

Question asked by Femol on Nov 21, 2014
Latest reply on Nov 21, 2014 by Femol
Hello,
I want to generate 2 PWM signal on Tim15 and on Tim2, at respectively 1MHz and 4MHz with a 50% duty cycle.
My code is the following one:
/* Includes ------------------------------------------------------------------*/
#include "stm32f30x.h"

/* Private function prototypes -----------------------------------------------*/
static void TIM_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f30x.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */

  /* TIM Configuration */
  TIM_Config();
 
  /* Infinite loop */
  while (1)
  {
  }
}

/**
  * @brief  Configure the TIM15 and TIM2.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  uint16_t TimerPeriod = 0;
  uint16_t Channel1Pulse = 0, Channel3Pulse = 0;

  /* GPIOA & GPIOB Clocks enable */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
 
  /* GPIOA & GPIOB Configuration: Channel 1 (TIM15) and Channel 3 (TIM2) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9  | GPIO_Pin_14;
  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_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_6);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_6);
      
      /* Tim15 and Tim2 Configuration ---------------------------------------------------
   Generate 2 PWM signals :
   Tim15 input clock (Tim15CLK) is set to APB2 clock (PCLK2)
     Tim2 input clock (Tim2CLK) is set to APB1 clock (PCLK1)
    => Tim15CLK = PCLK2 = SystemCoreClock
        => Tim2CLK = PCLK1 = SystemCoreClock
        
   Tim15CLK = SystemCoreClock, Prescaler = 0, Tim15 counter clock = SystemCoreClock
     Tim2CLK = SystemCoreClock, Prescaler = 0, Tim2 counter clock = SystemCoreClock  
   SystemCoreClock is set to 72 MHz for STM32F30x devices
   
   The objective is to generate 2 PWM signal at 1MHz for Tim15 and at 4MHz for Tim2:
     - Tim15_Period = (SystemCoreClock / 1000000) - 1
     - Tim2_Period = (SystemCoreClock / 4000000) - 1        
   The channel 1 and channel 3 duty cycle are set to 50%
   The Timer pulse is calculated as follows:
     - Channel1Pulse = DutyCycle * (Tim15_Period - 1) / 100
     - Channel3Pulse = DutyCycle * (Tim2_Period - 1) / 100        
   
   Note:
    SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f30x.c file.
    Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
    function to update SystemCoreClock variable value. Otherwise, any configuration
    based on this variable will be incorrect.
  ----------------------------------------------------------------------- */
  /* Compute the value to be set in ARR regiter to generate signal frequency at 1MHz */
  TimerPeriod = (SystemCoreClock / 1000000 ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
  Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
 
  /* Tim15 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15 , ENABLE);
 
  /* 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(TIM15, &TIM_TimeBaseStructure);

  /* Channel 1Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OC1Init(TIM15, &TIM_OCInitStructure);

    /* Tim1 counter enable */
  TIM_Cmd(TIM15, ENABLE);

  /* Tim1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM15, ENABLE);
/*----------------------------------------------------------------------- */
  /* Compute the value to be set in ARR regiter to generate signal frequency at 4MHz */
  TimerPeriod = (SystemCoreClock / 4000000 ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 3 */
  Channel3Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
 
  /* Tim2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
 
  /* 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(TIM2, &TIM_TimeBaseStructure);

  /* Channel 1Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

    /* Tim1 counter enable */
  TIM_Cmd(TIM2, ENABLE);

  /* Tim1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM2, ENABLE);
    
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/*****************************END OF FILE****/
Attached is what at obtained on scope at pins PB9 (yellow trace) and PA9(green trace).
Where are my mistakes?
Thanks for your help on the subject

Outcomes