Professional Documents
Culture Documents
4/11/12
Interrupts
n n
Atmega16 has 3 external interrupts and 17 internal interrupts These interrupts and the separate reset vector each have a separate program vector in the program memory space All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors The complete list of vectors is shown in a table on next slide
4/11/12
Interrupt
When an interrupt occurs, the Global Interrupt Enable Ibit is cleared and all interrupts are disabled n The I-bit is automatically set when a Return from Interrupt instruction is executed n The interrupts have priority in accordance with their interrupt vector position n The lower the interrupt vector address, the higher the priority What does it mean? n Like 8051 you cannot set the interrupt priority in Atmega16 the only solution is nested interrupt (interrupt with in an interrupt)
n
Nested Interrupts
n
The user software can write logic one to the I-bit to enable nested interrupts All enabled interrupts can then interrupt the current interrupt routine
External Interrupts
External Interrupt n The External Interrupts are triggered by the INT0, INT1, and INT2 pins Internal Through External Interrupt n If enabled, the interrupts will trigger even if the INT0..2 pins are configured as outputs n This provides a way of software interrupt INT0 and INT1 n The external interrupts can be triggered by a falling or rising edge or a low level INT2 n It is only edge triggered
More Details on Page 64 of Datasheet 4/11/12
Bit-I (7) of SREG is Global Interrupt Enable bit This bit must be set for the interrupts to be enabled
When the INT1 bit is set (one) the external pin interrupt 1 is enabled The corresponding interrupt of External Interrupt Request 1 is executed from the INT1 interrupt Vector
When the INT0 bit is set (one) the external pin interrupt 0 is enabled The corresponding interrupt of External Interrupt Request 0 is executed from the INT0 interrupt Vector
When the INT2 bit is set (one) the external pin interrupt 2 is enabled The corresponding interrupt of External Interrupt Request 2 is executed from the INT2 interrupt Vector
4/11/12
Last four bits of this register controls the mode of external interrupt 0 and 1
Bit 3, 2 ISC11, ISC10: Interrupt Sense Control 1 Bit 1 and Bit 0 (Interrupt 1 Sense control)
Bit 1, 0 ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0 (Interrupt 0 sense control)
4/11/12
If ISC2 is written to zero, a falling edge on INT2 activates the interrupt If ISC2 is written to one, a rising edge on INT2 activates the interrupt
4/11/12
For Interrupt 0
q q q
Set the SREG bit-I Set the INT0 pin of GICR Select its mode by setting bits ICS01 and ICS00 of MCUCR accordingly Set the SREG bit-I Set the INT1 pin of GICR Select its mode by setting bits ICS11 and ICS10 of MCUCR accordingly
For Interrupt 1
q q q
4/11/12
For Interrupt 0
q q q
Set the SREG bit-I Set the INT2 pin of GICR Select its mode by setting bit ISC2 of MCUCSR accordingly
4/11/12
When an edge or logic change on the INT1 pin triggers an interrupt request, INTF1 becomes set (one) The flag is cleared when the interrupt routine is executed If the I-bit in SREG and the INT1 bit in GICR are set (one), the MCU will jump to the corresponding Interrupt Vector Alternatively, the flag can be cleared by writing a logical one to it This flag is always cleared when INT1 is configured as a level interrupt
4/11/12
When an edge or logic change on the INT0 pin triggers an interrupt request, INTF0 becomes set (one) The flag is cleared when the interrupt routine is executed If the I-bit in SREG and the INT0 bit in GICR are set (one), the MCU will jump to the corresponding Interrupt Vector Alternatively, the flag can be cleared by writing a logical one to it This flag is always cleared when INT0 is configured as a level interrupt
4/11/12
When an edge or logic change on the INT2 pin triggers an interrupt request, INTF2 becomes set (one) The flag is cleared when the interrupt routine is executed If the I-bit in SREG and the INT2 bit in GICR are set (one), the MCU will jump to the corresponding Interrupt Vector Alternatively, the flag can be cleared by writing a logical one to it
4/11/12
Example
#include <avr/io.h> #include <avr/interrupt.h> unsigned char z=0; void main() { DDRB=0xFF; //Configures port B as output DDRD|=0b00001000; //configure bit4 of port D as intput SREG=SREG|0b10000000; //Enabling Global Interrupt GICR=0b10000000; //Enabling Interrupt 1 MCUCR=(1<<ISC10|0<<ISC11); //selecting mode of interrupt 1 while(1) { PORTB=z; //continuously outputs z } } ISR(INT1_vect) { z++; }4/11/12