interrupt based LEDs up counter on an atmega32

217 Views Asked by At

l am designing an interrupt based number counter which shows the values as they increment on 8 LEDs using an atmega32. My problem is my ISR(interrupt service routine)is not able to light up the LEDs as l increment from INT0below is the code l made, only the ISR is not lighting the LEDs

enter image description here

1

There are 1 best solutions below

0
On

In SR, the count variable is on the stack. Its value does not persist across invocations.


Here is your original code [with annotations]:

SR(INT0_vect)
{
    DDRA = 0xFF;
// NOTE/BUG: this is on the stack and is reinitialized to zero on each
// invocation
    unsigned char count = 0;

// NOTE/BUG: this will _always_ output zero
    PORTA = count;
// NOTE/BUG: this has no effect because count does _not_ persist upon return
    count++;

    return;
}

You need to add static to get the value to persist:

void
SR(INT0_vect)
{
    DDRA = 0xFF;
    static unsigned char count = 0;

    PORTA = count;
    count++;
}