cancel
Showing results for 
Search instead for 
Did you mean: 

Stop PWM output after N steps

harry_rostovtsev
Associate II
Posted on August 27, 2012 at 20:01

Is there a way to tell a TIMer on STM32 chips to do some number of steps and automatically stop after finished (and give an interrupt)?  I can do it now with a counter in the ISR but a few pulses slip out before I can shutdown the PWM output or the timer.

11 REPLIES 11
barry
Associate II
Posted on February 08, 2015 at 17:46

Thanks again Clive.

One question,  I can't figure out what one instruction will start the pulse sequence.

I don't want to start the pulses immediately at initialization  time and I want to repeat the sequence over and over.

The code shown below works, but I think I could clean it up if I understood the timers as well as you.

I am using the update interrupt to get additional cycles of 200 pulses

 /* Enable the TIM8 gloabal Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 /* TIM Interrupts enable */

  TIM_ITConfig(TIM8, TIM_IT_Update , ENABLE);

  

................

//Interrupt Handler

void   TIM8_UP_TIM13_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)

  {

    TIM_ClearITPendingBit(TIM8, TIM_IT_Update);

    tim8_counter--;

    if ( tim8_counter != 0)

    {

         TIM8_Restart();

    }

  }

}

 

  

 

void TIM8_Restart(void)  // This seems to work

{

  /* TIM8 Main Output Enable */

  TIM_CtrlPWMOutputs(TIM8, ENABLE);

   /* TIM8 enable counter */

  TIM_Cmd(TIM8, ENABLE);

}

 

//This is what I do the first time and when I want to send another series of 200 pulses

// Some of these instructions were removed from the init code you posted

     tim8_counter = TIM8_INTERRUPTS;

    TIM_CtrlPWMOutputs(TIM8, ENABLE);

    TIM_Cmd(TIM8, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

    TIM8_Restart();

Any ideas on what is actually turn the pulses off and on?

Thanks

Barry

barry
Associate II
Posted on October 22, 2015 at 04:02

I have this code running correctly for Timer 8 channel 1.  When I try to use this code for other channels on Timer 8, I run into problems.

Channel 2n  (complementary) does not work (i.e. no PWM output)

Channel 3 continually outputs pulses

All I have changed is the GPIO pin configuration

and I use OCxInit for each channel.

Does anyone know what else is channel dependent.  

Thanks