#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit rd=P2^1;
sbit lcden=P2^2;
sbit DQ=P3^0;
float TM;
int tvalue;
unsigned char tflag;
unsigned char data disdata[5];
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unsigned char com)
{
rs=0;
rd=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(unsigned char date)
{
rs=1;
rd=0;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void delay_18B20(unsigned int i)
{
while(i--);
}
void ds1820rst() /*ds1820¸′λ*/
{
unsigned char x=0;
DQ = 1;
delay_18B20(4);
DQ = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(20);
x=DQ;
delay(30);
DQ=1;
}
uchar ds1820rd()
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay_18B20(10);
}
return(dat);
}
void ds1820wr(uchar wdata)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = wdata&0x01; //
delay_18B20(10);
DQ = 1;
wdata>>=1;
}
}
read_temp()
{
unsigned char a,b;
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0x44);
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0xbe);
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
TM=tvalue*0.625;
tvalue=TM*1+0.5;
/*
if(tvalue<0x0FFF)
tflag=0;
else
{tvalue=~tvalue+1;
tflag=1;
}
tvalue=tvalue*(0.425);
return(tvalue);
}
void ds1820disp()
{
unsigned char flagdat;
disdata[0]=tvalue/1000+0x30;
disdata[1]=tvalue%1000/100+0x30;
disdata[2]=tvalue%100/10+0x30;
disdata[3]=tvalue%10+0x30;
if(tflag==0)
flagdat=0x20;
else
flagdat=0x2d;
if(disdata[0]==0x30)
{disdata[0]=0x20;
if(disdata[1]==0x30)
{disdata[1]=0x20;
}
}
write_com(0x80+0x40);
write_date(flagdat);
write_com(0x80+0x41);
write_date(disdata[0]);
write_com(0x80+0x42);
write_date(disdata[1]);
write_com(0x80+0x43);
write_date(disdata[2]);
write_com(0x80+0x44);
write_date(0x2e);
write_com(0x80+0x45);
write_date(disdata[3]);
write_com(0x80+0x46);
write_date('C');
}
void InitLcd1602()
{
write_com(0x38);
write_com(0x0C);
write_com(0x06);
write_com(0x01);
}
void main()
{
lcden=0;
InitLcd1602();
while(1)
{
read_temp();
ds1820disp();
}
}
新手勿喷。
|