For what you are saying, I can only think of three possibilities: 1) your assumption that the function is called on every I2C message is incorrect, 2) your program has an error (possibly a memory leak) in some unrelated function, which causes damage to the call variable. or 3) two or more threads simultaneously call your function, and calls increase in a different way than you expect, use instead of == , if this solves the problem, then you are working in an environment with milti-threaded, and you do not know.
You need an exact method to determine the exact value of calls if you do not have a debugger and you do not have the means to display the text. The only thing you left is playing time. I donโt know that you are a compiler, but Iโm sure that it contains some useful synchronization functions, so I would loop before incrementing for 10+ seconds calls, and after incrementing again 10+ calls seconds, for example:
sleep(1000*(10+calls)); ++calls; sleep(1000*(10+calls)); if(calls>8){
I would (chronometer in hand) expect a delay (10 + 0 plus 10 + 1) = 21 seconds on the first call, 23 seconds on the second call, 25 in the third and so on. Thus, I could be sure that the value of the calls started from 0, and then it gradually increased to 9.
In addition, you should check what you expect from what you don't expect, so instead:
++calls; if (calls==0) while (1);
do the following:
++calls; if (calls==1) while (1);
Thus, if your program freezes, you can be sure that the call value is exactly 1, and not regardless of zero. If you count one valid I2C relationship and your program freezes, the transition from 0 to 1 was performed correctly, so change the hang operator accordingly:
++calls; if (calls==2) while (1);
Again, if you count 2 valid I2C messages before your program freezes, it means the transition from 1 to 2 was correct, etc.
Another suggestion, try the following:
uint8_t Times(void){ static uint8_t calls = 0; return ++calls; } void callback(void){ if (Times()>8) {
And this:
void callback(void){ static uint8_t calls = 0; if (calls++>7) {
Hope this helps.