Show Posts
Pages: 1 ... 134 135 [136] 137 138 ... 280
2026  Community / Bar Sport / Re: A series of photographs on: January 25, 2013, 10:16:26 pm

Wonderful! True art!
2027  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 08:21:50 pm
So the cli() and sei() can be dispensed with, and the minimal code to do the job ends up like this, assuming the interrupt is disabled going in:

Code:
   EICRA = _BV(ISC11);          //external interrupt 1 on falling edge
    _delay_loop_1(2);            //allow time for the interrupt caused by setting ISCxx (#include <util/delay_basic.h>)
    EIFR = _BV(INTF1);           //clear the interrupt flag
    EIMSK = _BV(INT1);           //enable external interrupt 1

Edit 26Jan2013: Playing with this again today, it looks like the call to _delay_loop_1() is not needed. Might have gotten myself wrapped around the axle a bit there last night smiley-red

I wonder if External Interrupts work differently by design, accident, or mistake.

Knowing what I know now, it seems it would have to be one of the latter. Maybe someone can think of a reason that it should work this way, but at the moment, I am at a loss  smiley-roll
2028  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 08:06:43 pm
AHA! That app note was right. Setting the sense control bit (ISCxx) causes the interrupt. Using the code below, I didn't see it earlier, but by adding delay(1) I see it now. Wasn't seeing it before no doubt because the code was picking up EIFR before the four clock cycles it takes to respond to an interrupt.

Code:
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

#define LED LED_BUILTIN

volatile boolean int1Flag;
boolean ledState;
uint8_t eifr1, eifr2;

void setup(void)
{
    delay(1000);
    Serial.begin(115200);
    pinMode(LED, OUTPUT);

    cli();
    EICRA = _BV(ISC11);          //external interrupt 1 on falling edge
    delay(1);            //   <------- AHA!
    eifr1 = EIFR;
    EIMSK = _BV(INT1);           //enable external interrupt 1
    eifr2 = EIFR;
    EIFR = _BV(INTF1);
    sei();
   
    Serial << "EIFR1=0x" << _HEX(eifr1) << " EIFR2=0x" << _HEX(eifr2) << endl;
}

void loop(void)
{
    if (int1Flag) {
        int1Flag = false;
        digitalWrite(LED, ledState = !ledState);
        Serial << millis() << endl;
    }
}

ISR(INT1_vect)
{
    int1Flag = true;
}

Output:

Code:
EIFR1=0x2 EIFR2=0x2
1909
2910
3909
4910
2029  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 07:59:07 pm
Does changing the interrupt direction to raising edge give the same behaviour?

Quote
//external interrupt 1 on falling edge

Lefty

Yes, I tried that too. Just verified it again as well.

I found these two comments in an application note, but they don't fit the scenario here. It looks to me like enabling the interrupt causes the interrupt.

Quote
4. When changing the ISCn bit, an interrupt can occur. Therefore, it is
recommended to first disable INTn by clearing its Interrupt Enable bit in the
EIMSK Register.
5. Before enabling an interrupt, it is recommended to clear the flag bit of the
corresponding interrupt because when the flag bit is set, the interrupt will be
triggered the moment we enable the interrupt.
2030  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 07:42:02 pm
That did it!

Wow.  That's strange.  And annoying.  I know some of the interrupt types are automatically cleared when enabled.  I wonder if External Interrupts work differently by design, accident, or mistake.

Agree, not at all what I would have expected. Clearing on enable might make a little more sense. I wondered if it was some kind of latent thing, maybe while the bootloader did its thing or whatever, but it just doesn't make sense if the interrupt wasn't enabled along the way.

I sure appreciate your help on this one!
2031  Using Arduino / Project Guidance / Re: frequency counter using optoisolator,arduino uno on: January 25, 2013, 05:52:22 pm
Here is a frequency counter library for Arduino. Works well. I'm currently working on a GPS-based frequency counter for improved accuracy.
2032  Community / Bar Sport / Re: Look what I found... on: January 25, 2013, 05:10:43 pm
.... in the street outside my house this morning.

Wth?

The gods must be crazy.
2033  Community / Bar Sport / Re: are do we know on: January 25, 2013, 05:04:03 pm
Actually, that's a pretty good phonetic rendering, I'm impressed! I'd have been way more impressed had it actually known "Arduino"! smiley-grin
2034  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 04:45:56 pm
Try this...

That did it! I had tried resetting the interrupt flag, and I had tried inhibiting interrupts, but I hadn't put them together. Investigating further, it sure looks like the act of enabling the INT1 interrupt causes the interrupt flag to be set. This code

Code:
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

#define LED LED_BUILTIN

volatile boolean int1Flag;
boolean ledState;
uint8_t eifr1, eifr2;

void setup(void)
{
    delay(1000);
    Serial.begin(115200);
    pinMode(LED, OUTPUT);

    cli();
    EICRA = _BV(ISC11);          //external interrupt 1 on falling edge
    eifr1 = EIFR;
    EIMSK = _BV(INT1);           //enable external interrupt 1
    eifr2 = EIFR;
    EIFR = _BV(INTF1);
    sei();
   
    Serial << "EIFR1=0x" << _HEX(eifr1) << " EIFR2=0x" << _HEX(eifr2) << endl;
}

void loop(void)
{
    if (int1Flag) {
        int1Flag = false;
        digitalWrite(LED, ledState = !ledState);
        Serial << millis() << endl;
    }
}

ISR(INT1_vect)
{
    int1Flag = true;
}

results in

Code:
EIFR1=0x0 EIFR2=0x2
1321
2322
3322
4323

I also discovered that using the Arduino interrupt functions causes the same undesirable behavior as my original code, so I feel just a little better:

Code:
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

#define LED LED_BUILTIN

volatile boolean int1Flag;
boolean ledState;

void setup(void)
{
    delay(1000);
    Serial.begin(115200);
    pinMode(LED, OUTPUT);
    attachInterrupt(1, myInt1Handler, FALLING);
}

void loop(void)
{
    if (int1Flag) {
        int1Flag = false;
        digitalWrite(LED, ledState = !ledState);
        Serial << millis() << endl;
    }
}

void myInt1Handler(void)
{
    int1Flag = true;
}


2035  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 01:55:37 pm

Board?  (I don't think that matters but, at this point, it's a good idea to assume nothing.)

Chip-on-a-breadboard with Optiboot, telling the IDE it's an Uno smiley-grin  Would be easy enough to try a real one, though.
2036  Using Arduino / General Electronics / Re: What circuit drawing tool is this? on: January 25, 2013, 01:54:17 pm
OMG, no resistor shown on that drawing, the leds are doomed.  smiley-wink

Lefty

Yeah I do prefer the zigzags to the boxen. But that was another thread and I certainly don't want to dig it up smiley-lol
2037  Using Arduino / General Electronics / Re: Timer Tic Beep on: January 25, 2013, 01:52:25 pm
Another 555 is a possibility.
2038  Using Arduino / General Electronics / Re: What circuit drawing tool is this? on: January 25, 2013, 01:44:10 pm
Could be Eagle, just judging by the colors. I like Eagle, but there was a learning curve. I found it less than intuitive at first.
2039  Using Arduino / General Electronics / Re: Timer Tic Beep on: January 25, 2013, 01:36:29 pm
Tiny pic, can't read it...
2040  Using Arduino / Microcontrollers / Re: External interrupt fires early? on: January 25, 2013, 01:27:28 pm
what happens if you put varying length delays before enabling the interrupt, does that alter the 999 value.

Yes, for example, with delay(2000) instead, I get:

Code:
1999
2787
3787
4788
5788
Pages: 1 ... 134 135 [136] 137 138 ... 280