Extra INT0 interrupt is counted

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

I wrote below code for testing the external interrupt. But first data transmitted on serial port is 5 and subsequently 4 are getting added correctly. However if I clear the counter (currently commented) after giving 1 msec delay after sei(), I am getting 4, 8, 12 and so on correctly. If delay after sei() is disabled, I still have same issue and counting sequence is 5, 9, 13 etc. even if counter is cleared. What could be the reason for this? I am using Atmega328P controller. USART0Init() and USART0SendByte are not attached and they are proven.

volatile uint8_t counter=0;

ISR(INT0_vect)
{
    counter++;
}

int main(void)
{
    USART0Init();
    DDRD|=1<<PD2;
    PORTD &= ~(1<<PD2);  // Make it low. By default anyway it is low.
    EICRA|=((1<<ISC00)|1<<(ISC01));  //Rising Edge
    EIMSK|=1<<INT0;      // Enable Interrupt
    sei();              // Enable I bit
    _delay_ms(1);
    //counter=0;       // If counter is not cleared, counting sequence has issue
    while(1)
    {
        uint8_t count;
        // Send 4 pulses on INT0 pin
        for(count=0;count<4;count++)
        {
            PORTD|=1<<PD2;
            _delay_ms(1);
            PORTD &= ~(1<<PD2);
            _delay_ms(1);
        }
        USART0SendByte(counter);
        _delay_ms(2000);
    }
}

 

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

Do you get the same result in the simulator?

 

It may well be a spurious pulse, from setting up the interrupt.  I.e. INTF0 in EIFR is set.  Before the sei() clear the flag.  EIFR = (1<<INTF0);

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.

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

Clearing EIFR worked. It was not happining in all boards (I am Using nano chinese version that uses CH340). Only few are giving this. So could be related to some HW issue.

 

I have another question. I connected INT0 to another nano board port pin (configured as output). In such case I configured INT0 as input. But if I do not enable internal pull up, I observed that counting is not accurate. When input is driven by another output, I should not need pull up right? I am confused a bit.

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

How long is your wire from the output to the INT0 input?  Too long and you can get reflections which can create multiple pulses out of one.