View previous topic :: View next topic |
Author |
Message |
richi-d
Joined: 28 Aug 2007 Posts: 106
|
if statement is not working! |
Posted: Thu May 03, 2012 11:50 am |
|
|
GRRRR- new update- complete software is not more running!! Can anyone tell me why this is not working??
Code: | if ( (UART_BUFFER_RX2[0] == 0x80) && (UART_BUFFER_RX2[1] == 0x8E) ) |
UART_BUFFER_RX2[0] = 0x80 and UART_BUFFER_RX2[1] = 0x8E in the Watch window! |
|
![](templates/subSilver/images/spacer.gif) |
richi-d
Joined: 28 Aug 2007 Posts: 106
|
|
Posted: Thu May 03, 2012 12:04 pm |
|
|
This does not work:
Code: |
UART_BUFFER_RX2[0] = 0x80;
UART_BUFFER_RX2[1] = 0x8E;
if ( (UART_BUFFER_RX2[0] == 0x80) && (UART_BUFFER_RX2[1] == 0x8E) )
{ |
It doesn´t jump into the if.... |
|
![](templates/subSilver/images/spacer.gif) |
Battery David
Joined: 01 Feb 2010 Posts: 25
|
|
Posted: Thu May 03, 2012 12:15 pm |
|
|
Where are the defn's for UART_BUFFER_RX2[x]? |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1328
|
|
Posted: Thu May 03, 2012 12:37 pm |
|
|
If that array is of type char or int8, make sure it is "unsigned" or that comparison will fail sometimes. I posted a thread about that last year and alerted CCS about it.
EDIT:
I checked in 4.131 and see that it isn't changed.
What happens is that for some reason the compiler upgrades the array from type int8 to int16 and sign extends the value. Then it does the comparison, which ends up being 0xFF80 == 0x0080, which will fail every time.
This is a thread I posted on it last year if you want a code example:
http://www.ccsinfo.com/forum/viewtopic.php?t=45702 |
|
![](templates/subSilver/images/spacer.gif) |
richi-d
Joined: 28 Aug 2007 Posts: 106
|
|
Posted: Thu May 03, 2012 12:50 pm |
|
|
jeremiah,
you are great- I will test this tomorrow morning- I found earlier in another program out that when I give a 8bit value to a 16 bit value that it is not the same value in the end- I made &00FF to all this instruction... now I know why!
My older version 4.105 doesnt has this problem... |
|
![](templates/subSilver/images/spacer.gif) |
richi-d
Joined: 28 Aug 2007 Posts: 106
|
|
Posted: Fri May 04, 2012 1:47 am |
|
|
It works- thank you very much |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri May 04, 2012 1:53 am |
|
|
All C constants are unsigned. Even -255 is actually (-)(+255), the minus being an operator applied to the positive constant.
To do a signed comparison on constants with the bit 7 set, they have to be promoted to signed 16 bit. 0x8E should become 0x008E and not be sign extended. That's because 0x80 is a constant and therefore MUST be unsigned. Promoting it to 0xFF8E by sign extension is treating the constant as signed, which it isn't.
To ensure the comparison is done in what I presume is your expected unsigned in8 form you msut ensure UART_BUFFER_RX2 is declared as int, int8 or unsigned int8 (all are the same in CCS C), or you explicitly cast it in the comparison: ((unsigned in8)UART_BUFFER_RX2[2] == 0x8E).
CCS is fairly well known for doing some unexpected type promotions. At least its something I'm always onthe look out for. Also the debugger - the watch window - often shows variables in the wrong data type, defaulting to unsigned int8 in many cases.
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri May 04, 2012 12:47 pm |
|
|
having puzzled myself with carelessness early on with CCS
i avoid a lot of trouble when i program now
by always specifying
SIGNED or UNSIGNED explicitly to all my integer defines now
except for int1, that is ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|