cancel
Showing results for 
Search instead for 
Did you mean: 

generate one pulse

kong0073
Associate II
Posted on July 08, 2011 at 06:18

I am trying to generate a single pulse using the TIM_SelectOnePulseMode() function in STM32f10x_tim.c source file. But I am not able to get the pulse. why?

I am using TIM4 CH2... Any advice?

int main(void)

{

    TIM_Cmd(TIM4, DISABLE);

    RCC_Configuration();

    GPIO_Configuration();

    PrescalerValue = (SystemCoreClock / 24000000) - 1;

   

    TIM_TimeBaseStructure.TIM_Period = 65535

    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 16383

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC2Init(TIM4, &TIM_OCInitStructure);

    TIM_Cmd(TIM4, ENABLE);

    TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Repetitive);

    return 0;

}

void RCC_Configuration(void)

{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

    RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

}

#timer-one-shot #one-pulse #stm32 #tim4
16 REPLIES 16
rosarium
Associate II
Posted on July 08, 2011 at 06:32

which STM32 you are using?? the exact part no??

kong0073
Associate II
Posted on July 08, 2011 at 07:26

i'm using STM32F100B-eval...

rosarium
Associate II
Posted on July 08, 2011 at 09:46

for one pulse you have to do

/* One Pulse Mode selection */

TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

but you are doing

TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Repetitive);

Please find here attached an example source code for generating one pulse. Hope it will help.

Rgds,

Rosarium

________________

Attachments :

OnePulse.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1D8&d=%2Fa%2F0X0000000bjt%2FDDx0jf4tKrrBNem0inHVER9iZcqPyjb2YZcYTDmAE5w&asPdf=false
rosarium
Associate II
Posted on July 08, 2011 at 09:56

TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);

kong0073
Associate II
Posted on July 08, 2011 at 10:13

thanks! but erm, it didn't matter if i used ''single'' or ''repetitive'', there was still no output at my TIM4. And is the input capture mode necessary? since I just need to output pulses...

rosarium
Associate II
Posted on July 08, 2011 at 10:33

Yes input capture is also necessary.

Following is an example for TIM2 as attached last time.

int main(void)

{

  /* System Clocks Configuration */

  RCC_Configuration();

  /* 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;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  /* TIM2 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  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_TI2FP2);

  /* Slave Mode selection: Trigger Mode */

  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);

  while (1)

  {}

}

Posted on July 08, 2011 at 13:54

I am trying to generate a single pulse using the TIM_SelectOnePulseMode() function in STM32f10x_tim.c source file. But I am not able to get the pulse. why?

 

I am using TIM4 CH2... Any advice?

 

Well for starters, to get TIM4_CH2 out of PD13, you are going to need to enable the AFIO peripheral and Remap the TIM4 to use those pins.

If your board/part isn't using PD13, then you'll need to enable the appropriate pin (PB7 ?)
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kong0073
Associate II
Posted on July 11, 2011 at 04:22

ok, now I have changed to TIM2 CH1 (PA0)  which coincides with the wakeup.

I have enabled my AFIO and re-mapped my TIM2 CH1, but I am still not getting it. Why?

here's my code.

//  ******************************************************************************

 

#include ''stm32f10x.h''

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

void RCC_Configuration(void);

void GPIO_Configuration(void);

int main(void)

{

  RCC_Configuration();

  GPIO_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);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 16383;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  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);

  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);

  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);

  while (1)

  {}

}

void RCC_Configuration(void)

{

  SystemInit();

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);

}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

  while (1)

  {}

}

#endif

kong0073
Associate II
Posted on July 11, 2011 at 04:33

hi, i tried copy-and-paste your code to see if works, but i'm still not getting a pulse.

and i received another reply from another user that I should enable AFIO and remap, so please see my code below:

#include ''stm32f10x.h''

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

void RCC_Configuration(void);

void GPIO_Configuration(void);

int main(void)

{

  RCC_Configuration();

  GPIO_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);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 16383;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  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);

  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);

  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);

  while (1)

  {}

}

void RCC_Configuration(void)

{

  SystemInit();

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

}

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);

}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

  while (1)

  {}

}

#endif

---------------------------------------------------------------------------------

what am i doing wrong here? i can't seem to get the code no matter how i try. please help... much appreciated.