This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:avr:external_interrupts [2010/03/04 17:25] – Translated to English yllars | en:avr:external_interrupts [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== External Interrupts ====== | ||
| + | External interrups are one of the most simple peripheral functions. Typically AVRs have 1 to 8 special pins, which are used to cause interrupts in the program when their logical value changes or they are at a certain state. Since this function is usually used to monitor external logical signals, these pins are called external interrupt pins. | ||
| + | |||
| + | To use an external interrupt, the pin has to be configured as a standard IO input (it can also be used as an output, but in this case the interrupt can only be created by the controller itself). It is necessary to allow receiving interrupts and specify the condition that causes the interrupt to fire in the external interrupt configuration register. There are four possible conditions: | ||
| + | |||
| + | * Logical zero (voltage of 0V) | ||
| + | * Change in the logical value | ||
| + | * Descending front - logical change from one to zero. | ||
| + | * Rising front - logical change from zero to one. | ||
| + | |||
| + | |||
| + | When the mode is set to logical zero, the interrupt will fire continuously as long as the pin has a value of zero. During this period the execution of the main program is stopped. | ||
| + | |||
| + | Grouped by principle, there are two types of interrupts: synchronized to the controller' | ||
| + | |||
| + | < | ||
| + | |||
| + | <box 100% round # | ||
| + | |||
| + | Task: Make ATmega128 pin number 9 (pin 7 on bus E) fire an interrupt if its value is changed. This pin corresponds to the INT7 external interrupt, which is synchronous. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | // The code of the external interrupt | ||
| + | ISR(INT7_vect) | ||
| + | { | ||
| + | // Do something | ||
| + | } | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | // Change pin 7 on bus E to an input by changing bit 7 to zero | ||
| + | DDRE &= ~(1 << PIN7); | ||
| + | |||
| + | // Defining a pull-up resistor to to pin 7 on bus E | ||
| + | // to prevent input floating | ||
| + | PORTE |= (1 << PIN7); | ||
| + | |||
| + | // Set the interrupt mode to logical change for interrupt 7 | ||
| + | // in the external interrupt configuration register | ||
| + | EICRB = (1 << ISC70); | ||
| + | |||
| + | // Allow external interrupt 7 | ||
| + | EIMSK |= (1 << INT7); | ||
| + | |||
| + | // Allow global interrupts | ||
| + | sei(); | ||
| + | |||
| + | // Endless loop | ||
| + | while (1) continue; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | In addition to interrupts fired by single pins, if the AVR has enough pins it is possible to use entire groups of pins to fire logical value change interrupts. These interrupts are simply called pin change interrupts. They fire when the value of at least one pin in the group is changed. | ||