USING Multiple INTERRUPTS IN Atmega16 (INT0, INT1)

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys am stuck in a problem. Am doing a project where i have to count interrupts from 2 or 3 external  sources(switch).

 

The Problem is to Use multiple interrupts in the same program i.e. I am trying to use INT0 and INT1 in same program, to update two different counter using a external switch.

Individually INT0 and INT1 both works fine alone but together only INT1 works.

 

I tried this with INT0, INT1 and INT2. In this case only INT2 works.

 

I have tested the program both in hardware and in Proteus 8.

Both results are same.

 

AM ATTACHING THE Atmel STUDIO Project Files With PROTEUS FILE Please SEE.

 

Need Help 

Thanks In Advance.

Attachment(s): 

This topic has a solution.
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not going to bother touching your rar file.

I'd guess that you don't exit your isr so the others don't get a chance. Normally, an interrupt won't interrupt a interrupt until the isr has completed.

 

The usual response when people say they're using switches on interrupts is that it is a bad idea due to contact bounce. Use a timer tick to poll the switch inputs and debounce.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use multiple inputs on one ISR, look on the net for changedbits:

 

ISR (PCINT0_vect)
{
	uint8_t changedbits;

	changedbits = PINA ^ portbhistory;
	portbhistory = PINA;
	// do what you want with specific port changed -> not included in here
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank You For Your Valuable Reply.

 

I want to tell u that i have tried the code with square wave function generator of 5v peak. Then also i am getting the same problem. I am not getting why only ISR of INT2 is working when ISR of both INT0 and INT1 are declared. 

I have even tried with internal pull-up activated.

 

Please HELP 

ISR(INT0_vect)


{
    //if(INTF0)
    global_counts_m1 += 1;
    //PORTA=0xff;
    
}
ISR(INT1_vect)
{
    //if(INTF1)
    global_counts_m2 += 1;
    //PORTA=0xff;
    
}
void init_interrupt()
{


	// disable interrupts while initializing
	cli();
	// initialize the global state
	global_counts_m1 = 0;
	global_counts_m2 = 0;

		
			//PORTD=1<<2;				//PULL UP ACTIVATED
			//PORTD=1<<3;	

			GICR=(1<<INT0);
			GICR=(1<<INT1);

			MCUCR=1<<ISC11 | 1<<ISC10;
			MCUCR=1<<ISC01 | 1<<ISC00;

			//MCUCSR=1<<ISC2;
			//GICR=(1<<INT2);
	sei();
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are the shared variables declared volatile? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You've only shown me half the code! The other half is probably where the problem is. You need to declare your global_counts variables as volatile. Clawson has written a tutorial on this and i wrote a tutorial on 'the traps when using interrupts'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 

Here I am providing the Full Code and The Global Variables Are Volatile .... Please Have a Look

/*
 * meter_project.c
 *
 * Created: love u all 6:01:52 PM
 *  Author: Arnab
 */ 


#include <avr/io.h>                 
#include<avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/LCD_mega16.h>
#define F_CPU 16000000UL
	
void init_interrupt();
int get_counts_m1();
int get_counts_m2();
//volatile int get_counts_m3();

volatile unsigned int global_counts_m1;
volatile unsigned int global_counts_m2;
//volatile unsigned int global_counts_m3;

int main()
{	
	DDRC=0xFF;
	DDRA=0xFF;
	DDRD=0x00;
	DDRB=0x00;
	
	unsigned int m1;
	unsigned int m2;
	
	init_interrupt();

	lcd_init();
	lcd_cmd(0x0C);
	lcd_gotoxy1(0);
		
	while(1)
	{
	m1=get_counts_m1();
	m2=get_counts_m2();

	lcd_gotoxy1(0);
	lcd_showvalue(m1);
		
	lcd_gotoxy1(6);
	lcd_showvalue(m2);
	
		
	}
	
}

ISR(INT0_vect)
{
	//if(INTF0)
	global_counts_m1 += 1;
	//PORTA=0xff;
	
}
ISR(INT1_vect)
{
	//if(INTF1)
	global_counts_m2 += 1;
	//PORTA=0xff;
	
}


void init_interrupt()
{


	// disable interrupts while initializing
	cli();
	// initialize the global state
	global_counts_m1 = 0;
	global_counts_m2 = 0;

		
			//PORTD=1<<2;				//PULL UP ACTIVATED
			//PORTD=1<<3;	
			//PORTB=1<<2;
			GICR=(1<<INT0);
			GICR=(1<<INT1);

			MCUCR=1<<ISC11 | 1<<ISC10;
			MCUCR=1<<ISC01 | 1<<ISC00;
			//MCUCSR=1<<ISC2;
			//GICR=(1<<INT2);
	sei();
}

int get_counts_m1()
{
	cli();
	int tmp = global_counts_m1;
	sei();
	return tmp;
}
int get_counts_m2()
{
	cli();
	int tmp1 = global_counts_m2;
	sei();
	return tmp1;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:

Are the shared variables declared volatile? 

 

The shared Global Variables Are volatile ... please Have a kind look on the code i have Posted Now.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
			GICR=(1<<INT0);
			GICR=(1<<INT1);

What do you think GICR holds after those two lines?

 

I would suggest either:

			GICR = (1 << INT0);
			GICR |= (1 << INT1);

or perhaps the more obvious:

			GICR = (1 << INT0) | (1 << INT1);

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

            GICR=(1<<INT0);
            GICR=(1<<INT1);

What do you think GICR holds after those two lines?

 

I would suggest either:

			GICR = (1 << INT0);
			GICR |= (1 << INT1);

or perhaps the more obvious:

			GICR = (1 << INT0) | (1 << INT1);

 

Thank you , Thank You very Much.... The Code is Running Awesome Without any problem.... 

I changed 

   GICR=(1<<INT0);
            GICR=(1<<INT1);

to 

GICR=(1<<INT0)|(1<<INT1);

and its working ..... 

Thank You again

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You made the same mistake here too:

			MCUCR=1<<ISC11 | 1<<ISC10;
			MCUCR=1<<ISC01 | 1<<ISC00;

Once again either:

			MCUCR = (1 << ISC11) | (1 << ISC10);
			MCUCR |= (1 << ISC01) | (1 << ISC00);

or

			MCUCR = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00);

(the latter obviously more efficient in terms of both speed and size than the former).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This repeated construct caught my eye:

//if(INTF1)
	

Yes, it is in a comment--now.  But the fact that it is there implies to me that you don't realize that  INTF1 is a simple #define, probably 1.  So of what use is "if (1)..."?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.