hey guys...
i wanted to know how to make multiple conditions such that each condition has arange of values...i have written the following code but doesnt work
help me quick plz.
It is aPIC program for monitoring the ADC value and sending to LCD

if(ADRESL==0x00)
						{lcddata('O');
						DELAY(2);
						lcddata('F');
						DELAY(2);
						lcddata('F');
						DELAY(2);
						}					
						else if(0x00<ADRESL<0x70)
						{
						lcddata('S');
						DELAY(2);
						}											
							else
							{
							lcddata('M');
							DELAY(2);
							}

Dani AI

Generated

Short summary and practical fix (for )

The expression you used like 0x00 < ADRESL < 0x70 does not test a range in C/C++; the language evaluates comparisons left-to-right and the first comparison yields 0/1 which is then compared to the next value. Use a logical-AND style test (and make sure the lower bound is actually smaller than the upper bound). This is what was getting at, and ’s point about signedness is also important because small integer types can be promoted unexpectedly. (en.cppreference.com)

Use the full ADC result and unsigned types

Don’t base range checks on a single SFR byte if you need the full conversion. Configure the ADC result justification explicitly, read the conversion, combine the two result registers into a 10-bit unsigned value, then test ranges on that value. The PIC stores the 10-bit result across ADRESH/ADRESL and the justification bit controls where the bits sit. (static.datasheets.com)

Example pattern (original, compact)

/* ensure right-justified result (set ADFM appropriately for your device) */
ADCON2bits.ADFM = 1;   /* or set ADCON2 = ... per your compiler/header */

/* start conversion and wait */
ADCON0bits.GO = 1;
while (ADCON0bits.GO);

/* combine registers into a 10-bit unsigned value */
unsigned int adc = ((unsigned int)ADRESH << 8) | ADRESL;
adc &= 0x03FF; /* mask to 10 bits, defensive */

/* ordered else-if ranges (low..high) */
if (adc == 0) { lcddata('O'); }
else if (adc < 0x0070) { lcddata('S'); }
else if (adc < 0x00F5) { lcddata('M'); }
else { lcddata('H'); }

Quick troubleshooting checklist

  • Explicitly set ADFM and ADC timing (ADCON2) so you know where bits land. (static.datasheets.com)
  • Use unsigned types (uint16_t or unsigned int) to avoid signed-extension surprises. (cppreference.patrickfasano.com)
  • Temporarily output ADRESH and ADRESL (or send them over UART) right after conversion to confirm values in the simulator/hardware.
  • Make sure your inequalities have the correct lower/upper order and don’t overlap (test with known inputs: 0V and Vdd).

Following the above (as suggested, just add more else-if blocks) will make the ranges reliable and easier to debug.

Recommended Answers

All 16 Replies

Try to change code as:

if(ADRESL==0x00)
{lcddata('O');
DELAY(2);
lcddata('F');
DELAY(2);
lcddata('F');
DELAY(2);
}	
else if(0x00<ADRESL && ADRESL<0x70)
{
lcddata('S');
DELAY(2);
}	
else
{
lcddata('M');
DELAY(2);
}

And more over, concentrate on datatype of "ADRESL".

thanx Yellaiah.....also want to know how to specify three or four else if statements just like :

else if(0x00<ADRESL && ADRESL<0x70)
{
lcddata('S');
DELAY(2);
}

so that i can make code for three different ranges of values coming from ADC...

Just create more else if's.

Just create more else if's.

does that work....i'm having problems in the simulation...the first else if doesnt work in the correct range that Yellaiah told me....

ADRESL is of which type?

it is 8-bit binary output of a PIC's internal ADC...i am converting the particular binary codes to HEX and then using them in the code

ADRESL is of which type?

it is 8-bit binary output of a PIC's internal ADC...i am converting the particular binary codes to HEX and then using them in the code

does that work....i'm having problems in the simulation...the first else if doesnt work in the correct range that Yellaiah told me....

Then you did it wrong. Without seeing the code, we can't offer suggestions.

Then you did it wrong. Without seeing the code, we can't offer suggestions.

#include<p18f458.h>
#include<delays.h>
#define ldata PORTD
#define rs PORTEbits.RE0
#define rw PORTEbits.RE1
#define en PORTEbits.RE2
void DELAY(unsigned int);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void main(void)
{

		TRISB=0;
		TRISD=0;
		TRISE=0;
		TRISAbits.TRISA0=1;
		ADCON0=0x81;
		ADCON1=0xCE;
		TXSTA=0x20;
		SPBRG=15;
		TXSTAbits.TXEN=1;
		RCSTAbits.SPEN=1;
		en=0;
		DELAY(250);
		lcdcmd(0x38);
		DELAY(250);
		lcdcmd(0x0e);
		DELAY(250);
		lcdcmd(0x01);
		DELAY(250);
		lcdcmd(0x06);
		DELAY(250);
		lcdcmd(0x80);
		DELAY(250);
{
DELAY(2);
ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);

	{
		PORTB=ADRESL;
		DELAY(2);
		{
			if(ADRESL==0x00)
			{lcddata('O');
			DELAY(2);
			lcddata('F');
			DELAY(2);
			lcddata('F');
			DELAY(2);
			}	
			else if(0xF5<ADRESL && ADRESL<0x70)
			{
			lcddata('S');
			DELAY(2);
			}	
			else
			{
			lcddata('M');
			DELAY(2);
			}			
	}					
	}		
}

	
}


void DELAY(unsigned int itime)
{
unsigned int i;unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
void lcdcmd(unsigned char value)
{
		ldata=value;
		rs=0;
		rw=0;
		en=1;
		DELAY(1);
		en=0;

}

void lcddata(unsigned char value)
	{
	ldata=value;
	rs=1;
	rw=0;
	en=1;
	DELAY(1);
	en=0;
	
	}
commented: Still not getting the code tags message! -4

Then you did it wrong. Without seeing the code, we can't offer suggestions.

I have uploaded the full code...i's not too good in c++ becz i originally used to program in Assembly but now our tutor has made it compulsory to write code in c++....plz help me out guyz...

I don't see any attempt at more than one else-if . That's probably why more of them didn't work.

Learn to format your code properly. It's hard to read as it is.

I don't see any attempt at more than one else-if . That's probably why more of them didn't work.

Learn to format your code properly. It's hard to read as it is.

i uploaded the simple one bcz the single if-else statement in this code also isnt working within it's original range....

What's the value of ADRESL before the IF statement? Is it a byte or an int? signed or unsigned? 0xF5 is a negative value if a byte.

What's the value of ADRESL before the IF statement? Is it a byte or an int? signed or unsigned? 0xF5 is a negative value if a byte.

I'll tell u a bit of detail about ADRESL...
The ADC in the PIC gets an analog value and converts it into a 10 bit digital output. 8 bits are loaded in the ADRESL(analog to digital result low) while the upper two bits are loaded in the ADRESH(analog to digital result high). Both of these are 8 bit Registers inside the PIC's RAM.
I only want to use the ADRESL reg's output to interpret what amount of analogue input came in and then display it on the LCD. Now because i'm only using the ADRESL the value might come negative. I have also made a program using the CASE Switch statement and it worked nicely but the problem was that i could only specify a single input value. I want to specify a range of values to check if they came in and then display a single ASCII character on the LCD. You mentioned int. Can the ADRESL be specified as an int and then used in the if statement. Does it make any sense now. Please tell me my method isn't wrong. Thanks

I'll tell u a bit of detail about ADRESL...

Doesn't answer one of my questions. I suspect you will find you think you are using positive values but they are signed and negative.

Doesn't answer one of my questions. I suspect you will find you think you are using positive values but they are signed and negative.

yes i think it is signed because the analogue values also go in the negative range for example like minus temperature and the value i'm using might also be in the negative range but how does that affect my code...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.