[SOLVED] Converting ADC value to single integer.

Status
Not open for further replies.

hashim5003

Junior Member level 2
Joined
Jul 21, 2013
Messages
21
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
116
Hi, I am using ADC0804 and converting binary value to decimal as follows:
Code:
void binary_to_decimal()
{
	value = OUTPUT;		//get value from ADC
	x = value / 10;			//binary to decimal conversion
	a = x / 10;		         	//MSB of digital value
	b = x % 10;			//middle digit
	c = value % 10;			//LSB
}

and display it on LCD like:
Code:
lcddata (a + 0x30);			//display digital value on LCD
lcddata (b + 0x30);
lcddata (c + 0x30);

My question is that how I can convert adc value to single integer?
I want to combine a,b,c into single integer.
 

Hi...

/// adc_val is variable, adc_port is port name

after adc conversion
void read() {
cs = 0; //Make CS low
rd = 0; //Make RD low
adc_val = adc_port; //Read ADC port
rd = 1; //Make RD high
cs = 1; //Make CS high
}

now adc_val have hex decimal mal value, so easily convert hex to decimal
 
Last edited:

I can convert adc value to decimal value, like I said above.
What I want is to combine decimal values a(MSB),b(middle value),c(LSB) to single integer.
In simple words I want to combine three integers into single integer.
 

For if adc value have 3 digits..

For 100th digit a=adc_value/100

For 10th digit b=adc_value%100

for unit digit c=adc_value%10..
 

this is the similar program that I have.
I want to use the converted adc decimal value to use in equation and display result on lcd, and to use that decimal value I have to combine it in one integer.
 

Combine the 3 variables a,b,c

{

int num[3]={a, b, c}, n1, n2,dec_adc;

n1 = num[0] * 100;
n2 = num[1] * 10;

dec_adc = n1 + n2 + num[2];



}
 
Last edited:

Combine the 3 variables a,b,c

{

int num[3]={a, b, c}, n1, n2,dec_adc;

n1 = num[0] * 100;
n2 = num[1] * 10;

dec_adc = n1 + n2 + num[2];



}

ok. that works. but I a getting an error when I send it to lcd.
"Illegal pointer conversion."
what could be the reason?
 

Post code for lcd sent...

error is on the red color line.
Code:
void lcdcmd (unsigned char u)
{
	ldata = u;					//put LCD command values on pins
	RS = 0;							
	RW = 0;
	EN = 1;							//strobe enable pin
	delay();
	EN = 0;
}

void lcddata (unsigned char v)
{
	ldata = v;					//put LCD data values on pins
	RS = 1;
	RW = 0;
	EN = 1;							//strobe enable pin
	delay();
	EN = 0;
}

void lcdinit()    		//Initialize LCD
{
	lcdcmd(0x38);   		//Function set
	delay();
	lcdcmd(0x0C);       //Display on, cursor off
	delay();
	lcdcmd(0x01);				//Claer LCD
	delay();
}

void lcdstring (char *sendstring)		//send string to LCD
{
	while (*sendstring)
			lcddata(*sendstring++);
}


void main()
{	
	lcdinit();										//Call LCD initialize	function
	
	lcdstring("Please wait..");
		
	lcdcmd(0x01);									//Clear LCD
	
	conversion ();								//convert analog to digital
	
	R = 0;

	lcdcmd (0x80);
	
	value = OUTPUT;								//get value from ADC
	x = value / 10;								//binary to decimal conversion
	a = x / 10;										//MSB of digital value
	b = x % 10;										//middle digit
	c = value % 10;								//LSB
	
	a1 = a * 100;
	b1 = b * 10;

	sum = a1+b1+c;
	
	[COLOR="#FF0000"]lcdstring(sum);[/COLOR]
	lcdstring(" mg/dL");
	
	R = 1;
	
	while(1);
}
 

Code:
void lcdstring (char *sendstring)		//send string to LCD
{
	while (*sendstring)
			lcddata(*sendstring++);
}


Above method is only display for string of characters..

So, you sent to the lcd by string example... lcddata("string");

This is the send data to lcd
Code:
void lcddata(unsigned char value)
{
ldata=value;
rs = 1;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}
 
Last edited:

Code:
#include <reg51.h>

sfr OUTPUT = 0xA0;		//ADC output port define
sbit SR = P3^0;				//300mV 
sbit R = P3^1;				//ADC read 
sbit W = P3^2;				//ADC start conversion 
sbit INTR = P3^3;			//ADC conversion complete indication 

sfr ldata = 0x90;			//LCD port define
sbit RS = P3^7;				//LCD data/command enable 
sbit RW = P3^6;				//LCD data/command write
sbit EN = P3^5;				//LCD enable

unsigned char value, x, i, j, a, b, c, f, a1, b1, c1, sum;

void lcdcmd (unsigned char u)
{
	ldata = u;					//put LCD command values on pins
	RS = 0;							
	RW = 0;
	EN = 1;							//strobe enable pin
	delay();
	EN = 0;
}

void lcddata (unsigned char v)
{
	ldata = v;					//put LCD data values on pins
	RS = 1;
	RW = 0;
	EN = 1;							//strobe enable pin
	delay();
	EN = 0;
}

void lcdinit()    		//Initialize LCD
{
	lcdcmd(0x38);   		//Function set
	delay();
	lcdcmd(0x0C);       //Display on, cursor off
	delay();
	lcdcmd(0x01);				//Claer LCD
	delay();
}

void lcdstring (unsigned char *sendstring)		//send string to LCD
{
	while (*sendstring)
		lcddata(*sendstring++);
	
}


void main()
{	
	lcdinit();										//Call LCD initialize	function
	
	lcdstring("Please wait..");
	
	
	lcdcmd(0x01);									//Clear LCD
	
	conversion ();								//convert analog to digital
	
	R = 0;
	
	lcdcmd (0x80);
	
	value = OUTPUT;								//get value from ADC
	x = value / 10;								//binary to decimal conversion
	a = x / 10;										//MSB of digital value
	b = x % 10;										//middle digit
	c = value % 10;								//LSB
	
	a1 = a * 100;
	b1 = b * 10;

	sum = 124;
	
	lcddata(sum);
	lcdstring(" mg/dL");
	
	R = 1;
		
	while(1);
}

- - - Updated - - -

when i add 0x30 with sum(variable) it displays correct value from 0 to 9, after that it shows weird caracters.
 

Code:
void main()
{	
	lcdinit();										//Call LCD initialize	function
	
	lcdstring("Please wait..");
	
	lcdcmd(0x01);									//Clear LCD
	
while(1)
{
	conversion ();								//convert analog to digital
	
	R = 0;
	
	lcdcmd (0x80);
	
	value = OUTPUT;								//get value from ADC
	x = value / 10;								//binary to decimal conversion
	a = x / 10;										//MSB of digital value
	b = x % 10;										//middle digit
	c = value % 10;								//LSB
	
	a1 = a * 100;
	b1 = b * 10;

	sum = 124;
	
	lcddata(sum);
	lcdstring(" mg/dL");
	
	R = 1;
		
	}
}

try this main code
 


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…