Hi, Vahbiz:
What you need to implement is called a "critical section".
In your main routine you are decrementing a value that can be asynchronously incremented by an ISR. The decrement is probably not "atomic", which means that the operation requires a number of instructions, any one of which can be interrupted by the ISR.
Consider the following sequence:
In the main code, the value of "Count" is loaded into a register for decrementing.
The value in the register is then decremented.
-- The interrupt occurs. The following sequence occurs in the ISR:
-- The value of "Count" is loaded into a register for incrementing.
-- The value in the register is incremented.
-- The incremented value in the register is stored in "Count".
-- The interrupt returns
Back in the main code, the value of the decremented register is stored in "Count", overwriting the incremented value stored by the ISR.
So it appears that no increment took place, because the increment occurred during the critical section.
The solution is to make the decrement atomic (non-interruptible). Simply disable interrupts prior to the decrement, and re-enable them after a decrement.
In general, you need a critical section anytime an asynchronously-shared resource is manipulated. An ISR is typically treated as a critical section automatically by the hardware, by it raising the priority level. But you need to maintain the critical sections in the rest of your code.
Let me know if I didn't make sense. When I read what I have written, it often sounds like techno-babble.