Log in

View Full Version : When would I use an interrupt?


Apollo Clark
11-01-2004, 21:36
I'm a noob to C programming, and don't understand how interrupts work, so...

How is:

void Default_Routine(void)
{
if(bButtonA)
vButtonPressed();
....
};

different from calling an interrupt when ever bButtonA is pressed?

(i tried to post this message earlier, but it didn't seem to go though...)

mightywombat
11-01-2004, 21:50
hmmm... i still have a limited understanding but check out this post. It will enlighten you hopefullys. I actually can't find it. Do a search for interrupts in the programming forum. Read the posts. Kevin Watson and WizardofAz had some really good things to say. I wish I could find the thread. Kevin Watson also has some demo code at www.kevin.org/frc (http://www.kevin.org/frc) for interrupts.

m0rph3us
11-01-2004, 22:03
The difference is that vButtonPressed(); will only be called when the if statement is being executed. With interrupts it doesn't matter what part of the code is being executed for if the button is pressed it will automatically call vButtonPressed();

One basicaly gives faster feedback than the other

Apollo Clark
11-01-2004, 22:13
Mightywombat: thanks, i downloaded the source and am going over it right now ^_^

m0rph3us:OHHHHHHHHHHHHH... makes so much sense now.

So, when does the processor check the interrupt registers?

And, is the modem receiving data even when it's in the middle of executing a program?

Jay Lundy
11-01-2004, 22:27
When you have something like a button you probably won't need to use an interrupt. Well it actually depends on how often the button is checked in normal operation. If the button is checked at 40 Hz (loop time for the FIRST default code, based on the radio baud rate), then you won't need an interrupt (in fact an interrupt might make it harder to deal with because you have to worry about key bounce).

But if you have some critical sensor that updates at 10 kHz and you want to catch every update with a polling frequency of 40 Hz, you would need an interrupt.

No matter where the code is, as soon as an interrupt happens it jumps to the appropriate memory location (depending on whether it's high priority or low priority). The only exception is if another interrupt of equal or higher priority is currently being processed.

Rickertsen2
11-01-2004, 22:38
I'm a noob to C programming, and don't understand how interrupts work, so...

How is:

void Default_Routine(void)
{
if(bButtonA)
vButtonPressed();
....
};

different from calling an interrupt when ever bButtonA is pressed?

(i tried to post this message earlier, but it didn't seem to go though...)
First let me explain clearly what an interrupt is. Basically an interrupt is a peice of code that is automatically executed whenever a specific event occurs. When an interrupt occurs, your program will immediately stop executing whatever it was doing and jump to the interrupt handler. Once the interrupt handler has finished, the program jumps back to wherever it was before the interrupt occured. Our control system has many soureces of interrupts. The two most useful are explained below.:

Timers: These are special counters that operate in the background. They can be programmed to count up at a specified speed. They can be programmed to trigger an interrupt once they reach a specified number. This is extremely useful for keeping track of time or making somethign happen at a specific time. Thsi description really doesn't so timers justice. I recimmend you read the IFI whitepaper at: http://innovationfirst.com/FIRSTRobotics/pdfs/Timers_White_Paper_2003-Nov-20.pdf

IO: pins: interrupts can be set up to execute each time the state of an IO pin changes. An example of this would be your button scenario above.

What you did above is called polling. This method of detecting whether something is happening has two disadvantages. The first is that you are wasting time actively checking to see if something happened or not. The second is that its not very good at detecting things immmediately or things that happen quickly.

KevinB
11-01-2004, 23:44
In summary of the above, interrupts are more or less only necessary if the event triggering the interrupt is: (a) highly time sensitive, that is, the code should execute EXACTLY when that event occurs; or (b) going to be occuring faster than the main loop would poll for it (ie. hundreds or thousands of times per second).

Jeff McCune
18-01-2004, 13:29
So, when does the processor check the interrupt registers?
I think you're missing the point of interrupts. It's not that the function is executed anywhere you're code is running. This is true, but this is just a side-effect of the real purpose of interrupts.

Interrupts provide a way for the harware to be notified of an *event* versus the hardware frequently checking if an event happened sometime in the past. You don't *have* to use interrupts for a simple digital switch, but you might as well. I'm a big fan of not wasting resources, if I have interrupts left over, I'll use them for a trivial task such as this. My logic is, why waste time checking something if I can be told when it happens.

Also, if this controler has "true" interrupts, then the hardware doesn't need to "check the registers." Asserting an interrupt pin on the chip will literally *interrupt* execution and force execution of the interrupt handler. Doesn't need to check anything.