ATmega16 - Help using INT2

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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:
#define LD_BUZZER_LED_ON    PORTC = 0b00001100
#define LD_BUZZER_LED_OFF    PORTC = 0x00
In "int main(void)" section
Code:
MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
GICR = 1<<INT2 | 1<< INT1 | 1<<INT0;
GIFR = 1<<INTF0 | 1<<INTF0| 1<<INTF2;
In "ISR(INT2_vect)" section
Code:
 ISR(INT2_vect){
    if (INT2){
        LD_BUZZER_LED_ON;
    }

    else {
        LD_BUZZER_LED_OFF;
    }
}
Please advise
Thank you in advance
 

kubeek

Joined Sep 20, 2005
5,794
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.
 

Thread Starter

Shah_Key

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

I tried:
Rich (BB code):
ISR(INT2_vect){
        LD_BUZZER_LED_ON;
}
But it still didn't work
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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.
Please see above message
Thank you
 

kubeek

Joined Sep 20, 2005
5,794
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?
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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?

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:
Rich (BB code):
/*
 * T_S__0_01.c
 */ 


#define F_CPU    1000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>


ISR(INT2_vect);
//#define LD_DETECTED            PORTB = 0b00000100
#define LD_BUZZER_LED_ON    PORTC = 0b00001100
#define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin

int main(void)
{
    unsigned char i;
    
    sei();                // Enable Global Interrupts
    
    //////////////////////////////
    MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    GICR = 1<<INT2 | 1<< INT1 | 1<<INT0;
    GIFR = 1<<INTF0 | 1<<INTF0| 1<<INTF2;
    ///////////////////////////////
    //DDRB=0xFF;

}


 ISR(INT2_vect)
{
//    if (DDRB = 0B00000100){
        LD_BUZZER_LED_ON;
//    }

//    else {
//        LD_BUZZER_LED_OFF;
//    }
}
Thank you
 

kubeek

Joined Sep 20, 2005
5,794
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.
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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.
Still no output.
Rich (BB code):
/*
 * T_S__0_01.c
 */ 


#define F_CPU    1000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>


ISR(INT2_vect);
//#define LD_DETECTED            PORTB = 0b00000100
#define LD_BUZZER_LED_ON    PORTC = 0b00001100
#define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin

int main(void)
{
    unsigned char i;
    
    sei();                // Enable Global Interrupts
    
    //////////////////////////////
    MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    GICR = 1<<INT2;
    ///////////////////////////////
    while(1);
}


 ISR(INT2_vect)
{
//    if (DDRB = 0B00000100){
        LD_BUZZER_LED_ON;
//    }

//    else {
//        LD_BUZZER_LED_OFF;
//    }
}
 

kubeek

Joined Sep 20, 2005
5,794
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.
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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.
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?
 

kubeek

Joined Sep 20, 2005
5,794
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.
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
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.
Could you please help me setting DDRC= 0b00001100; please?
 

Thread Starter

Shah_Key

Joined Jan 8, 2013
23
just put it in the main
Like this:

Rich (BB code):
/*
 * T_S__0_01.c
 */ 


#define F_CPU    1000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>


ISR(INT2_vect);
//#define LD_DETECTED            PORTB = 0b00000100
#define LD_BUZZER_LED_ON    PORTC = 0b00001100
#define LD_BUZZER_LED_OFF    PORTC = 0x00        // Turns off the Pin

int main(void)
{
    unsigned char i;
    
    sei();                // Enable Global Interrupts
    
    DDRC= 0b00001100;
    
    //////////////////////////////
    MCUCR = 1 << ISC11 | 1<< ISC10 | 1<< ISC01 | 1<<ISC00;
    GICR = 1<<INT2;
    ///////////////////////////////
    while(1);
}


 ISR(INT2_vect)
{
        LD_BUZZER_LED_ON;
}
 
Top