Hi,
I got an issue which I'd be happy to see how you'd approach it.
There's a certain IC which my uC controls.
There's also a global variables, which forms a counter
- When Counter = 0, the uC powers off the IC.
- When Counter = 1, the uC Powers on the IC.
- When Counter >1, the uC does keeps the IC as is (powered On).
Each task that runs on the uC increments the Counter when it needs to work with it, and decrements it when it doesn't need it anymore.
That way, the uC powers on the IC when the 1st task to use the IC increments the Counter (in that case, Counter = 1), and powers if off when the last task to use that IC decrements that counter (in that case, Counter = 0).
Now, I wanna use that IC as well.
However, I noticed that there're some cases in which some task decrements the counter more times than it increments it, which is illegal, and therefore the IC remains off, which blocks my usage of the IC.
It'd be complex and would take a while to figure out which task violates the rules, and until I figure it out, I wanna work'around it.
I thought of doing the following, what do you think?
C:
void Decrement_Counter ()
{
Counter--;
if (Counter == 0xFFFFFFFF)
{
Counter = 0;
TURN_ON_IC();
}
else if (Counter == 0)
{
TURN_OFF_IC();
}
}
C:
void Increment_Counter ()
{
Counter++;
if (Counter == 1)
TURN_ON_IC();
}