Electronic – Deciding Timer Interrupt time

interruptsmicrocontrollertimer

I have used timer interrupt in my application, mainly for timing reasons like timeout detection for processes,pulse generation etc, every 1 ms.
Now i have a doubt.

Suppose in main(), if im doing another operation like, maybe writing to a SPI Flash a huge chunk of data, and the timer interrupt occurs,will the data writing process stop due to interruption by timer ISR or data be corrupted due to interruption by timer ISR?
Im just wondering if whether the timer interrupt time is decided considering such operations that make up the complete application code?

If so,if a SPI flash write takes about 5ms and timer interrupt happens every 1ms, how can i keep track of timeouts,pulse generations etc without compromising of data corruption scenarios,because now ill have to change the timer interrupt to greater than 5 ms, and then i cant keep a reference of timeouts for processes where small timeouts like 1 ms may be needed.

Update

Im using a hardware timer, and when the interrupt occurs,il be using a software timer counter(variable) in the ISR,to keep track of time.

Best Answer

If you write the mainline code where the SPI Flash Writes take place in the proper way then the occurrence of the timer interrupt should not corrupt the operation in progress. If the main stream operation has certain timing dependancies that must be kept then it is necessary to disable the interrupts for short periods of time when the critical timing windows occur.

Another thing is to keep the code in the timer interrupt very simple and concise. Do not put any real decision code in the timer routine. Instead make yourself a collection of software managed timer variables. The timer interrupt simply checks each one of these variables and if non-zero it would simply decrement the value by one. The mainstream code would set one or more of these variables to suitable non zero values when a time period is needed. Then in the normal course of its mainline operations the timer variable can be checked to see if it has gone to zero. This can thus signal a timeout or time delay has occurred.