AnsweredAssumed Answered

F0discovery PWM with external clock and trigger

Question asked by padbury.chris on Nov 13, 2012
Latest reply on Dec 12, 2012 by voisey.rob
I want to create a simple PWM implementation which uses an external clock source and is synchronised to a trigger pulse (one pulse per PWM period). I need at least three (ideally four) channels of output with different duty cycles but all synchronised.

I have started by using the TIM_PWM_Output example in the firmware package. This provides the four outputs I want. What I am unclear on is how to set up the external clock source and which pin should be used for that.

The code uses TIM1. I see my first hurdle as using an external clock source, then I can try to add the external trigger. Here is my code as it looks copied from the TIM_PWM_Output example:

#include "stm32f0_discovery.h"
  
int Set_PWM_Period(int period);
int Set_PWM_Duty_Cycle(int duty_cycle_1, int duty_cycle_2, int duty_cycle_3, int duty_cycle_4);
int Synchronise_To_Row_Clock(void);
int TIM_Config(void);
  
int Set_PWM_Period(int period) {
  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  uint16_t TimerPeriod = 0;
  uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;  
      
  /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
  TimerPeriod = (SystemCoreClock / 17570 ) - 1;
  TimerPeriod = period;  //<-- ************************************************************************
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
  Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 */
  Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
  /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 */
  Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
  /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
  
  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , 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(TIM1, &TIM_TimeBaseStructure);
  
  /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  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_OCInitStructure.TIM_Pulse = Channel1Pulse;
  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);
  
  TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
  TIM_OC4Init(TIM1, &TIM_OCInitStructure);
  
  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);
  
  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
  
    return 0;
}
  
int Set_PWM_Duty_Cycle(int duty_cycle_1, int duty_cycle_2, int duty_cycle_3, int duty_cycle_4) {
    return 0;
}
  
int Synchronise_To_Trigger_Pulse(void) {
    return 0;
}
  
/**
  * @brief  Configure the TIM1 Pins.
  * @param  None
  * @retval None
  */
int TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* GPIOA Clocks enable */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
    
  /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  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_PinSource8, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
  
  return 0;
}
  
  
int main(void) {
    int PWM_period, PWM_duty_cycle;
    TIM_Config();
  
    PWM_period = 1000;   //manual setting of PWM_period
  
    Set_PWM_Period(PWM_period);
      
    //PWM_duty_cycle_1 = 500;   //manually set PWM_duty_cycle for test
    //PWM_duty_cycle_2 = 375;   //manually set PWM_duty_cycle for test
    //PWM_duty_cycle_3 = 250;   //manually set PWM_duty_cycle for test
    //PWM_duty_cycle_4 = 125;   //manually set PWM_duty_cycle for test
  
    //Set_PWM_Duty_Cycle(PWM_duty_cycle_1, PWM_duty_cycle_2, PWM_duty_cycle_3, PWM_duty_cycle_4);
  
    //Synchronise_To_Trigger_Pulse();   // trigger for sync
  
    while(1);
  
}

Any help gratefully received.

Outcomes