cancel
Showing results for 
Search instead for 
Did you mean: 

Fast Timer Interrupt Distrupts My Program

IAtam.1
Associate II

I need to achieve fast interrupts because I'm making integration from accelerometer and gyroscope datas in my code. And when I set a timer interrupt frequency to 2.5ms code get stuck. But when I increase timer frequency to 25ms it works just fine.

I have 3 sensors, 1 sd card module, 1 lora module and a gps module. Can UART, SPI or I2C transmissions get distrupted by fast timers?

Thank you for your time.

4 REPLIES 4
TDK
Guru

"Code get stuck" isn't very informative. Where is the execution at? Is it stuck within an interrupt handler that keeps getting called which prevents your main loop for progressing?

Spending too much time in timer interrupts can certainly impact your program if it's not written appropriately. UART and SPI generally shouldn't be impacted but I2C might be, if your timer doesn't return quickly enough.

If you feel a post has answered your question, please click "Accept as Solution".
Nandan V
Associate II

A little more information about the structure of your code would be helpful. I am assuming you are reading the sensors from the ISR.

The best practice (IMO) would be to not read the sensors from the interrupt service routine (ISR). Instead set a flag and read from the main loop.

This way the ISR would be a one-liner and therefore shouldn't affect your "program".

Measure duration of the timer interrupt, e.g. toggling a pin at entry and at exit and observing using oscilloscope/LA.

JW

I actually tried that but I think it prevents me from using my interrupt to full extent.

When I read from my sensors in handler I read every 25ms but when I set a flag I read every 200-250ms because my code always waits for lora and sd card modules and that makes my reading way too slow.