-2

I´m doing a little programming on C with a dsPIC, I have found a little tiny problem that I don´t know why or how is it happenning. Compiler C30 for MPLAB

I have this code:

int Function1(){
.
.
.
while(1){
.
.
.
P1 = EPC96_1[18];  //Here the value of P1 = 0xB6
P2 = EPC96_1[19];
CRC_CCITT(EPC96_2, 18);   //in this function CRC_1 is calculated also CRC2
if(P1 != CRC_1){       //In the calculation of CRC_1 it comes always 0xB6
  P1++;                //Both values P1 and CRC1 are the same, stil it 
  if(P2 != CRC_2)      //comes here and continues with the break
    break;
  }
}
return 1;
}
//end of my function, return to main code

----Sorry, I didn´t specify, it supose not to come into the if, the != is supose to be like that, because what I want the program to do is when both variables are equal, it should return to the while(1) start.----

I looked up for the variables on the Watch of the MPLAB (8.92) and they are the same. I do not think the problem is in the code before or after or in any other place, but I could be wrong. Has anybody found the same problem during your experience time?

Thank you for your time.

  • 1
    You say `P1` is `0xB6` and that `CRC_1` also is `0xB6`. In that case `P1 == CRC_1` and the `if`-statement will not be followed (it has `!=`). – Kusalananda Jun 24 '16 at 12:30
  • How is `CRC_1` set by the `CRC_CCITT()` function? Did you mean `CRC_1 = CRC_CCITT(EPC96_2, 18)`? Or is it a global variable? – Kusalananda Jun 24 '16 at 12:43
  • In my experience, strange issues like these are due to my own bugs. I will never question the compiler unless two different compilers show different behaviour (which is *very* rare). – Kusalananda Jun 24 '16 at 12:49
  • 1
    Please post a [mcve] – KevinDTimm Jun 24 '16 at 14:04

2 Answers2

1

Assuming that P1 and CRC_1 are int variables.

According to the comments in your code, both variables will have value 0xB6. The if-statement checks for inequality (!=), not equality (==), so it will skip the block.

I suggest inserting printf()-statements to output the values of the two variables before and/or inside the if-statement. It may be that either one of them holds unexpected values.

printf("P1=%d, CRC_1=%d\n", P1, CRC_1);
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • `printf()` won't help him much on an embedded platform. – Tim Čas Jun 24 '16 at 14:32
  • @TimČas Well, step through with a debugger then. If that's what he's doing with *Watch*, then he's most likely either misreading the screen (0xB6 is really 0x86, for example), or he's not running the program he think he is. – Kusalananda Jun 24 '16 at 14:35
0

The data type of variables are unknown. What datatype of P1, P2, EPC96_1, CRC_1, CRC_2? Where the variable are defined? What function CR_CCCITT() is do?

Beka
  • 97
  • 5