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
Author Message
vtrx

Joined: 11 Oct 2017
Posts: 69

Not Equal
Posted: Sat Jan 30, 2021 6:33 pm

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: 851
Location: Utah

Posted: Sat Jan 30, 2021 6:56 pm

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: 21362

Posted: Sat Jan 30, 2021 8:21 pm

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: 16935

 Posted: Sun Jan 31, 2021 2:50 am 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.... 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: 69

 Posted: Sun Jan 31, 2021 1:47 pm 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1

 Jump to: Select a forum Software----------------General CCS C DiscussionCode LibraryEZ App LynxBest Of Hardware----------------CCS ICD / Mach X / Load-n-Go
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