AVR usage example--AVR external interrupt example

Publisher:祝福的4号Latest update time:2016-07-28 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When the system is running the main program normally, if there is suddenly an important task to be processed immediately, the system will save the current work and then process this task. After completing this important task, it will return to the original main program to continue running. This is an interrupt.

Once the main program enters the interrupt service routine, the AVR chip will automatically turn off the global interrupt, and no other interrupt requests will be executed during this period. The chip will automatically reopen the global interrupt until the interrupt program ends. (Note that during this period, some interrupt requests may be discarded, and some requests will leave interrupt request marks. Once the current interrupt is executed, the request with the interrupt mark may be responded to immediately. For example, the falling edge trigger of INT0 will leave an interrupt request mark, while the low level trigger will not leave an interrupt request mark.) If you want to respond to another more important interrupt while executing the interrupt service routine, you must add a statement to turn on the global interrupt in the interrupt service routine.

Use ICCAVR Application Builder to set up as shown below. You can set whether to use interrupts, rising delay, falling delay, low level, and any logic level change.

AVR Usage Example -- AVR External Interrupt Example - Jesse Rei - Jesse Rei's Blog (Indecisive)

Modify the generated program, DDRA = 0x01; PORTD = 0x0C;, add the MAIN function as follows

  •  

    //ICC-AVR application builder : 2006-12-8 17:04:44// Target : M16// Crystal: 7.3728Mhz#include #include unsigned int i=0;void port_init(void){ PORTA = 0x00; DDRA = 0x01; PORTB = 0x00; DDRB = 0x00; PORTC = 0x00; //m103 output on ly DDRC = 0x00; PORTD = 0x0C; //Enable the pull-up resistors for INT0 and INT1 DDRD = 0x00; //Must set the corresponding ports of INT0 and INT1 as inputs}#pragma interrupt_handler int0_isr:2void int0_isr(void){ //external interrupt on INT0 i++; //Perform operations in the interrupt }#pragma interrupt_handler int1_isr:3void int1_isr(void){ //external interupt on INT1 PORTA = 0x01; //Perform operations in the interrupt }//call this routine to initialize all peripheralsvoid init_devices(void){ //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); MCUCR = 0x08; //The falling edge of INT1 generates an asynchronous interrupt request, and the rising edge of INT0 is delayed GICR = 0xC0; //INT0 and INT1 enable TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized}void main(void){ init_devices(); while(1) //Infinite loop waiting for interrupt;}

 

Related explanation

  1. #pragma interrupt_handler int0_isr:2
  2. Conventional representation of interrupts: int0_isr has a corresponding application void int0_isr(void), 2 is the interrupt vector, the smaller the value, the higher the priority, and the priority of INT0 is second only to reset.
  3. MCU Control Register - MCUCR The MCU Control Register contains interrupt trigger control bits and general MCU functions.
  4. General Interrupt Control Register - GICR Enables or disables external interrupt requests

 

During use: In order to reduce interference, please add a 4.7K pull-up resistor to the interrupt pin. You can also add capacitor filtering to prevent jitter according to actual needs.

Keywords:AVR Reference address:AVR usage example--AVR external interrupt example

Previous article:Installation of AVR cross-compilation toolkit in Debian
Next article:AVR timer/counter application design

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号