This is an old revision of the document!


Interrupções Externas

As interrupções externas são das funções periféricas mais simples. AVRs típicos AVRs tem 1 a 8 pinos especiais, que são utilizados para causar interrupções no programa, quando o valor lógico destes muda ou quando se encontram num determinado estado. Uma vez que esta função é normalmente usado para monitorar sinais lógicos externos, estes pinos são designados como pinos de interrupção externos.

Para utilizar uma interrupção externa, o pino tem de ser configurado como uma entrada standard de IO (que também pode ser utilizada como uma saída, mas, neste caso, a interrupção só pode ser criada pelo próprio controlador). É necessário permitir a recepção de interrupções e especificar a condição que causa a interrupção para disparar o registo de configuração de interrupção externa. Há quatro condições possíveis:

  • De zero lógico (tensão de 0V)

  * Alteração do valor lógico   * Frente Decrescente - mudança lógica de um para zero.   * Frente Crescente- mudança lógica de zero para um.

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's clock and asynchronous. Synchronized interrupts work by remembering the values of the inputs, which means that the changes in logical values are found by comparing values read during two different clock cycles. If the logical changes in the signal happen faster than the controller's duty-cycle, the interrupts either fire incorrectly or are skipped altogether. Asynchronous interrupts do not depend on the controller's clock and enable detecting faster changes in the external signal as well - the logical level must still be constant for at least 50 ns. ATmega128 has 4 synchronized and 4 asynchronous external interrupts.

 

Example

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.

#include <avr/interrupt.h>
 
// 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.

pt/avr/external_interrupts.1448905745.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0