Interrupt con Arduino: un ottimo modo di procedere senza ritardi tra l'ingresso e l'uscita |
Materie universitarie in cui l'argomento è contenuto:
Calcolatori Elettronici
Categorie in cui l'argomento è contenuto:
PROGETTI CON MICROCONTROLLORI
Generici
Con il presente articolo vorrei chiarire una potenzialita' nascosta di Arduino che permette di definire azioni tramite interrupt leggendo il segnale di ingresso. Con questa azione riradi di loop e fi if all'interno del codice sono evitati
voto :
Introduction to Interrupts Why would I need an interrupt? Robots spend a lot of time waiting for things to happen. A common example: your robot wants to drive straight until an IR sensor says that an object is too close. Seems fairly simple: Code:
driveForward();
What is an interrupt? The really cool thing about microcontrollers is they have fancy hardware that can do things like PWM, analog-to-digital conversion -- and interrupts. An interrupt is a little piece of hardware that sits, waiting to detect a trigger event, such as a particular pin going from a low state to a high state. When this event happens:
In your ISR, you would have code that does some processing to handle the event. For instance, your ISR would:
The Interrupt-Driven Bumper Let's now implement an example using the Arduino. The Arduino is based on an ATMEGA168 AVR. This chip has 2 hardware interrupts (named 0 and 1). The pins that can be used for interrupt triggers are tied to digital 2 and 3, respectively. The Arduino makes using interrupts quite easy, they have a function AttachInterrupt(interrupt, ISR, trigger):
There are a few things going on here. First, we start rolling forward. We have an integer used as a flag, that is either 0 when we have not hit an object, or 1 when we hit something. An interrupt occurs when we hit something, it will stop the robot to avoid any damage, and also set our flag. Then, our main loop will handle backing the robot up when it gets a chance. There are a few reasons to implement our code like this. First, delay() relies on interrupts. Second, you don't ever want interrupts to run for very long, as they will typically stop other interrupts from occurring -- this can be devastating when you have something like a system clock that relies on interrupts (as the Arduino does). Code:
// Interrupt-Driver Bumper Example Wait, I ran out of interrupts! The Arduino library only supports 2 pin interrupts, because it only uses the 2 dedicated hardware interrupts. However, the ATMEGA168 (the chip that the Arduino is built out of) can actually generate interrupts on every port, it's just slightly more complicated. These are called the Pin Change Interrupts. Each port of the AVR has it's own interrupt vector, and you can turn on interrupts for as many of the pins as you want. There are a few limitations though. First, an interrupt is generated for any pin change, you can't limit the hardware to rising or falling only, although you could implement that in your ISR. Second, because all of the pins in a port share an interrupt vector, if you use more than one pin in a port you will have to manually check which pin generated the interrupt. It should be noted that switching into the ISR takes several clock cycles, so your ISR will not run instantaneously, and thus it may be difficult to "check" which pin generated the interrupt.Lastly, because there isn't a library out there, you have to write a little more code. Note that the Arduino has arbitrary names for its pins, that have no relevance to the AVR names, you'll have to use the ATMEGA168 datasheet, plus the Arduino pin out chart to sort out register values. This is definately an advanced topic, but it is a great way to learn a few more details of the AVR architecture. Code:
// Quick example of using pin change interrupts Some Warnings for Arduino Users Don't use the delay() or millis() functions inside an ISR on the Arduino. The reason being that they depend on the system clock, which itself is generated from an interrupt. On the AVR architecture, when one interrupt starts processing its ISR, all other intterupts are disabled temporarily. For this same reason, you want to keep your ISR as short as possible (to avoid messing with the system clock itself). End Notes That about covers the basics of interrupts, with some examples using the Arduino. Take a look at my tutorial on closed loop feedback for more information about using encoders.
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
Data inserimento: 05/03/2010