cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with timer interrupt routines

jamsoft
Associate II
Posted on April 14, 2013 at 12:25

Hello all,

I've run into the following problem with the program execution.

On the STM32f4 Discovery board I've configured two timers with the IRQ routines for measuring two external PWM signals. This works excellently...but...

In the main function (within the while block) I need to make some other things (e.g. reading LIS302DL accelerometer values) but only IRQ routines are executed and the while block takes place only after the PWM signal interruption. If PWM signal is not present, the accelerometer values are read well.

What can I do to execute all main function and to measure the PWM in the same time?

Have I to create another timer with interrupt routine for execution other parts of code?

Thank you for your answers in advance.

#interrupt #timer
7 REPLIES 7
Posted on April 14, 2013 at 14:51

The processor can only execute one thread at a time, if you spend too much time in the interrupt routine, loop endlessly, fail to leave, or fail to clear the interrupt, then the foreground loop will never execute.

Your description is all well and good, but the code for the interrupt routines would more illustrative.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jamsoft
Associate II
Posted on April 14, 2013 at 15:31

Clive, thank you for your answer.

Yes, it is true, that IRQ routines write output to the serial (and it is maybe time consuming)  - but this is what I need...

Also the sprintf function can be time-consuming.

Here is my IRQ routine code snippet:

void TIM1_CC_IRQHandler(void)

{

    char buffer[10];

    RCC_ClocksTypeDef RCC_Clocks;

  RCC_GetClocksFreq(&RCC_Clocks);

  /* Clear TIM1 Capture compare interrupt pending bit */

  TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

  /* Get the Input Capture value */

  IC2ValueT1 = TIM_GetCapture2(TIM1);

  if (IC2ValueT1 != 0)

  {

    DutyCycle = (TIM_GetCapture1(TIM1) * 100) / IC2ValueT1;

        

        distance = DutyCycle;

        sprintf(buffer, ''%f'', distance);

        

        //write the results to console

        USART_puts(USART1, ''SENSOR 1 - DUTY CYCLE: '');

        USART_puts(USART1, buffer);

        USART_puts(USART1, ''\r\n'');        

  }

  else

  {

    DutyCycle = 0;

  }        

}

An IRQ routine for the second timer is same...

What is the worst, the need is to capture 6 PWM signals (I planned using other channels of those two timmers within the same two IRQ routines) and still get informations from the accelerometer and do some computings with all of those values...

And that's not all...

There are another 2 timers running, each with 2 channels engaged - for 2 servos and 2 motors PWM controll signal generation...but they don't use IRQ routines.

I hope it doesn't means that I'll have to use separate CPU for each couple of sensors and one for final computation 🙂

Maybe the solution could be to stop all timers with IRQ routine when executing one of them and in the end of the while cycle (in the main function) start them again...

Another solution should be (possibly) to take into account the IRQ priority but I am not experienced with this...

Any idea how to solve this problem?

Thank you once more for your help.

jamsoft
Associate II
Posted on April 14, 2013 at 15:37

Oh, maybe you'll ask for what the ''distance = DutyCycle;'' command is...

It is prepared for performing another calculation there that is not implemented yet.

 

Posted on April 14, 2013 at 15:39

You would definitely want to get the sprintf and USART stuff out of the interrupt handler.

You could buffer USART output, and then deal with it a character at a time in a USART interrupt handler.

The microprocessor is capable of doing the things you want, but the resources are finite and if you saturate them doing one thing they won't be available for others.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jamsoft
Associate II
Posted on April 14, 2013 at 15:52

Thank you, I'll try what you recommend. Also, lower the timers sampling frequency should help by my opinion but I'm not sure about that. I am begginer in ARM SW development... 

Posted on April 14, 2013 at 16:44

I am beginner in ARM SW development... 

Having streamlined IRQ handlers is not an ARM issue. A routine that expects to service a periodic interrupt at 1 ms (1 KHz) can't take more than 1 ms to run.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jamsoft
Associate II
Posted on April 22, 2013 at 16:36

Thank you very much, Clive!

I did what you recomend and now everything works like a charm!

Jirka