Help: LM35 & ADC0831 interfacing with 8051

Thread Starter

hashim5003

Joined Apr 29, 2013
32
Hello everyone!
I am trying to use LM35 for temprature reading with 8051. I need to know that how to receive serial data from ADC0831 with 8051 and then send it to LCD? I have wrote some code but can't do the receiving part? Should I create an array to save the bytes received from adc or something else?
 

ActivePower

Joined Mar 15, 2012
155
You need a single 8-bit variable (char) to read in the 8-bit value, say result. Create a loop to run 8 times and in each iteration read in a bit, shift the result by one and OR it with the bit you just read. No need to use an array.
 

Thread Starter

hashim5003

Joined Apr 29, 2013
32
You need a single 8-bit variable (char) to read in the 8-bit value, say result. Create a loop to run 8 times and in each iteration read in a bit, shift the result by one and OR it with the bit you just read. No need to use an array.
What's the reason for ORing?
 

ActivePower

Joined Mar 15, 2012
155
I meant bitwise OR, unless you already got that. We need it to keep 'adding' the incoming bits to the variable storing the result.

For example,
Rich (BB code):
int a = 2;         // Binary = 010
int b = a << 1;    // Binary = 100
b = b | 1;        // Binary = 101
 

Thread Starter

hashim5003

Joined Apr 29, 2013
32
@ActivePower
I have wrote the code from what I have understand:
Is it correct?

Rich (BB code):
unsigned char read_data ()
{
	unsigned char i;
	char result = 0;
	char read;
	for ( i = 0; i < 8; i++ )
	{
		read = result << 1;    //shift variable by one bit
		clk = 1;                  //clock pulse
		delay(5);
		clk = 0;
		delay(5);
		result = result | read;
	}
	return read;                   //return data stored in variable
}
 
Last edited:

Thread Starter

hashim5003

Joined Apr 29, 2013
32
You need a single 8-bit variable (char) to read in the 8-bit value, say result. Create a loop to run 8 times and in each iteration read in a bit, shift the result by one and OR it with the bit you just read. No need to use an array.
ActivePower
I have worked on the code. plz check if it is correct or not? thanks

Rich (BB code):
#include<reg51.h>

void delay (int);
void lcd_init (void);
void lcd_cmd (char);
void lcd_data (char);
//char read_data (char);

sbit RS = P1^0;
sbit EN = P1^1;
sbit CS = P1^2;
sbit CK = P1^3;
sbit DO = P1^4;
char value;

void delay ( int a )
{
	int i,j;
	for ( i = 0; i < a; i++ )
		for ( j = 0; j < 1275; j++ );
}

void lcd_init ()
{
	lcd_cmd(0x38);		  
	delay(5);
	lcd_cmd(0x0F);      
	delay(5);
	lcd_cmd(0x0C);
	delay(5);
	lcd_cmd(0x80);
	delay(5);
}

void lcd_data ( char y )
{
	RS = 1;
	P2 = y;
	EN = 1;
	delay(5);
	EN = 0;
	delay(5);
}

void lcd_cmd ( char z )
{
	RS = 0;
	P2 = z;
	EN = 1;
	delay(5);
	EN = 0;
	delay(5);
}

unsigned char read_data()
{
	unsigned char i;
	char result;
	
		CS = 1;
		delay(5);
		CS = 0; 
	
		CK = 1;
		delay(5);
		CK = 0;
		delay(5);
		CK = 1;
		delay(5);
		CK = 0;
		delay(5);
	
		for ( i = 0; i < 8; i++ )
		{
			CK = 1;
			delay(5);
			CK = 0;
			delay(5);
			result = result << 1;
			result = result | DO;
			delay(5);
		}
	return result;
}

void main ()
{
	lcd_init();

	while(1)
	{
		value = read_data;
		lcd_data (value);
	}
}
 

ActivePower

Joined Mar 15, 2012
155
1. You didn't initialize the `result` variable in your final code and it will end up getting an arbitrary value. It's better to initialize it to 0x00.

2. I think you intended to write read_data() in your main().

3. Use comments. While your application is small enough not to require a lot of them, it is advisable to comment your code especially in sections where you got stuck before. You will be surprised to see how less sense the same tricky part makes months from now.

4. Refrain from using magic numbers. The 1275 in your for() loop appears out of nowhere. It is better to have a define'd constant getting that value from a one-line computation. Again, this is more a friendly advice than a rule set in stone.

5. Why do you need the global `value` variable. You're returning the result from your readADC function and writing it to the LCD, why introduce a global in the middle. You could get away with an automatic variable or a call like this:

Rich (BB code):
lcd_data(read_data())
Having a global complicates things - it gets hard to keep track of what's accessing what global really soon as your program grows.

Otherwise, based on a quick skim it looks okay. Good work!
 
Last edited:

Thread Starter

hashim5003

Joined Apr 29, 2013
32
Thanks Mr ActivePower for your help.
I couldn't do that with adc0831.
Although it worked but there were some complications.

SO, I am going to use adc0804 with LM35 now.
I will let you know if I need help with adc0804.
 
Top