CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

if statement is not working!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
richi-d



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

if statement is not working!
PostPosted: Thu May 03, 2012 11:50 am     Reply with quote

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!
richi-d



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

PostPosted: Thu May 03, 2012 12:04 pm     Reply with quote

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....
Battery David



Joined: 01 Feb 2010
Posts: 25

View user's profile Send private message

PostPosted: Thu May 03, 2012 12:15 pm     Reply with quote

Where are the defn's for UART_BUFFER_RX2[x]?
jeremiah



Joined: 20 Jul 2010
Posts: 1321

View user's profile Send private message

PostPosted: Thu May 03, 2012 12:37 pm     Reply with quote

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
richi-d



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

PostPosted: Thu May 03, 2012 12:50 pm     Reply with quote

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...
richi-d



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

PostPosted: Fri May 04, 2012 1:47 am     Reply with quote

It works- thank you very much
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri May 04, 2012 1:53 am     Reply with quote

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
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri May 04, 2012 12:47 pm     Reply with quote

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 Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group