Interrupt Service routine

Hi,

Hardware environment: Im using an Arduino 2560. Blinking LED, on digital output port 4. Port 4 is also connected to digital input poort 2.

Problem: I have setup an Interrupt Service routine which is triggered on a falling edge of interrupt 0 (port 2). Within the Interrupt Service Routine (ISR) I just increment a counter. I assume when the LED is blinking 5 times, the counter in the ISR displays 5. However the counter displays 254. How is that possible ?

Software:

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <string.h>

#include “bits.h” //Library to use bit functions

#define PORT_PG_DDRG DDRG
#define PORT_G PORTG

char buffer[50];
int interruptHandlerCalled;

void setup()
{
// PORTG maps to Arduino digital pin 5
PORT_PG_DDRG = PORT_PG_DDRG | B00100000; // set PORTG (digital 5) to output

// Install interrupt handler “LEDIsBlinking” on digital I/O pin 2.
attachInterrupt(0, LEDIsBlinking, FALLING); //pin 2

Serial.begin(115200);
}

void loop()
{
int nrOfTimesBlinking, // Defines how many times the LED will blink
delayBlinking; // Defines the delay between the blinking time of the LED

uint8_t pinLayout;

delayBlinking = 500; // Delay between the LED is on and off

// PORTG PG5 maps to Arduino 2560 digital pin 4.
pinLayout = B00100000;
interruptHandlerCalled = 0;

for (nrOfTimesBlinking = 0; nrOfTimesBlinking < 5; nrOfTimesBlinking++)
{
PORT_G = pinLayout;
delay(delayBlinking);
PORT_G = PORT_G & ~pinLayout;
delay(delayBlinking);
}
sprintf(buffer, “Number of interrupts is: %d”, interruptHandlerCalled);
Serial.println(buffer);
delay(1000);
}

void LEDIsBlinking()
{
interruptHandlerCalled++;
}

interruptHandlerCalled needs to be declared volatile.

volatile int interruptHandlerCalled;

Pete

 int nrOfTimesBlinking, // Defines how many times the LED will blink
      delayBlinking;     // Defines the delay between the blinking time of the LED

Neither of those comments is correct. The variables are declared, and the type is defined. The values of the variables are what the comments address, and they are NOT defined.

@GettingWork: Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Read this before posting a programming question

How to use this forum