#include<reg52.h>
#include<intrins.h>
typedef unsigned char uint8;
#define uint unsigned int
#define uchar unsigned char
sbit DQ=P3^3;
uchar code Bw[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar code Xsw[16]={0x30,0x31,0x31,0x32,0x33,0x33,0x34,0x35,0x36,0x36,0x37,0x38,0x38,0x39,0x39};
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
sbit BUSY=P0^7;
uchar wendu;
uchar temp_g,temp_d;
unsigned char code word1[]={"Temperature:"};
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;--i)
for(j=110;j>0;j--);
}
void Delayus(int t)
{int s;
for(s=0;s<t;s++);
}
void wait(void)
{
P0=0xFF;
do
{
RS=0;
RW=1;
EN=0;
EN=1;
}
while(BUSY==1);
EN=0;
}
void w_dat(uint8 dat)
{
wait();
EN=0;
P0=dat;
RS=1;
RW=0;
EN=1;
EN=0;
}
void w_cmd(uint8 cmd)
{
wait();
EN=0;
P0=cmd;
RS=0;
RW=0;
EN=1;
EN=0;
}
void w_string(uint8 addr_start,uint8 *p)
{
w_cmd(addr_start);
while(*p!='\0')
{
w_dat(*p++);
}
}
void Init_LCD1602(void)
{
w_cmd(0x38);
w_cmd(0x0c);
w_cmd(0x06);
w_cmd(0x01);
}
uchar Reset()
{
uchar d;
DQ=0;
Delayus(29);
DQ=1;
Delayus(3);
d=DQ;
Delayus(25);
return(d);
}
void write_bit(uchar bitval)
{
DQ=0;
if(bitval==1)
DQ=1;
Delayus(5);
DQ=1;
}
void ds18write_byte(char val)
{
uchar i;
uchar temp;
for(i=0;i<8;i++)
{
temp=val>>i;
temp&=0x01;
write_bit(temp);
}
Delayus(5);
}
uchar read_bit()
{
uchar i;
DQ=0;
DQ=1;
for(i=0;i<3;i++)
return(DQ);
}
uchar ds18read_byte()
{
uchar i;
uchar value=0;
for(i=0;i<8;i++)
{
if(read_bit())
value|=0x01<<i;
Delayus(6);
}
return(value);
}
int Readtemperature()
{
uchar temp_d,temp_g,k,get[2],temp;
Reset();
ds18write_byte(0xcc);
ds18write_byte(0x44);
Delayus(0);
Reset();
ds18write_byte(0xcc);
ds18write_byte(0xbe);
for(k=0;k<2;k++)
{
get[k]=ds18read_byte();
}
temp_d=get[0];
temp_g=get[1];
if((temp_g&0xf0)==0xf0)
{
temp_d=~temp_d;
if(temp_d==0xff)
{
temp_d=temp_d+0x01;
temp_g=~temp_g;
temp_g=temp_g+0x01;
}
else
{
temp_d=temp_d+0x01;
temp_g=~temp_g;
}
w_cmd(0xc5);
w_dat(Xsw[temp_d&0x0f]);
temp=((temp_d&0xf0)>>4)|((temp_g&0x0f)<<4);
w_cmd(0xc1);
w_dat(0x2d);
}
else
{
w_cmd(0xc5);
w_dat(Xsw[temp_d&0x0f]);
temp=((temp_d&0xf0)>>4)|((temp_g&0x0f)<<4);
w_cmd(0xc1);
w_dat(Bw[temp/100]);
}
return temp;
}
main()
{
Init_LCD1602();
w_string(0x80,word1);
while(1)
{
wendu=Readtemperature();
temp_g=wendu%100/10+'0';
temp_d=wendu%10+'0';
w_cmd(0xc2);
delay(2);
w_dat(temp_g);
delay(2);
w_dat(temp_d);
delay(2);
w_cmd(0xc4);
delay(2);
w_dat(0x2e);
delay(2);
w_cmd(0xc6);
delay(2);
w_dat(0xdf);
delay(2);
w_dat(0x43);
}
}