AnsweredAssumed Answered

stm32f0 discovery board to run One Pulse Mode on timer 2

Question asked by zhang.ming.002 on Jul 9, 2015
Latest reply on Jul 10, 2015 by zhang.ming.002
Hi, I am using the discovery board to try OPM on timer 2. I can not get it going with any effort on my code list below. Please help!!! It is important to our new product.

void GPIO_Configuration(void);


int main(void)
{
  /* System Clocks Configuration */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOC, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);


  /* Configure the GPIO ports */
  GPIO_Configuration();


  /* TIM2 configuration: One Pulse mode ------------------------
     The external signal is connected to TIM2_CH2 pin (PA.01),
     The Rising edge is used as active edge,
     The One Pulse signal is output on TIM2_CH1 pin (PA.00)
     The TIM_Pulse defines the delay value
     The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
     The TIM2CLK is fixed to 72 MHz, the Prescaler is 1, so the
     TIM2 counter clock is 36 MHz.
     The Autoreload value is 65535 (TIM2->ARR), so the maximum
     frequency value to trigger the TIM2 input is 500 Hz.
     The TIM_Pulse defines the delay value, the delay value is fixed
     to 455.08 us:
     delay =  CCR1/TIM2 counter clock = 455.08 us.
     The (TIM_Period - TIM_Pulse) defines the One Pulse value,
     the pulse value is fixed to 1.365ms:
     One Pulse value = (TIM_Period - TIM_Pulse)/TIM2 counter clock = 1.365 ms.
  ------------------------------------------------------------ */


  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;


  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


  /* TIM2 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 16383;   //delay..
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;


  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  //PA1 output.


  /* TIM2 PWM2 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 1; //No delay, immediate reacts to input.
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;


  TIM_OC3Init(TIM2, &TIM_OCInitStructure);   //PA2 output.


  /* TIM2 configuration in Input Capture Mode */


  TIM_ICStructInit(&TIM_ICInitStructure);


  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //PA0 trigger
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;


  TIM_ICInit(TIM2, &TIM_ICInitStructure);


  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);


  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);


  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);


  //GPIO_SetBits(GPIOA,GPIO_Pin_1);


  while (1)
  {
       GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9); //Turn 2 leds on.
  }
}


///////////////////////////////////////////////////
void GPIO_Configuration(void)
{
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  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_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //PC8,9 to be output pp.
  GPIO_Init(GPIOC, &GPIO_InitStructure);


  GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_6);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_6);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //PA2 to be alternative func.
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 to be alternative func.
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);




}


Outcomes