Difference between interrupt service routine and interrupt subroutine

Hi,

I am using an Arduino UNO R3. In my code, i am using a timer2 library to execute a function at a specific time interval of 5 ms. Then, i also have 2 Hall encoders from my 2 DC motors which are attached to the hardware interrupt pins 2 and 3. My confusion is about how to distinguish between the interrupts from the timer and encoders. So, from my understanding, the timer interrupt is known by the user, so it is a interrupt subroutine, and the encoders are interrupt service routine, since they cannot be controlled by the user. Am i correct?

MsTimer2::set(5, timerfunction);
MsTimer2::start();
attachInterrupt(digitalPinToInterrupt(2), encoderFuncA, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), encoderFuncB, CHANGE);

I researched and i found this answer but i want to confirm if it's entirely correctly.

Subroutine runs when you call it. ISR runs whenever a certain signal occurs. (The signal can be generated by software or hardware.) The big difference is that you know where the subroutine runs (because you call it). But you do not know when the ISR will be executed. You code may run normally when an hardware interrupt occurs and your program jumps to the ISR. This can happen anywhere in your code (in between two statements or even in the middle of a complete statement, remember a statement is compiled into multiple assembly instructions).

Source: Difference between subroutine and interrupt in computer organization - Brainly.in

I've never heard of an interrupt subroutine. If it is an interrupt then it isn't something that you call. That question was about the difference between a subroutine (we're coding C++ so we call them functions) and an interrupt.

My confusion is about how to distinguish between the interrupts from the timer and encoders.

What do you mean by that sentence. They'll be completely different functions. And you'll tell the timer library which one it can run and you'll use attachInterrupt to tell the external interrupts which one to run.

Delta_G:
I've never heard of an interrupt subroutine. If it is an interrupt then it isn't something that you call. That question was about the difference between a subroutine (we're coding C++ so we call them functions) and an interrupt.

What do you mean by that sentence. They'll be completely different functions. And you'll tell the timer library which one it can run and you'll use attachInterrupt to tell the external interrupts which one to run.

I know they will be different functions, but i mean, for the timer interrupt, i know when the function will run, since i can set the time interval, so the function will run based on what i have configured. But compared to the encoders, the functions will execute whenever there is a change in the pulse level signal, which is not user-controllable, since it is entirely independent and is controlled by the hardware itself, in this case, the UNO.

I've heard of interrupt subroutine and interrupt service routine, so i'm confused about which one is relevant and which one to use when.

for the timer interrupt, i know when the function will run, since i can set the time interval, so the function will run based on what i have configured.

That's not what it meant. It doesn't mean you won't know when in time it will run. It means you don't know at what point in your code it will run. When it runs you don't know what will have been the last line executed by your code and when it exits you don't know what the next line run by your code will be.

As opposed to a function you call from your code where you know exactly what order it was called in and what came before and what came after.

DryRun:
I've heard of interrupt subroutine and interrupt service routine, so i'm confused about which one is relevant and which one to use when.

I've never heard that first one, but I would imagine that they are both talking about the same thing.

In C++ we almost never talk about "subroutine". We usually talk about functions because of the way the code is structured in C and C++. In other languages they call them subroutines and maybe in those languages they call an interrupt handler a subroutine.

Then, in my first post, functions encoderFuncA and encoderFuncB are both activated by external interrupts and classified as Interrupt Service Routines. But is the function timerfunction also considered as an ISR or would it be a special type of ISR since it is from an internal interrupt?

But is the function timerfunction also considered as an ISR or would it be a special type of ISR since it is from an internal interrupt?

Yes it is an ISR. All the caveats about what can be done within an ISR apply.

It is a timer interrupt ISR. There are several different types of timer interrupts, and the one used by MSTimer2 is a Timer Overflow Interrupt.

How do i configure the priority of ISRs? According to this quote, it should be possible. For example, i want the timer ISR to always have priority over the encoder ISRs.

If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have.

Source: attachInterrupt() - Arduino Reference

Where does that say it should be possible?

It really isn't. Check out section 11 or 12 in the data sheet.

The vectors with the lower addresses get priority. So the external interrupts are going to win.

Key is to make sure they are very short. Keep all your code non-blocking and your 5ms is an eternity. That should be able to be handled just from loop without the interrupt depending on what it is (you haven't said).

If you want to make a pulse with the ultimate of priority, don't use a timer interrupt. Use the PWM hardware. That's what it does, makes square wave pulses. If that's what you want to do.