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 CCS Technical Support

Not Equal

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



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

Not Equal
PostPosted: Sat Jan 30, 2021 6:33 pm     Reply with quote

I have a variable (int8) that is loaded with an Eeprom value that cannot be greater than 3.
I tried to use the following expression, but it is not doing the comparison as expected.

Code:
void main(void)

 ...
   TIPO=read_eeprom(0);
   if(TIPO !=0 || TIPO !=1 || TIPO !=2 || TIPO !=3)
    {
     TIPO=3;
     write_eeprom(0,3);   
    }

 ...


the TIPO variable cannot be less than zero or greater than 3.
Using the expression above does not work, it is always executed even if the variable is within 0 or 3.
it just worked with the following expression.
Code:
void main(void)

 ...
   TIPO=read_eeprom(0);
   if(TIPO >3)
    {
     TIPO=3;
     write_eeprom(0,3);   
    }

 ...

Why is the first expression not functional?
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Sat Jan 30, 2021 6:56 pm     Reply with quote

I believe your code is working properly. If TIP0 is equal to '2' your if() statement compares if TIP0 is not equal to '0'. Well, it's not equal to '0' because it's '2'. Therefore, it enters the if() and executes the code inside it.

I'm no expert but what if your code is like this...

Code:
if(TIP0 >= 0 && TIP0 < 4)


Try that.

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 30, 2021 8:21 pm     Reply with quote

You want it to set TIPO to 3 if it's value is 4 or greater.
Any value in the range of 0 to 3 is OK. So you should have used
the '&&' operator as shown below. Then it works.
Code:

#include <18F46K22.h>
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)

//======================================
void main(void)
{
int8 TIPO = 4;

if(TIPO !=0 && TIPO !=1 && TIPO !=2 && TIPO !=3)
    {
     TIPO=3;
     printf("Set TIPO = 3 \r");
    }
else
     printf("Did not set TIPO = 3 \r");
 


while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sun Jan 31, 2021 2:50 am     Reply with quote

Think about it. You are asking for the code to execute if
TIPO!=0 _or_ TIPO!=1 _or_ TIPO!=2 _or_ TIPO!=3
This is true for every number.

If it is 0, the last three terms all return 'TRUE' so it'll execute.
If it is 1, the first and last two terms will all return TRUE, so again it'll
execute.
for 2, and 3, again three terms will be true, so it'll execute.
Then when the number gets to 4, all the terms will be true, so again it'll
execute.
Same applies for every number.... Sad

Your 'not' is in the wrong place.

if( !(TIPO ==0|| TIPO ==1 || TIPO ==2 || TIPO ==3))

Will execute if TIPO is not 0, 1, 2, or 3.

or as PCM shows, you can use and.
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Sun Jan 31, 2021 1:47 pm     Reply with quote

Thank you all.
I don't know why I got confused with the result of the comparison.
The variable cannot have values other than 0 and 3 or the program hangs.
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