Using External Interrupts For MegaAVR Devices
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
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 STK600 starter kit for functionality.
Rev. 8468A-AVR-11/11
1 2 3 4 5 6 7 8
Atmel AVR1200
8468A-AVR-11/11
Atmel AVR1200
Vector no. 9 10 Program address $0010 $0012 Source INT7 PCINT0 Port pins in ATmega2560 PE7 PCINT0:7 PB0:7 PCINT8 PE0 PCINT9:15 PJ0:PJ6 PCINT16:23 PK0:7 Interrupt definitions External Interrupt Request 7 Pin Change Interrupt Request 0
11 12
$0014 $0016
PCINT1 PCINT2
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.
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.
ISCn1 0 0 1 1 ISCn0 0 1 0 1 Description The low level of INTn generates an interrupt request Any edge of INTn generates an interrupt request The falling edge of INTn generates an interrupt request The rising edge of INTn generates an interrupt request
NOTE
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.
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.
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 150R 10K VTG LED VTG BC847W 10K To port pin 1 2 3 4 VTG 150R 150R VTG Switch
To port pin
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.
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.
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
10
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
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