INT0_vect not working as I expected, little help?

So I'm using a INT0 to monitor digital pin 2 for a falling edge and I'm getting squat.

Here's my code

#include <avr/io.h>
#include <avr/interrupt.h>

volatile boolean pinchange = true;

void setup()
{
   //setup the interrupt
  
  DDRD = DDRD ^ B00000100; //set pin to input

  PORTD = PORTD | B00000100; //set the pin to use internal pull-up resistor
  
  EICRA = EICRA | B00000010 ^ B00000001; //falling edge triggers the interrupt
  
  EIMSK = EIMSK | B00000001; //interrupt request enabled
  
}

ISR(INT0_vect)
{
  pinchange = true;  
}

Can anybody see anything I'm doing wrong? Is there a minimum time the line has to be driven low for the interrupt to recognize that the line as fallen?

Merve, any reason you are not using attachInterrupt?
http://www.arduino.cc/en/Reference/AttachInterrupt

None really, except I like no know whats going on, also I have read that attachinterrupt is slower than plotting out the code yourself.

I'll give it a go now though

Here's my new code, and it's still not working. I know it's not a wiring issue because if I use a while((PIND&B00000100)); it quite happily gets kicked out of the loop when the pin drops.

#include <avr/io.h>
#include <avr/interrupt.h>

volatile boolean pinchange = true;

void setup()
{
   //setup the interrupt
  
  DDRD = DDRD ^ B00000100; //set pin to input

  PORTD = PORTD | B00000100; //set the pin to use internal pull-up resistor
 /* 
  EICRA = EICRA | B00000010 ^ B00000001; //falling edge triggers the interrupt
  
  EIMSK = EIMSK | B00000001; //interrupt request enabled
  */
  attachInterrupt(0,pin_change,FALLING);
}
/*
ISR(INT0_vect)
{
  pinchange = true;  
}
*/

void pin_change()
{
pinchange = true;
}

Could it have something to do with me using a volatile boolean?

No, once again I missed something. Well, didn't miss something but it's not the masters fault. I can toggle the interrupt by hand, but the slaves code isn't working properly. That can wait till tomorrow.

All you need to know is this code is fine :stuck_out_tongue:

Why not just do this:

volatile boolean pinchange = false;

void setup()
{
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);// turn on pull-ups
  attachInterrupt(0,pin_change,FALLING);
}

void pin_change()
{
   pinchange = true;
}

My advice is to get your sketch logic working using the simple Arduino commands and if its not fast enough because of the function call overhead that Arduino adds to attafchInterrupt you can optimize that later.

This is all the sketch is designed to do, the whole purpose was me getting it to work using the avr-libc code instead of arduino.

And it did, from the start, it was a problem with my slave code.

But why (given this is the Arduino site) do this:

  DDRD = DDRD ^ B00000100; //set pin to input
  PORTD = PORTD | B00000100; //set the pin to use internal pull-up

When you can do this:

 pinMode(2, INPUT);
 digitalWrite(2, HIGH);// turn on pull-ups

It's exactly the same, and I'm learning off the datasheet, which gives examples in C and assembly - not arduino.

Hi Merve,

If you are interested in learning about the low level workings of the chip (which the Arduino abstractions are designed to hide) have a look at the avrfreaks forum. It's a really good site for technical discussions covering register level programming.

The code has exactly the same result, are you saying that because I used a different syntax in the ARDUINO IDE to program my ARDUINO board I'm not support to post in the ARDUINO forums?

Next time I'll just change the code into arduino syntax

I was simply pointing out that avrfreaks has more discussions geared towards register level technology so there are probably more people there that would be inclined to participate.

anyway, glad you got the problem sorted.

have fun!

 DDRD = DDRD ^ B00000100; //set pin to input

I believe that when this statement is called DDRD = 0x00 which is all pins
set to input. XORing it sets it to an output.

I wouldn't use XOR to set or reset bits. If you want to set a bit use OR. If you
want to reset a bit the use AND. The result of XOR is dependent on the previous
condition where as ORing with a '1" always produces "1" and ANDing with
a "0" always produces a "0".

I have some interrupt code using the AVR commands on my website http://www.wiblocks.com

(* jcl *)


www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org