Arduino Timer Interrupt Example

preview_player
Показать описание
Don't be afraid to use interrupts in your Arduino sketches. Interrupts enable you to do so much more with your Arduino that it's well worth taking the trouble to learn a few simple rules.

This video demonstrates the use of the timer 1 hardware timer of the Arduino Uno to provide precisely timed processing. Both simple interrupts and reentrant interrupts are covered to show the benefits of allowing your interrupt service routines to interrupt themselves.

Transcript:

The Arduino Uno has 3 hardware timers that you can use to run tasks at precise intervals. If you call an interrupt service routine when the timer triggers you can achieve accurate timing irrespective of what the other code in your sketch or programme is doing.

I'm going to demonstrate the use of interrupts by using them to trigger 2 LEDs from within the service routine. LED D2 is toggled on each interrupt and LED D1 is toggled after a number of interrupts are counted.

When D1 is toggled the service routine executes a long task to demonstrate the effect that this has on the behaviour of your circuit.

Here's a pseudo-code version of the sketch I used. As you can see it is very simple and it isn't that different to the real code that I used.

This is what happens when you load and run the sketch on your Arduino. D2 flashes quickly when toggled by the interrupt. D1 flashes slower but notice that D2 pauses each time that D1 changes state. This is because the time consuming task takes longer than the time between the timer interrupts. Interrupts are disabled as soon as you go into an interrupt service routine. SO D2 has to wait.

If you want to schedule tasks to run at precise intervals then this isn't going to work because the long routine holds up the timing. So what if you could enable interrupts while the long routine is running and allow it to be interrupted by the fast interrupt?

Well you can. It's called a reentrant interrupt service routine and it's very easy to code. See the additions to the pseudo-code in red. All you have to do is enable interrupts while it is in the long routine and disable them again afterwards. Provided that the long task is not allowed to interrupt itself then all is well.

This is what happens when you make the interrupt service routine reentrant. Now the fast LED D2 is flashing at regular intervals without any pause. The long routine is no longer holding it up.


There's lots of other stuff on the web site too. Go and have a look.
Рекомендации по теме
Комментарии
Автор

Hey If i want the interrupt to trigger after the program gets into an if else statement, what do I do?

devilboii
Автор

what if i want to start other arduino board as interrupt trigger occured and vice versa

sb-rjyb
Автор

So what would happen if the time consuming task takes longer to execute than the time before n interrupts have occurred? In that case the time consuming task would be running twice at the same time..

bieploiu
Автор

What is that white sort of box (with the blue and red lines on it) what you plug the wires in with the leds called? I have one myself but without any manual/name, if you could tell me how it is called i could look up how it works. Just the name would really help me.

eeopd
Автор

can you help on my problem. I have build a lift controller using Arduino. I have attached stop button to interrupt on Arduino. I want to stop lift as soon as stop button is pressed. But the code executes the interrupt routine but do not stop the list. I mean the digital write doesn't work in ISR. Pls. advice.

VishalBhingare_Ltd
welcome to shbcf.ru