[SOLVED] problem in displaying input value from ADC to LCD

Status
Not open for further replies.

pravin b

Member level 5
Joined
May 20, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Mumbai, India
Activity points
2,083
hello friends,

I have programmed my 8051 in C to get digital values from ADC0804 (ranges from 0 to 255) connected at port1 and display the number on LCD connected to port 2. however LCD shows only number "000" all the time, while changes in digital values can be seen at the output of ADC and 8051 port1. Even my HEX2ASCII conversion routine works fine (tested it by sending any 3 digit number).
LCD works fine as I am able to see the char 'm', which i used for testing the LCD.
clock frequency=11.0592MHz

DOES I M GOING WRONG IN MY C CODE THEN???????

I m hereby providing my code and hardware. Please help me to find the problem area.
Thanks & regards;
Pravin B

Code:
#include<REG51.H>

sfr lcd_port = 0xA0;					//LCD connected to p2
sfr ADC_port = 0x90;					//ADC connected to p1
sbit rs=P3^0;							//rs connected to p3.0
sbit rw=P3^6;							//rw connected to p3.6
sbit en=P3^2;						    //en connected to p3.2
sbit rd=P3^3;							//rd connected to p3.2
sbit wr=P3^4;							//wr connected to p3.4
sbit intr=P3^5;							//intr connected to p3.5
sbit busy=P2^7;							//busy flag= p2.7

unsigned int ADC_value, buf;
							

void delay(unsigned int count)			//delay subroutine
{
unsigned int i,j;
for(i=0;i<=count;i++)
	for(j=0;j<=1275;j++);
}

void ready()							//subroutine to check if lcd is busy
{
busy=1;									//make busy pin input
rs=0;									//rs=low
rw=1;									//rw=high
while(busy==1)							//check if busy flag 
	{
	en=0;
	delay(2);
	en=1;
	}
}

void lcd_cmd(unsigned int value)		//sub to write commands 
{
ready();
lcd_port=value;
rs=0;
rw=0;
en=1;
delay(2);
en=0;
}

void lcd_data(unsigned int value)		//sub to write data
{
ready();
lcd_port=value;
rs=1;
rw=0;
en=1;
delay(2);
en=0;
}
	
void lcd_init()							 //lcd initializtion
{
rs=0;									 //rs=output
rw=0;									 //rw=output
en=0;									 //en=output
lcd_port=0x00;							 //lcd_port=port2=output

lcd_cmd(0x38);
lcd_cmd(0x0E);
lcd_cmd(0x01);													 
lcd_cmd(0x06);										  
lcd_cmd(0x80);										   
}

void ADC_init()							 //ADC initialization
{
intr=1;
rd=0;
wr=0;
ADC_port=0xFF;							 //ADC_port=port1=input
wr=0;									 //start of conversion
delay(2);
wr=1;
while(intr==0)		                                        //check for intr flag
	{
	rd=0;
	ADC_value=ADC_port;				   //read digital o/p;A2D value stored in variable ADC_value
	rd=1;
	}
}

void hex2ascii()						//conversion to ASCII for display on LCD
{

unsigned int one, ten, hundred;

buf=ADC_value;

one=buf%10;
ten=buf/10;
ten=ten%10;
hundred=buf/100;

one=one|0x30;
ten=ten|0x30;
hundred=hundred|0x30;  	

lcd_data('m');							//char 'm' sent for testing of LCD
lcd_data(one);							//one's bit sent to LCD
lcd_data(ten);							//ten's bit sent to LCD
lcd_data(hundred);						//hundred's bit sent to LCD

}				
												 
void main()
{
while(1)
	{
	lcd_init();							 //LCD initialization
	ADC_init();							 //ADC initialization
	hex2ascii();						         //HEX 2 ASCII conversion of digital output
	}								 					
}

 

Hey however I have able to rectify the problem by modifying my while loop in ADC_init() to do-while loop (ADC conversion);
my be my variable ADC_value was taking some garbage value.....correct me if I am wrong!!!!
Code:
do{
	rd=0;
	ADC_value=ADC_port;		 //read digital o/p;A2D value stored in variable ADC_value
	rd=1;
	}while(intr==0);			 //check for intr flag
}
 

Use mikroC PRO AVR 5.60 Compiler from mikroElektronika. In that you can use ready to use ADC_Value = ADC_Read(ch); and LCD commands. I mean to say there are ready to use libraries for ADC, LCD, and many more.
 

Thanks for the suggestion Jayanth, actually i used to make my own library files for common functions like ADC and LCD to use them in my KEIL.
And about MikroC till now I have not used it so would love to get hands on It too....
 

In this program declare the LCD_Init() fun call, above the main loop. like..and also check whether the ADC ic is working or not, by connecting it in to the serial window...

Code:
void main()
{
   lcd_init();							 //LCD initialization
   while(1)
   {
	ADC_init();							 //ADC initialization
	hex2ascii();						         //HEX 2 ASCII conversion of digital output
   }								 					
}
 
Last edited:
hey avinash however i resolved the problem by now....and have also shared the changes in original program. Writing LCD_init() outof while(1) loop didn't changed my output; since it is only to initialize the LCD.

i think i should mark this thread solved!!!!!!
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…