In this post, I am going to show you How to interface LM35 with 8051 I also post C code for it. you can use Keil uvision4 to build the code and program the MCU.
The Pin configuration is shown above we have to take data from pin no. 2
C code for Temperature sensor.
#define DATA P0
#define ADC P2
sbit RS = P3^0;
sbit E = P3^1;
sbit ALE=P1^2;
sbit SOC=P1^1;
sbit OE=P1^0;
sbit EOC=P1^3;
sbit SET0=P1^5;
sbit SET1=P1^6;
sbit SET2=P1^7;
sbit CLOCK=P1^4;
void delay(int x)
{
int i;
for(i=0;i<x;i++);
}
void cmd_lcd (char dat) // function to write command at lcd port
{
DATA=dat;
RS=0; //clear RS (ie. RS=0) to write command
E=1; // send H-L pulse at E pin
delay(500);
E=0;
delay(500);
}
void data_lcd (char dat) // function to write data at lcd port
{
DATA=dat;
RS=1; // set RS=1 to write DATA
E=1; // send H-L pulse at E pin
delay(500);
E=0;
delay(500);
}
void init_lcd() // function to initialize the LCD at power on time
{
cmd_lcd (0x38); // 2x16 display select
cmd_lcd (0x0c); // display on cursor off command
cmd_lcd (0x06); // automatic cursor movement to right
cmd_lcd (0x01); // lcd clear command
cmd_lcd (0x80); // first row first coloumn select command
}
void string_lcd(char *str) // function to display string to lcd
{
while(*str!='\0') // '\0' is null char as last char of pointer is null
{
data_lcd(*str);
str++;
}
}
void number_lcd(int num) // function to display 3 digit decimal value to lcd
{
data_lcd( (num / 100) + 0x30);
data_lcd( (num / 10)%10 + 0x30);
data_lcd( (num % 10) + 0x30);
}
void clock(void)
{
int a,b;
for(b=0;b<=1000;b++)
{
for(a=0;a<30;a++);
CLOCK=~CLOCK;
}
}
unsigned char adc(char s1, char s2, char s3)
{
unsigned char dat;
SET0=s1; SET1=s2; SET2=s3;
ALE=1; SOC=1; clock();
ALE=0; SOC=0; clock();
while(EOC==0);
OE = 1;
dat = ADC;
return(dat);
}
void main(void)
{
unsigned char x;
P0=0xff;
SOC=EOC=1;
OE=0;
init_lcd();
string_lcd("temperature");
while(1)
{
cmd_lcd(0xc0);
x=adc(1,1,1); //channel selection "000" for x co-ordinates
number_lcd(x);
cmd_lcd(0xc3);
string_lcd("^c");
}
}
you have to connect ADC and LCD as pin definition and also the LM35 with ADC as per code.