ATmega16 - Help using INT2

Discussion in 'Embedded Systems and Microcontrollers' started by Shah_Key, May 7, 2013.

  1. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    Hello

    I am using an ATmeta16 and I am trying to trigger an output when INT2 becomes active(~4V or so). However I cannot get it working. This is what I have done so far:

    In "ISR(ADC_vect)" section
    Code (Text):
    1.  
    2. #define LD_BUZZER_LED_ON    PORTC = 0b00001100
    3. #define LD_BUZZER_LED_OFF    PORTC = 0x00
    4.  
    In "int main(void)" section
    Code (Text):
    1.  
    2. MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    3. GICR = 1<<INT2 | 1<< INT1 | 1<<INT0;
    4. GIFR = 1<<INTF0 | 1<<INTF0| 1<<INTF2;
    5.  
    In "ISR(INT2_vect)" section
    Code (Text):
    1.  
    2.  ISR(INT2_vect){
    3.     if (INT2){
    4.         LD_BUZZER_LED_ON;
    5.     }
    6.  
    7.     else {
    8.         LD_BUZZER_LED_OFF;
    9.     }
    10. }
    11.  
    Please advise
    Thank you in advance
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    No idea why you´re setting the flags in GIFR, but that should not matter much.
    What is the INT2 in the if(INT2) in the isr? Try leaving just the LD_BUZZER_LED_ON without the condition and see what happens.
    Also since you have int1 and int0 enabled, make sure you have the corresponding isrs in the code, or else the mcu will reset if the pin triggers.
     
  3. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    I have just written
    "ISR(INT2_vect);" in the beginning

    I tried:
    Code ( (Unknown Language)):
    1. ISR(INT2_vect){
    2.         LD_BUZZER_LED_ON;
    3. }
    But it still didn't work
     
  4. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    Please see above message
    Thank you
     
  5. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Please post the whole code, do you have global interrupts enabled, that is do you call sei() in main? Did you check that your define really does set the leds on?
     
  6. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0

    Yes the global interrupts are enabled in the main.
    And yes the LEDs do work for definate when i tested it on another code.

    Whole code:
    Code ( (Unknown Language)):
    1.  
    2. /*
    3.  * T_S__0_01.c
    4.  */
    5.  
    6.  
    7. #define F_CPU    1000000UL
    8. #include <avr/delay.h>
    9. #include <avr/io.h>
    10. #include <string.h>
    11. #include <avr/interrupt.h>
    12.  
    13.  
    14. ISR(INT2_vect);
    15. //#define LD_DETECTED            PORTB = 0b00000100
    16. #define LD_BUZZER_LED_ON    PORTC = 0b00001100
    17. #define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin
    18.  
    19. int main(void)
    20. {
    21.     unsigned char i;
    22.    
    23.     sei();                // Enable Global Interrupts
    24.    
    25.     //////////////////////////////
    26.     MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    27.     GICR = 1<<INT2 | 1<< INT1 | 1<<INT0;
    28.     GIFR = 1<<INTF0 | 1<<INTF0| 1<<INTF2;
    29.     ///////////////////////////////
    30.     //DDRB=0xFF;
    31.  
    32. }
    33.  
    34.  
    35.  ISR(INT2_vect)
    36. {
    37. //    if (DDRB = 0B00000100){
    38.         LD_BUZZER_LED_ON;
    39. //    }
    40.  
    41. //    else {
    42. //        LD_BUZZER_LED_OFF;
    43. //    }
    44. }
    45.  
    46.  
    Thank you
     
  7. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Change GICR = 1<<INT2 | 1<< INT1 | 1<<INT0; to just GICR = 1<<INT2;
    Delete the line with GIFR
    Add while(1); to the end of main.
     
  8. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    Still no output.
    Code ( (Unknown Language)):
    1.  
    2. /*
    3.  * T_S__0_01.c
    4.  */
    5.  
    6.  
    7. #define F_CPU    1000000UL
    8. #include <avr/delay.h>
    9. #include <avr/io.h>
    10. #include <string.h>
    11. #include <avr/interrupt.h>
    12.  
    13.  
    14. ISR(INT2_vect);
    15. //#define LD_DETECTED            PORTB = 0b00000100
    16. #define LD_BUZZER_LED_ON    PORTC = 0b00001100
    17. #define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin
    18.  
    19. int main(void)
    20. {
    21.     unsigned char i;
    22.    
    23.     sei();                // Enable Global Interrupts
    24.    
    25.     //////////////////////////////
    26.     MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    27.     GICR = 1<<INT2;
    28.     ///////////////////////////////
    29.     while(1);
    30. }
    31.  
    32.  
    33.  ISR(INT2_vect)
    34. {
    35. //    if (DDRB = 0B00000100){
    36.         LD_BUZZER_LED_ON;
    37. //    }
    38.  
    39. //    else {
    40. //        LD_BUZZER_LED_OFF;
    41. //    }
    42. }
    43.  
    44.  
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Ok probably last few ideas, try deleting the ISR(INT2_vect); in the beginning.

    Also you can try toggling a pin as a first thing in the code, to check if the code resets somehow.

    Which brings me to this, where do you set the direction of the outputs? :rolleyes: All pins are inputs until you set them otherwise.
     
  10. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    I tried deleting ISR(INT2_vect); in the beginning. - Sill didnt work

    Isn't
    #define LD_BUZZER_LED_ON PORTC = 0b00001100
    setting them as an output?
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    No, that is defining what data you want want on outputs, if they were enabled. You need to set DDRC= 0b00001100;
    Actually if you have them as inputs, then you are setting pullups on them.
     
  12. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    Could you please help me setting DDRC= 0b00001100; please?
     
  13. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    just put it in the main
     
  14. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0
    Like this:

    Code ( (Unknown Language)):
    1.  
    2. /*
    3.  * T_S__0_01.c
    4.  */
    5.  
    6.  
    7. #define F_CPU    1000000UL
    8. #include <avr/delay.h>
    9. #include <avr/io.h>
    10. #include <string.h>
    11. #include <avr/interrupt.h>
    12.  
    13.  
    14. ISR(INT2_vect);
    15. //#define LD_DETECTED            PORTB = 0b00000100
    16. #define LD_BUZZER_LED_ON    PORTC = 0b00001100
    17. #define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin
    18.  
    19. int main(void)
    20. {
    21.     unsigned char i;
    22.    
    23.     sei();                // Enable Global Interrupts
    24.    
    25.     DDRC= 0b00001100;
    26.    
    27.     //////////////////////////////
    28.     MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    29.     GICR = 1<<INT2;
    30.     ///////////////////////////////
    31.     while(1);
    32. }
    33.  
    34.  
    35.  ISR(INT2_vect)
    36. {
    37.         LD_BUZZER_LED_ON;
    38. }
    39.  
    40.  
     
  15. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Yes that should work, but just to be sure get rid of the ISR(INT2_vect); i never needed it.
     
  16. Shah_Key

    Thread Starter New Member

    Jan 8, 2013
    23
    0

    I tried it but still didn't work.
     
Loading...