120 likes | 197 Views
External & internal Interrupts. Interrupt Sources. There are 21 different interrupts and each one has its own vector located in a predefined location at the start of the memory space.
E N D
Interrupt Sources • There are 21 different interrupts and each one has its own vector located in a predefined location at the start of the memory space. • 3 external interrupts (INT0, INT1 and INT2) are available at pins PD2(pin 16) and PD3 (pin 17) and PB2 (pin 3). They are Edge or Level triggered interrupts (except Int2 which is only edge triggered). • Reset input (pin 9) which causes also an interrupt and can be both externally and internally initiated. • If enabled, the interrupts will trigger even if the INT0 and INT1 pins are configured as outputs. This feature provides a way of generating a software interrupt (how?). • INT0, INT1 (level) and INT2 are detected asynchronously (no Clock needed and thus they can be used to get the µC from sleep modes.
Notes to interrupt vector table • If the program never enables an interrupt source, the Interrupt Vectors are not used, and regular program code can be placed at these locations.
External Interrupts: • To enable INT0/INT1/INT2: • The I-bit in the Status Register must be set. But also the corresponding External Interrupt Request Enable bit in the General Interrupt Control Register (GICR) • When an event on the INT0/INT1/INT2 pin triggers an interrupt request, INT0-Flag/INT1-Flag/INT2-Flag (bit 6/7/5 of the General Interrupt Flag Register – GIFR) becomes set. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. General Interrupt Flag
Interrupt Triggering • The external interrupts INT0/INT1 can be triggered by a falling or rising edge or a low level.
INT2 • The external interrupts INT2 can only be triggered by a falling or rising edge. • ISC2 (interrupt sense control of INT2): • ISC2 = 0 -> Falling Edge • ISC2 = 1 -> Rising Edge MCU Control and Status Register
Using Interrupts in C The access to the AVR interrupt system is implemented with the interrupt keyword. Examples: interrupt [2] void external_int0(void) { /* Place your code here */ RETI } interrupt [10] void timer0_overflow(void) { /* Place your code here */ RETI }
Using Interrupts in C (Contd.) • Interrupt vector numbers start with 1. • The compiler will automatically save the affected registers when calling the interrupt functions and restore them back on exit. • a RETI assembly instruction is placed at the end of the interrupt function. • Interrupt functions can't return a value nor have parameters. • You must also set the corresponding bits in the peripheral control registers to configure the interrupt system and enable the interrupts.
Including Assembly Lines in the Program You can include assembly language anywhere in your program using the #asm and #endasmdirectives. Example: void delay(unsigned char i) { while (i--) { /* Assembly language code sequence */ #asm nop nop #endasm }; } Inline assembly may also be used. Example: #asm("sei") /* enable interrupts */