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.

switch on led when having 2 adcs not having the same value

Status
Not open for further replies.

burassu

Junior Member level 1
Joined
Jul 2, 2011
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,391
I am using an 8051 ucontroller and I am programing it with keil in c , and I need to switch on an led while the value of an adc1 is not equal to the value of adc2.



Can anyone help me by giving me an example ,since I am new to this system I am not that familiar to it.

Tnks !!
 

hey your info is not full. Which type of ADC are you using.If you are using 0804 ,then store 8bit data from port to some variable(x) and another value of adc2 in another variable(y). Then compare them
if(x==y)
led=0;
else
led=1;
 

I am using the adc of the ucontroller of port 1.
 

there is no inbuilt ADC in 8051
you have to interface externally
 

there is no inbuilt ADC in 8051
you have to interface externally

There are several 8051 variants available which have ADC as well as other peripheral modules.

Silicon Labs - Mixed-Signal, 8-bit Microcontrollers (MCUs)

Are one of my favorite manufactures of 8051 variants, their devices can achieve 100 MIPS.

@burassu

Have you written any code that you can post or upload?

You don't want to do a comparison against a single value, instead you want to do a comparison across a small range, much like a hysteresis curve.

Otherwise, small amounts of noise can constantly flicker the LED.

If you can post or upload the code you have along with the schematic, I can assist you.

BigDog
 

Ok guys i thought you are using AT89c51... Anyways post your code i will assist you or bigdogguru..
 

Code:
//************************ Functions and variables declerations.******************

#include <reg51.h>			   				//Pre-processor commands.
#include <absacc.h>

unsigned char mess[] = "Temp in :";	
unsigned char mess2[] ="Temp select :";
unsigned char x;							//Create character x.
unsigned char ADC();					//Create  ADC.
unsigned char ADC1();                    // Create ADC1 
unsigned char temp;						//Create character value.
unsigned char temp1;


#define PPIA 0x4000 					//Port A.   
#define PPIB 0x4001 					//Port B.
#define PPIC 0x4002 					//Port C not being used.
#define PPICNTRL 0x4003 				//8255 Control Register.

sfr 	ADCLK=0xF2;							//Clock control register.
sfr 	ADCON=0xF3;							//Control register.
sfr 	ADDL=0xF4;							//Data low byte.
sfr		ADDH=0xF5;							//Data high byte.
sfr		ADCF=0xF6;							//Configuration register, defines which ADC.
sbit LED = P2^0	;
void TLCD(unsigned char value);
void LCD_String(unsigned char*string1);	//create function for lcd string.
void LCD_init();						//create function for lcd initialization.
void LCD_data();						//create function for lcd data.	
void Delay(unsigned int mstime); 			//Prototype Functions 	
void MSDelay(unsigned int mstime); 		//Prototype Functions used in between Characters.

//************************ LCD *********************************************************

void LCD_init()							//LCD initialization function.
{										//
XBYTE[PPIA] = 0x38;						//Initialize LCD 16x2.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
										//
XBYTE[PPIA] = 0x0E;						//DIsplay on cursor off.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Small small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
 										//
XBYTE[PPIA] = 0x01;						//Clear Display.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Small delay.
										//
XBYTE[PPIA] = 0x80;						//Line 1.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
}



void LCD_init2()							//LCD initialization function.
{										//

MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
										//
XBYTE[PPIA] = 0x0E;						//DIsplay on cursor off.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Small small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
 										//

MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Small delay.
										//
XBYTE[PPIA] = 0xc0;						//Line 2.
MSDelay(100);
XBYTE[PPIB] = 0x04;						//EN = 1 , RS = 0 , RW = 0.
MSDelay(100);							//Start small delay.
XBYTE[PPIB] = 0x00;						//EN = 0 , RS = 0 , RW = 0.
MSDelay(100);							//Start delay.
}


void LCD_data(unsigned char value)		//LCD data function.
{										//
	XBYTE[PPIA] = value;				//Send value to LCD.
	XBYTE[PPIB] = 0x05;					//EN = 1 , RS = 1 , RW = 0.
	MSDelay(150);						//Start small delay.
	XBYTE[PPIB] = 0x01;					//EN = 0 , RS = 1 , RW = 0.
	MSDelay(150);						//Start delay.
}

void LCD_String(unsigned char*string1)	//String function.
{										//
	while(*string1)						//Loop here until the string is all send to the 
	{									//data of the LCD. This will display the meaasge
		LCD_data (*string1);			//that is called with the string.
		MSDelay(250);
		string1++;						//Increment string.
	}									//
}										//

void MSDelay(unsigned int mstime)		//milliseconds delay.
{										//
unsigned int i,j;						//nested loops.
for(i=0;i<mstime;i++) 					//outer loop.
for(j=0;j<100;j++); 					//inner loop.
}										//

//************************ ADC Actual temp********************************************

unsigned char ADC()				   			//Analog to Digital Conversion Function.
{											//
	ADCON=ADCON & 0xF8;						//Select CH ADC.
	ADCON = ADCON|0x08;						//Mask.	
	while((ADCON&0x10)!=0x10);				//Wait ADEOC=1 bit4.	
	ADCON=ADCON & 0xEF;						//Clear ADEOC flag=0.	
	return(ADDH);							//Return parameter/value to main.
}											//
void TLCD(unsigned char value)				//Alg function.
{											//
	unsigned char x;						//character x.
	unsigned char dec1,dec2,dec3;			//character for decimal.
	unsigned char units, tens, hundreds; 	//character for tens and units and hundreds.
											//
	x=value/10;								//divide value by 10.
	dec1=value%10;							//place value for decimal 1.
	dec2=x%10;								//
	dec3=x/10;								//
	units=dec1|0x30;						//OR units.
	tens=dec2|0x30;							//OR tens.
	hundreds=dec3|0x30;						//OR hundreds.
	LCD_data(hundreds);						//Send data to LCD.
	Delay(100);								//Small delay.
	LCD_data(tens);							//Send data to LCD.
	Delay(100);								//Small delay.
	LCD_data(units);						//Send data to LCD.
	Delay(100);								//Small delay.
}											//

void Delay(unsigned int mstime)		 		//milliseconds delay.
{											//
unsigned int i,j;							//nested loops.
for(i=0;i<mstime;i++) 						//outer loop.
for(j=0;j<100;j++); 						//inner loop.
}											//

//********************** MAIN PROG **************************************
					
void main(void)								//Main Function Block.
{
XBYTE[PPICNTRL]=0x80;						//Control word ALL Ports o/p.
LCD_init();									//Initialize LCD.
LCD_String(mess);							//Send string message.
LCD_init2();									//Initialize LCD.
LCD_String(mess2);


while(1)									//Loop here.
	{										//
		ADCF=0x01;				    		//P1.0 
		

		ADCON=0x20;							//Enable  ADC
		ADCLK=0x01;							//Time=1.33us.
		MSDelay(150);
		

		XBYTE[PPIA] = 0xFE,0x32,0x00,0x0B;				//Line 2.
		MSDelay(100);
		XBYTE[PPIB] = 0x04;					//EN = 1 , RS = 0 , RW = 0.
		MSDelay(100);						//delay.
		XBYTE[PPIB] = 0x00;					//EN = 0 , RS = 0 , RW = 0.
		MSDelay(100);						//delay.
			temp=ADC();					//Get Value from ADC.	  
			TLCD(temp);					//Convert value to be writen on LCD.


            ADCF=0x02;							  //P1.1
			
		ADCON=0x20;							//Enable  ADC
		ADCLK=0x01;							//Time=1.33us.
		MSDelay(150);
		

		XBYTE[PPIA] = 0xFE,0x32,0x01,0x0B;					//Line 2.
		MSDelay(100);
		XBYTE[PPIB] = 0x04;					//EN = 1 , RS = 0 , RW = 0.
		MSDelay(100);						//delay.
		XBYTE[PPIB] = 0x00;					//EN = 0 , RS = 0 , RW = 0.
		MSDelay(100);						//delay.
			temp1=ADC1();					//Get Value from ADC.
			
			TLCD(temp1);					//Convert value to be writen on LCD					//
	
	
	if ((ADC1 == ADC)) 
	{
    LED= 1; // switch on p2.1
   							 
    
	}
    
	else if((ADC1) != (ADC))
	 {
	  LED= 0; // switch on p2.1
	 }
	}
}

//************************ ADC Actual temp********************************************

unsigned char ADC1()				   			//Analog to Digital Conversion Function.
{											//
	ADCON=ADCON & 0xF8;						//Select CH ADC.
	ADCON = ADCON|0x08;						//Mask.	
	while((ADCON&0x10)!=0x10);				//Wait ADEOC=1 bit4.	
	ADCON=ADCON & 0xEF;						//Clear ADEOC flag=0.	
	return(ADDH);							//Return parameter/value to main.
}											//
void TLCD1(unsigned char value)				//Alg function.
{											//
	unsigned char x;						//character x.
	unsigned char dec1,dec2,dec3;			//character for decimal.
	unsigned char units, tens, hundreds; 	//character for tens and units and hundreds.
											//
	x=value/10;								//divide value by 10.
	dec1=value%10;							//place value for decimal 1.
	dec2=x%10;								//
	dec3=x/10;								//
	units=dec1|0x30;						//OR units.
	tens=dec2|0x30;							//OR tens.
	hundreds=dec3|0x30;						//OR hundreds.
	LCD_data(hundreds);						//Send data to LCD.
	Delay(100);								//Small delay.
	LCD_data(tens);							//Send data to LCD.
	Delay(100);								//Small delay.
	LCD_data(units);						//Send data to LCD.
	Delay(100);								//Small delay.
}											//

void Delay1(unsigned int mstime)		 		//milliseconds delay.
{											//
unsigned int i,j;							//nested loops.
for(i=0;i<mstime;i++) 						//outer loop.
for(j=0;j<100;j++); 						//inner loop.
}
 
Last edited by a moderator:

What is the crystal frequency of your design?

Are you using two pots as the analog sources?


BigDog
 

Yes , but actualy they will be replaced with lm35 . and the crystal is that 0f 18.432HZ
 

Just glancing at your code, the following snippet has some issues:

Code:
if ((ADC1 == ADC)) 
{
    LED= 1; // switch on p2.1
   							 
    
}
else if((ADC1) != (ADC))
{
	  LED= 0; // switch on p2.1

}

Try this instead:

Code:
if (ADC1() == ADC()) 
{
    LED= 1; // switch on p2.1
   							    
}
else 
{

    LED= 0; // switch on p2.1

}

You forgot the parenthesis of the function calls ADC1() and ADC(), and there is no point in retesting the equivalency of the ADC channels if the are not equal in the first section of the IF statement.

BigDog
 

I would also suggest to take into account the tolerance of the ADC since the converters and even the sensors are not 100% accurate.
For example you can perceive the result as equal if the difference is less than 5 or something like that.

Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top