control time for Timer interrupt routine

Joined Feb 4, 2022
145
I'm getting confused in a basic concepts, A microcontroller has a hardware timer that is configured to generate timer interrupts for every 1 ms. When the interrupt occurs every 1 ms. The microcontroller takes action on each interrupt. Suppose the microcontroller takes less than 100 us to service the interrupt service routine.

I understand that the microcontroller enters the interrupt service routine every 1 ms. I don't understand at what time the microcontroller exits from the interrupt service routine.

Does the main main loop do nothing until 1 ms has elapsed?

In my situation, I guess the microcontroller enters the interrupt service routine every 1 ms and goes back to the main loop after spending 100 us in the interrupt service routine.

BobTPH

Joined Jun 5, 2013
7,586
It goes back to main when the interrupt service routine executes a return from interrupt instruction. If that takes 1 ms, the main is blocked all the time. If it takes 100us, then main is blocked for 10% of the time.

Bob

DickCappels

Joined Aug 21, 2008
10,068
I'm getting confused in a basic concepts, A microcontroller has a hardware timer that is configured to generate timer interrupts for every 1 ms. When the interrupt occurs every 1 ms. The microcontroller takes action on each interrupt. Suppose the microcontroller takes less than 100 us to service the interrupt service routine.

I understand that the microcontroller enters the interrupt service routine every 1 ms. I don't understand at what time the microcontroller exits from the interrupt service routine.

Does the main main loop do nothing until 1 ms has elapsed?

In my situation, I guess the microcontroller enters the interrupt service routine every 1 ms and goes back to the main loop after spending 100 us in the interrupt service routine.
I have written several programs in which the main loop just sat in an infinite loop waiting for an interrupt or jumped to code that sleeps the controller until the next interrupt. Sleeping until the next interrupt gets you precise and repeatable timing. It also saves some energy which can be important for battery operated systems.

The rule is: Anything that gets the controller to do what you need it to do is fair.

Joined Feb 4, 2022
145
I think I got it. When interrupt occurs every 1 millisecond and interrupt routine service takes 10 microseconds to execute. main loop blocks for 100 microseconds and executes for 900 microseconds

DickCappels

Joined Aug 21, 2008
10,068
You can do that if you can count on the interrupt routine to take the desired amount of time. If you only have one thing going on and the routine is predictable, you need not bother with interrupts at all.