Interrupts and Global Variable Access

...and of course counter and the flag MUST be declared volatile.