cancel
Showing results for 
Search instead for 
Did you mean: 

Timer help

pedahl
Associate II

Hi,

i have a STM32F303RC and i have the following problem:

With the Pin A2 i want to produce a sequence of 10 pulses.

My attempt is to use a PWM TIMER Channel (TIM2 CH3) in one pulse Mode and retrigger it till i have generated my 10 Steps.

However, my code only generates 2 pulses.ow can i rearm the Timer correctly?

Does anyone know an easier way to solve the problem?

 

void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim) 
{ 
  if(htim->Instance==htim2.Instance) 
  { 
    __HAL_TIM_SetCounter(&htim2,0); 
    p++; 
  } 
} 

int main(void) 
{ 
  MX_TIM2_Init(); 
  HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_3); 
  HAL_StatusTypeDef status= HAL_TIM_OnePulse_Start(&htim2, TIM_CHANNEL_3); 
  if(status != HAL_OK) {} 
  while(p<10) 
  { 
    while(htim2.State != HAL_TIM_STATE_READY) { } 
  } 
   finished=1; 
}

 

5 REPLIES 5

> and retrigger it

Where? How? Do you think __HAL_TIM_SetCounter means "retrigger"?

Normally, you would start a timer in one-pulse mode by setting TIM2->CR1 |= TIM_CR1_CEN; surely Cube/HAL has some incantation doing this. I don't use Cube; but it is open source, you can find it easily in its sources yourself.

JW

Advanced timers like TIM1 and TIM8 also have a repetition count they can apply to one-shot mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The Problem is that i can only use TIM2...

pedahl
Associate II

My original Code was:

 

void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim) 
{ 
  if(htim->Instance==htim2.Instance) 
  { 
    HAL_TIM_OnePulse_Stop(htim, TIM_CHANNEL_3);
    HAL_TIM_PWM_Stop_IT(htim, TIM_CHANNEL_3);  
    p++; 
  } 
} 

int main(void) 
{ 
  MX_TIM2_Init(); 
  if(status != HAL_OK) {} 
  while(p<10) 
  {
    HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_3); 
    HAL_StatusTypeDef status= HAL_TIM_OnePulse_Start(&htim2, TIM_CHANNEL_3);  
    while(htim2.State != HAL_TIM_STATE_READY) { } 
  } 
   finished=1; 
}

 

I had entered this code but the timer was not rearmed.

Now i have step in the code from HAL_TIM_OnePulse_Stop()... I think it might be a bug (Reset only the Channel 1 and Channel 2) :

HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(OutputChannel);

  /* Disable the TIM Capture/Compare 1 interrupt */
  __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);

  /* Disable the TIM Capture/Compare 2 interrupt */
  __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);

  /* Disable the Capture compare and the Input Capture channels
  (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);

  if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  {
    /* Disable the Main Output */
    __HAL_TIM_MOE_DISABLE(htim);
  }

  /* Disable the Peripheral */
  __HAL_TIM_DISABLE(htim);

  /* Set the TIM channels state */
  TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  TIM_CHANNEL_N_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);

  /* Return function status */
  return HAL_OK;

 

 

That's unfortunate. Things to consider when designing Rev 2

But still I have no idea of the frequency or pulse widths desired.

For relatively low frequencies, say couple hundred KHz, interrupts can work, but higher frequencies the interrupt latency predominates.

Fast sequences could perhaps be serviced by DMA rather than IRQ's

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..