Atmel AVR1200: Using External Interrupts for megaAVR Devices Features • • • • Flexible pin configuration Synchronous and asynchronous interrupt sensing Asynchronous wake-up signal to wake up from sleep modes ® Driver source code included for Atmel ATmega2560 - Basic external interrupt usage on ATmega2560 - Nested external interrupt usage on ATmega2560 - Generating software interrupt with external interrupt pin configured as output on ATmega2560 - Using external interrupt to wake up device on ATmega2560 8-bit Atmel Microcontrollers Application Note 1 Introduction This application note illustrates the functionality and configuration steps (usage) of the external interrupts available on Atmel megaAVR® family of Atmel AVR® Microcontrollers. The application note also describes the points to be considered while using a GPIO pin as an external interrupt source pin. The example codes have been implemented on ATmega2560 device with Atmel AVR Studio® 5 and tested on Atmel AVR STK®600 starter kit for functionality. Rev. 8468A-AVR-11/11 2 External interrupt – overview Interrupts are signals given to the CPU of the microcontroller unit, either from internal peripheral modules or from external pins of the MCU that alters the regular flow of the program execution by making the CPU to make a jump to execute instruction routines in some pre-defined location depending on the interrupt that occurred. Once the CPU completes the routine, it gets back to the location from where it had made a jump. These pre-defined locations are called as the interrupt vector addresses or interrupt vectors. An interrupt causes the device to save its state of execution and start executing the interrupt handler like for Atmel AVR Microcontrollers the PC register is the only register that will be saved and the stack pointer register is updated on an event of an interrupt. It is up to the user to save other registers like the status register, 32 general purpose registers (register file), on an event of an interrupt, if there is such a requirement in the application. The Interrupts are commonly used to save time (like multitasking) than the conventional polling method (waiting for the event to occur indefinitely). 2.1 External interrupt vectors The Atmel megaAVR supports several interrupt sources out of which external interrupts are significant. The external interrupts can be triggered using two sets of pins. INTn pins (ordinary external interrupt pins) and PCINTn pins (pin change external interrupt pins). The ‘n’ varies from device to device and signifies the number like INT0. Please refer to respective device datasheet for the specific values of n. For Atmel ATmega2560 the numbers of external interrupt pins are as given below: 1. INT7:0 Pins (with various input sense configurations). 2. Pin change interrupts pins (PCINT23:0). Generally for each interrupt source there is an interrupt vector to which the program execution control jumps, to execute the corresponding service routine. The interrupt vectors for external interrupts on ATmega2560 are shown in Table 2-1. For device specific interrupt vectors, please refer to the respective datasheet. Table 2-1. External interrupt vector address table. Vector no. 2 Program address Source Port pins in ATmega2560 Interrupt definitions 1 $0000 RESET RESET External Pin, Power-on Reset, Brown-out Reset, Watchdog Reset, and JTAG AVR Reset 2 $0002 INT0 PD0 External Interrupt Request 0 3 $0004 INT1 PD1 External Interrupt Request 1 4 $0006 INT2 PD2 External Interrupt Request 2 5 $0008 INT3 PD3 External Interrupt Request 3 6 $000A INT4 PE4 External Interrupt Request 4 7 $000C INT5 PE5 External Interrupt Request 5 8 $000E INT6 PE6 External Interrupt Request 6 Atmel AVR1200 8468A-AVR-11/11 Atmel AVR1200 Vector no. Program address Source Port pins in ATmega2560 Interrupt definitions 9 $0010 INT7 PE7 External Interrupt Request 7 10 $0012 PCINT0 PCINT0:7 – PB0:7 Pin Change Interrupt Request 0 Pin Change Interrupt Request 1 Pin Change Interrupt Request 2 11 $0014 PCINT1 PCINT8 – PE0 PCINT9:15 – PJ0:PJ6 12 $0016 PCINT2 PCINT16:23 – PK0:7 The eight external interrupt sources (INT7:0) have dedicated interrupt vectors where as group of pin change interrupts share the same interrupt vector as listed in Table 2-1. Any signal level change in any of the eight pins PCINT0:7 (if enabled) will trigger the interrupt PCINT0. This means that, if an interrupt is triggered by either the pin PCINT0 or PCINT4, the CPU will jump to the same vector address $0012. Similarly, any signal level change in any of the eight pins PCINT8:15 (if enabled) will trigger the interrupt PCINT1 and any signal level change in any of the eight pins PCINT16:23 (if enabled) will trigger the interrupt PCINT2. For pin change interrupt each of PCINT0 to PCINT7 is OR'ed together and synchronized. It is up to application code to solve the handling by keeping track of previous pin values and then in the interrupt routine scan the present pin values to check which pin has changed. Similar is the case for PCINT8:15 and PCINT16:23. 2.2 External interrupt sensing External interrupts can be sensed and registered either synchronously or asynchronously. Synchronous sensing requires I/O clock whereas asynchronous sensing does not requires I/O clock. This implies that the interrupts that are detected asynchronously can be used for waking the device from sleep modes other than idle mode because the I/O clock is halted in all sleep modes except idle mode. The sense configuration for external interrupts and pin change interrupts for Atmel ATmega2560 is given in Table 2-2. For device specific sense configuration, please refer to the respective datasheet. Table 2-2. External interrupts sense configuration. Program address Interrupt source Sensing $0002 INT0 Asynchronous (Edges and level) $0004 INT1 Asynchronous (Edges and level) $0006 INT2 Asynchronous (Edges and level) $0008 INT3 Asynchronous (Edges and level) $000A INT4 Synchronous (Edges and level) $000C INT5 Synchronous (Edges and level) $000E INT6 Synchronous (Edges and level) $0010 INT7 Synchronous (Edges and level) $0012 PCINT0 Asynchronous $0014 PCINT1 Asynchronous $0016 PCINT2 Asynchronous 3 8468A-AVR-11/11 From Table 2-2, all the pin change interrupts are detected asynchronously. Other interrupts (INT7:0) can be triggered by sensing the rising or falling edges or low level on the corresponding interrupt pins. The type of sensing (edge or level) for each of the INTn (n = 0 to 7 for Atmel ATmega2560) interrupts is software configurable using two Interrupt Sense Control (ISC) bits per interrupt. This is given in Table 2-3. Table 2-3. External interrupts individual sense configuration. NOTE ISCn1 ISCn0 Description 0 0 The low level of INTn generates an interrupt request 0 1 Any edge of INTn generates an interrupt request 1 0 The falling edge of INTn generates an interrupt request 1 1 The rising edge of INTn generates an interrupt request PCINT23:0 does not have sense configuration options (This means that the interrupt will be generated whenever there is a logic change in the pin, that is, from high to low transition and low to high transition). 2.2.1 Asynchronous sensing in ATmega2560 From Table 2-2, interrupts INT3:0 are registered asynchronously. Pulses on INT3:0 pins wider than the minimum pulse width (typically 50ns for ATmega2560) will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. 2.2.2 Synchronous sensing in ATmega2560 From Table 2-2, interrupts INT7:4 are registered synchronously. The value on the INT7:4 pins are sampled before detecting edges. If edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. For both type of sensing, if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. 2.3 Interrupt response time The interrupt execution response for all the enabled AVR interrupts is four/five clock cycle’s minimum. This four/five clock cycles depends on the program counter width. If the program counter width is not more than two bytes, then the interrupt response time will be four clock cycles minimum and if the program counter width is more than two bytes, then the interrupt response time will be minimum five clock cycles. These four/five clock cycles include: 1. Two/Three cycles for pushing the Program Counter (PC) value into the stack. 2. One cycle for updating the stack pointer. 3. One cycle for clearing the Global interrupt enable (I) bit. If an interrupt occurs when the MCU is in sleep mode, the interrupt execution response time is increased by five clock cycles. This increase comes in addition to the start-up time from the selected sleep mode. This start up time is the time it will take to start the clock source. 2.4 Interrupt priority Priority for the interrupts is determined by the interrupt vector address. An interrupt with lowest interrupt vector address has the highest priority. So reset has the highest 4 Atmel AVR1200 8468A-AVR-11/11 Atmel AVR1200 priority followed by INT0, then INT1 and so on. If two interrupts occurs simultaneously, then the interrupt with higher priority is served first. 2.5 Important points to be noted when using external interrupts 1. If a level triggered interrupt is used for waking up the device from Power-down, the required level must be held long enough for the MCU to complete the wakeup, to trigger the level interrupt. If the level disappears before the end of the Startup Time, the MCU will still wake up, but no interrupt will be generated. 2. Both INT7:0 and pin change interrupts can be used to wake-up the device from any sleep modes. But INT7:4 should be configured to sense level interrupt to wake-up the device from sleep mode other than idle mode. 3. If enabled, a level triggered interrupt will generate an interrupt request as long as the pin is held low. 4. When changing the ISCn bit, an interrupt can occur. Therefore, it is recommended to first disable INTn by clearing its Interrupt Enable bit in the EIMSK Register. 5. Before enabling an interrupt, it is recommended to clear the flag bit of the corresponding interrupt because when the flag bit is set, the interrupt will be triggered the moment we enable the interrupt. 6. If enabled, interrupts will be triggered even when the pins are configured as outputs. This provides a way of generating a software interrupt. 7. If a logic high level (“one”) is present on an asynchronous external interrupt pin configured as “Interrupt on Rising Edge, Falling Edge, or Any Logic Change on Pin” while the external interrupt is not enabled, the corresponding External Interrupt Flag will be set when resuming from the power-down, power-save and stand-by sleep modes. 8. Once the CPU enters the ISR, the global interrupt enable bit (I-bit) in SREG will be cleared so that all other interrupts are disabled. In order to use nested interrupts, the I-bit has to be set by software when the CPU enters an ISR. 5 8468A-AVR-11/11 3 Getting started This section walks you through the basic steps for getting started and running with the external interrupts on Atmel megaAVR devices. The tasks given below simply use the switches and LEDs available on Atmel STK600. Without using STK600, these tasks can be verified by connecting a switch circuit and LED circuit to the port pins directly as shown in Figure 3-1. It is to be noted that the first three tasks uses some delay loops inside the interrupt service routine to demonstrate the use of interrupts. Generally it is never recommended to use a delay routine inside the ISR. Figure 3-1. Basic LED and switch circuit. VTG VTG 150R 150R 10K VTG LED 3 4 VTG 150R VTG To port pin Switch BC847W 10K 1 2 To port pin 3.1 Task 1 – Basic external interrupt usage Task: Enable INT0 interrupt and pin change interrupt PCINT0 to light up one LED for each in their respective interrupt service routines. 1. Configure PORTA as output. To use the switches in the STK600 as interrupt source, enable pull-up on PORTB (for PCINT0) and PORTD (for INT0). 2. Configure INT0 to sense rising edge. Enable the interrupts INT0 and PCINT0 and set the global interrupt enable bit. 3. Inside the ISR of INT0, turn on LED0 and then turn off LED0 after some delay. Similarly for PCINT0, turn on and off LED1 with some delay in between. Hardware setup: 1. Connect PORTA header to the LED header on STK600 using a ten wire ribbon cable. 2. Connect two wires, one between the pins SW0 and PD0 and the other between SW1 and PB0. Without using STK600, connect two LED circuits as shown in Figure 3-1, one at PA0 and another at PA1 and two switch circuits, one at PD0 and another at PB0. While running the example code when switch SW0 is pressed, due to pull-up the interrupt INT0 will be triggered when SW0 is released and LED0 blinks once. When switch SW1 is pressed the interrupt PCINT0 is triggered and LED1 blinks once and when SW1 is released the interrupt PCINT0 is triggered once again and LED1 blinks 6 Atmel AVR1200 8468A-AVR-11/11 Atmel AVR1200 once again. So a single switch action (press and release) on SW1 produces two blinks on LED1. 3.2 Task 2 – Nested external interrupt usage As mentioned before, once the CPU enters the ISR, the global interrupt enable bit (Ibit) in SREG will be cleared so that all other interrupts are disabled. In order to use nested interrupts, the I-bit is set by software when the CPU enters an ISR. Task: Enable INT0 and INT1 interrupts. Within the ISR of INT0 set the I-bit so that INT1 interrupt will be sensed and executed (by jumping to ISR of INT1) while the CPU is inside ISR of INT0. 1. Configure PORTB0 and PORTC0 as outputs to turn ON LED0 and LED1 respectively. To use the switches in the Atmel STK600 as interrupt source, enable pull-up on PORTD (since PD0 & PD1 are INT0 & INT1 respectively). 2. Configure INT0 and INT1 to sense rising edge. Enable the interrupts INT0 and INT1 and set the global interrupt enable bit. 3. Inside the ISR of INT0, set the I-bit (using sei() instruction), turn on LED0 and then turn off LED0 after some delay. Similarly for INT1, turn on and off LED1 with some delay in between. Hardware setup: 1. Connect PB0 pin to LED0 and PC0 pin to LED1 using wires, on the STK600. 2. Connect two wires, one between the pins SW0 and PD0 (for INT0) and the other between SW1 and PD1 (for INT1). Without using STK600 connect two LED circuits as shown in Figure 3-1, one at PB0 and another at PC0 and two switch circuits, one at PD0 and another at PD1. While running the example code when switch SW0 is pressed, due to pull-up the interrupt INT0 will be triggered only when SW0 is released and LED0 blinks for some time. During the time while LED0 is glowing, a switch action on SW1 will trigger INT1 and hence the LED1 blinks for a moment and goes off. This is because, inside the ISR of INT0, the I-bit is set so that all other interrupts are activated. So even when the CPU is inside the INT0 routine it senses the interrupt INT1 and jumps to the ISR of INT1 and executes the routine and then jumps back to ISR of INT0. Please comment the line ‘sei();’ in the ISR of INT0 and check what happens! 3.3 Task 3 – External interrupt based on signal change on pin As mentioned before once an interrupt is enabled it will be triggered even when the corresponding pin is configured as output. Task: Enable 16-bit Timer 3 in CTC mode with OC3B pin (PE4 pin - configured as output) (also INT4 pin) toggling on compare match. Enable the interrupt INT4 with the ISR containing a routine that turns on and off the LED0 connected to PORTA. 1. Configure PORTA0 as output to drive LED0. 2. Configure INT4 to sense rising edge and enable INT4. Also set the global interrupt enable bit. 3. Configure Timer3 to operate in CTC mode (OCR3A as TOP) with OC3B pin toggling on compare match. Load OCR3A with some value. 4. Configure OC3B pin (INT4 pin/PE4 pin) as output and start the timer with some prescalar value (In the example code it is divide by 256). 5. Within the ISR turn ON LED0 connected to PORTA0 and turn OFF LED0 after some delay. 7 8468A-AVR-11/11 Hardware Setup: 1. Connect a wire between pins PA0 and LED0. 2. Connect a wire between pins PE4 and LED1 to view the OC3B output. Without using STK600, connect two LED circuits as shown in Figure 3-1, one at PA0 and another at PE4. When running the example code, the LED1 (connected to OC3B pin) toggles because of timer action. Whenever the LED1 switches OFF (means a transition from low to high – a rising edge), INT4 is triggered and so LED0 blinks once. 3.4 Task 4 – External interrupt for device wake-up Task: Enable INT0 and set the device in sleep mode. Use INT0 to wake-up the device and turn ON the LED to indicate that the device is in active mode. 1. Configure PORTA0 as output to drive LED0. 2. Enable pull-up on PORTB0 and PORTD0 to connect to switches SW0 and SW1 respectively. 3. Configure INT0 (on PORTD0) to sense rising edge and enable INT0. Also set the global interrupt enable bit. 4. Set sleep mode to power-down mode and turn ON LED0. 5. Wait until the switch SW0 is pressed and once it is pressed, turn OFF LED0 (to indicate that the device enters sleep mode) and enter into sleep mode. 6. Inside the ISR (after wake-up) turn ON LED0 to indicate that the device is in active mode now. Repeat steps 5 and 6. Hardware Setup: 1. Connect a wire between pins PA0 and LED0. 2. Connect a wire between pins PB0 and SW0 (This connection is to make the device enter sleep mode). 3. Connect a wire between pins PD0 and SW1 (External Interrupt INT0 connection). Without using STK600 connect one LED circuit as shown in Figure 3-1 at PA0 and two switch circuits, one at PB0 and another at PD0. By running the example code LED0 will be turned ON. Once SW0 is pressed LED0 is turned OFF and the device enters sleep mode. Now a switch action on SW1 wakesup the device and turns ON LED0. 8 Atmel AVR1200 8468A-AVR-11/11 Atmel AVR1200 4 Driver implementation This application note includes a source code package written for Atmel AVR Studio 5 IDE with AVR tool chain. Please note that this external interrupt driver is not intended for use with high-performance code. It is designed as a library to get started with the external interrupts. The example codes included are: 1. Basic external interrupt usage on Atmel ATmega2560. 2. Nested external interrupt usage on ATmega2560. 3. Generating software interrupt with external interrupt pin configured as output in ATmega2560. 4. External interrupt for device wake-up of ATmega2560. 5 Resources • Atmel megaAVR datasheets http://www.atmel.com/avr • Atmel AVR Studio with help files http://www.atmel.com/avrstudio 9 8468A-AVR-11/11 6 Atmel technical support center Atmel has several support channels available: • Web portal: http://support.atmel.no/ All Atmel microcontrollers • Email: [email protected] All Atmel AVR products • Email: [email protected] All 32-bit AVR products Please register on the web portal to gain access to the following services: • • • • • 10 Access to a rich FAQ database Easy submission of technical support requests History of all your past support requests Register to receive Atmel microcontrollers’ newsletters Get information about available trainings and training material Atmel AVR1200 8468A-AVR-11/11 Atmel AVR1200 7 Table of contents Features ............................................................................................... 1 1 Introduction ...................................................................................... 1 2 External interrupt – overview.......................................................... 2 2.1 External interrupt vectors .................................................................................... 2 2.2 External interrupt sensing.................................................................................... 3 2.2.1 Asynchronous sensing in ATmega2560 .................................................................... 4 2.2.2 Synchronous sensing in ATmega2560 ...................................................................... 4 2.3 Interrupt response time ....................................................................................... 4 2.4 Interrupt priority ................................................................................................... 4 2.5 Important points to be noted when using external interrupts .............................. 5 3 Getting started ................................................................................. 6 3.1 Task 1 – Basic external interrupt usage.............................................................. 6 3.2 Task 2 – Nested external interrupt usage ........................................................... 7 3.3 Task 3 – External interrupt based on signal change on pin ................................ 7 3.4 Task 4 – External interrupt for device wake-up................................................... 8 4 Driver implementation ..................................................................... 9 5 Resources......................................................................................... 9 6 Atmel technical support center .................................................... 10 7 Table of contents ........................................................................... 11 11 8468A-AVR-11/11 Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: (+1)(408) 441-0311 Fax: (+1)(408) 487-2600 www.atmel.com Atmel Asia Limited Unit 01-5 & 16, 19F BEA Tower, Milennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon HONG KONG Tel: (+852) 2245-6100 Fax: (+852) 2722-1369 Atmel Munich GmbH Business Campus Parkring 4 D-85748 Garching b. Munich GERMANY Tel: (+49) 89-31970-0 Fax: (+49) 89-3194621 Atmel Japan 16F, Shin Osaki Kangyo Bldg. 1-6-4 Osaki Shinagawa-ku Tokyo 104-0032 JAPAN Tel: (+81) 3-6417-0300 Fax: (+81) 3-6417-0370 © 2011 Atmel Corporation. All rights reserved. ® ® ® ® ® Atmel , Atmel logo and combinations thereof, AVR , AVR Studio , megaAVR , STK , and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others. Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. 8468A-AVR-11/11