This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:avr:interrupts [2010/02/08 12:55] – mikk.leini | en:avr:interrupts [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Interrupts ====== | ====== Interrupts ====== | ||
- | Katkestuse (inglise keeles // | + | Interrupts in AVR can be caused by counters, communication interfaces, analog-to-digital converters, comparators, special input-output pins and several other functions, depending on the controller. Any interrupt can be allowed or disallowed by the unit that generates it. Regardless of the interrupt being allowed or disallowed, there is a 1-bit field (interrupt flag) in the corresponding unit of the controller, which is marked as true when the interrupt condition is fulfilled. If the interrupt flag is changed to true and the interrupt is allowed, the controller starts to execute the code specified for this interrupt. |
- | AVR mikrokontrolleris on iga katkestus seotud kindla sündmusega. Igal sündmusel on olekuregistris lipubitt, mis tähistab sündmuse juhtumist. Lisaks on sündmustega seotud katkestuste maskeerimise registrid ja vastavad bitid. Kui sündmuse katkestuse bitt on maskeerimata ja tekib sündmus, jätab protsessor mõne(kümne) töötakti jooksul käimasoleva programmi täitmise pooleli ning alustab katkestuse programmi täitmist. Pärast katkestuse programmi täitmist jätkab protsessor poolelijäänud programmi täitmist. | + | Every interrupt in the AVR microcontroller is tied to a specific event. Each event has a flag bit in the status register, which marks the occurring of the event. Events are also tied to interrupt mask registers and the corresponding bits. If the event' |
- | ===== Näide ===== | + | <box 100% round # |
- | Katkestuste kasutamiseks | + | To use interrupts with the AVR LibC library, it is necessary to include |
<code c> | <code c> | ||
Line 14: | Line 14: | ||
ISR(XXX_vect) | ISR(XXX_vect) | ||
{ | { | ||
- | // Tee midagi | + | // Do something |
} | } | ||
</ | </ | ||
- | Globaalne, kõigi katkestuste toimumise lubamine, määratakse ära juht- ja olekuregistris SREG. Võimaluse kõiki katkestusi keelata või lubada tingib andmete kaitse vajadus. Kuna katkestused katkestavad käimasoleva programmi täitmise, võivad nad segada või rikkuda andmeid, mida põhiprogramm katkestamise hetkel kasutas. Sellist olukorda saab vältida kõikide katkestuste keelamisega enne tundlike andmetega tegelemist. Globaalne katkestuste keelamine on lihtne, kui seda saab teha ühe registri (SREG) muutmisega. Pärast kriitilise programmiosa lõppu saab katkestused uuesti lubada ja kõik katkestused, | + | </ |
- | ==== Näide ==== | + | Global allowing of all interrupts is configured from the control and status register SREG. The option to allow or disallow all interrupts at once is there to help protect data. Since interrupts disrupt the execution of the main program, some data used by the main program may be disturbed or corrupted in the process. Situations like this can be avoided by simply disallowing all interrupts before processing such delicate data. Disallowing interrupts globally is easy, if it can be done by changing only one register (SREG). After the critical part of the program has been executed, the interrupts can easily be allowed again and all the interrupts that would have fired in the meantime are executed. |
- | Oletame, et programmis on kasutusel | + | < |
+ | |||
+ | <box 100% round # | ||
+ | |||
+ | Let's suppose there is a 16-bit variable in use in the program, which is changed by both the main program and the interrupt program and the value of this variable is later given to another variable: | ||
<code c> | <code c> | ||
#include < | #include < | ||
- | // Globaalsed | + | // Global |
unsigned short x, y; | unsigned short x, y; | ||
- | // Suvaline katkestus, mis muudab | + | // A random interrupt that changes the value of x |
ISR(XXX_vect) | ISR(XXX_vect) | ||
{ | { | ||
Line 38: | Line 42: | ||
int main() | int main() | ||
{ | { | ||
- | // Muutujale | + | // Give a value to x |
x = 0x1111; | x = 0x1111; | ||
- | // Mingi katkestuse tööle seadistamine | + | // Allow interrupts globally |
- | + | ||
- | // Globaalne katkestuste lubamine - | + | |
- | // | + | |
sei(); | sei(); | ||
- | // x väärtuse muutujasse | + | // Give the value of x to y |
y = x; | y = x; | ||
} | } | ||
</ | </ | ||
- | Programm on väga lihtne | + | The program itself is very simple |
- | Toodud näites muutujale y muutuja | + | In the following example, the value of x is given to y using a safe method: |
<code c> | <code c> | ||
- | // Globaalne katkestuste keelamine | + | // Disallow interrupts globally |
cli(); | cli(); | ||
- | // Laadimine | + | // Give the value of x to y |
y = x; | y = x; | ||
- | // Globaalne katkestuste uuesti lubamine | + | // Re-allow all interrupts again |
sei(); | sei(); | ||
</ | </ | ||
+ | </ |