ATMega 328 software interrupts

Discussion in 'Programmer's Corner' started by Manmeet Singh, Nov 8, 2010.

  1. Manmeet Singh

    Thread Starter Active Member

    May 21, 2008
    37
    0
    Hello everyone Im planning on using the Arduino UNO for a project of mine and as im waiting for the order to get shipped I thought I would start on the code. Now I am familiar with interrupts but not with the ATMega 328 found on the Arduino UNO. How do I set up the interrupt ISRs?

    According to http://arduino.cc/en/Reference/AttachInterrupt
    This is not for a specific ATMega processor
    Example

    void setup()
    {
    pinMode(pin, OUTPUT);
    attachInterrupt(0, blink, CHANGE);
    }

    void blink()
    {
    ...
    }

    but then elsewhere I have found this kind of code for the ATMega8


    // Definition of interrupt names
    #include < avr/io.h >
    // ISR interrupt service routine
    #include < avr/interrupt.h >

    // Install the interrupt routine.
    ISR(INT0_vect) {
    ....
    }

    void setup() {
    ...
    // Global Enable INT0 interrupt
    GICR |= ( 1 < < INT0);
    // Signal change triggers interrupt
    MCUCR |= ( 1 << ISC00);
    MCUCR |= ( 0 << ISC01);
    ....
    }

    Neither of them clear an interrupt flag in the ISR either which is strange
    to me. Any tips on the right way todo this?

    Thanks
     
  2. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    It all depends on which language and compiler you use and that will dictate the way of writing codes and usually varies from compiler to compiler. I don't know about the above, but the bottom one is from the more popular WinAVR or AVR-GCC. There are other compilers like mikroC, mikroBASIC, IAR Workbech, etc. which are all different. So now you have to choose which compiler you want and which language.

    Hope this helps.
    Tahmid.
     
  3. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    eg. This code is in WinAVR AVR-GCC
    Code ( (Unknown Language)):
    1. // Definition of interrupt names
    2. #include < avr/io.h >
    3. // ISR interrupt service routine
    4. #include < avr/interrupt.h >
    5.  
    6. // Install the interrupt routine.
    7. ISR(INT0_vect) {
    8. ....
    9. }
    10.  
    11. void setup() {
    12. ...
    13. // Global Enable INT0 interrupt
    14. GICR |= ( 1 < < INT0);
    15. // Signal change triggers interrupt
    16. MCUCR |= ( 1 << ISC00);
    17. MCUCR |= ( 0 << ISC01);
    18. ....
    This is in mikroBasic:
    Code ( (Unknown Language)):
    1. program something
    2.  
    3. sub procedure interruptInt0() org IVT_ADDR_INT0
    4. ....
    5. end sub
    6.  
    7. main:
    8.    GICR.INT0 = 1
    9.    MCUCR.ISC00 = 1
    10.    MCUCR.ISC01 = 1
    11. .....
    12. .....
    13. .....
    14. end.
    They are the same thing in different languages and different compilers.

    Hope this helps.
    Tahmid.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    In looking more closely at your Arduino interrupt effort the one thing I see missing is the pin assignment declaration. The statement that I added using bold red font tells the Arduion compiler that you have assigned the label "pin" to input number 13. It turns out that input 13 is the one that is connected to the heartbeat LED on the Arduino Duemilanove and the Arduino Uno.

    The attachInterrupt(0, blink, CHANGE) statement first tells the compiler that the interrupting signal is connected to pin 0. Next it tells the compiler to set up the ATMEGA328's internal registers that control interrupts (specifically the pin change interrupt) and points the interrupt to your interrupt service routine code contained in the function blink(). Lastly, the keyword CHANGE tells the compiler that you want the interrupt to occur each time the signal on input 0 changes from 0 to 1 or from one to 0.

    hgmjr
     
Loading...