Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] IF statement does not recognize second condition

Status
Not open for further replies.

kate.gomez

Newbie level 4
Joined
Sep 19, 2011
Messages
5
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,354
Hi,

Good afternoon! I am currently working on a project using RTC. I had to evaluate the current time i.e. hour-minute basis, before executing a code. The EEPROM_Read(0x22) is set to store the current hour, and the EEPROM_Read(0x21) is set to store the current minute. However, the program seems to look at the first condition only, and does not recognize the presence of '&&' to evaluate whether both conditions were satisfied. For example, current time is 10:15, it will display the correct PORTB display, but when current time is 10:45, it will still follow the previous display. With this, I have assumed that the second condition is not really seen by the program.

Here is the part of the code:

if ((EEPROM_Read(0x22)==10) && (0<=EEPROM_Read(0x21)<=29))
PORTB = 0x01;
else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x03;
else if((EEPROM_Read(0x22)==11) && (0<=EEPROM_Read(0x21)<=29))
PORTB = 0x07;
else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x0F;
 

I haven't seen this way to check for two conditions at the same time and I'm not sure if it will work correctly so just brake it in two parts

0<=EEPROM_Read(0x21)<=29
to (0<=EEPROM_Read(0x21)) & (EEPROM_Read(0x21)<=29)

in addition you are using && so anything that is not 0 in the following expression will be considered true so I'm not sure but I think that this (30<=EEPROM_Read(0x21)<=59) (as an example) will always be true

Alex
 
The second condition will not work as you intended. If you want to test two conditions, using your first line as as an example, splt it into two comparisons:
"0 <= EEPROM_Read(0x21)" and "EEPROM_Read(0x21) <= 29)".

The reason it doesn't work is the first comparison evaluates as true or false (1 or 0) which is then checked to see if it is <= 29. Obviously that will always be true, regardless of the EEPROM value.

Incidentally, you would have to use EEPROM_Read(0x22) four times and EEPROM_Read(0x21) eight times, Your code will probably work faster if you read these values into a variable at the start of the code and use that instead of calling the EPROM_Read routine so many times.

Brian
 
The reason it doesn't work is the first comparison evaluates as true or false (1 or 0) which is then checked to see if it is <= 29. Obviously that will always be true, regardless of the EEPROM value.

are you referring to (0<=EEPROM_Read(0x21)<=29))?

isn't the evaluation being executed from right to left like when we use a=b=3 (which is an assignment and not an evaluation)?
I may be wrong but I just want to make sure.

Alex
 
are you referring to (0<=EEPROM_Read(0x21)<=29))?
isn't the evaluation being executed from right to left like when we use a=b=3 (which is an assignment and not an evaluation)?

The order of evaluation of a conditional expression is always left to right.

Any expression of the form "(0<=EEPROM_Read(0x21)<=29)" is evaluated as if parenthesized "( (0<=EEPROM_Read(0x21)) <=29)", so as both you and betwixt indicated these forms of expressions are always true, unless the final test value is zero or one instead of 29 in this case, at which point the condition is determined by the first conditional subexpression.

Also it should be mentioned the first conditional subexpression "0<=EEPROM_Read(0x21)" in this case, if EEPROM_Read() returns an unsigned type, is always true "1" as well.

BigDog
 
as far i know using (0<=EEPROM_Read(0x21)<=29)) won't work correctly. instead use ((EEPROM_Read(0x21)>=0) && ((EEPROM_Read(0x21)<=29) to get expected output.

next as betwixt said, assign (EEPROM_Read(0x21) and (EEPROM_Read(0x22) to variables at the start of code to speed up your design.
 
thank you very much for all your suggestions!!! I was able to make the program work the way it is intended to. I do appreciate all your help. :) thank you again!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top