精确到小数点后两位。
#include<AT89X51.h><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#define uchar unsigned char
#define uint unsigned int
sbit RS=P3^0;
sbit RW=P3^1;
sbit EN=P3^2;
sbit DQ=P3^7;
unsigned char code str1[]={"temperature is:"};
unsigned char code str2[]={" "};
uchar data disdata[6];
uint tvalue;
uchar tflag;
/*************************lcd1602**************************/
void delay1(unsigned int ms)
{unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<100;j++);
}
void wr_com(unsigned char com)
{ delay1(1);
RS=0;
RW=0;
EN=0;
P2=com;
delay1(1);
EN=1;
delay1(1);
EN=0;
}
void wr_dat(unsigned char dat)
{ delay1(1);;
RS=1;
RW=0;
EN=0;
P2=dat;
delay1(1);
EN=1;
delay1(1);
EN=0;
}
void lcd_init()
{
delay1(15);
wr_com(0x38);delay1(5);
wr_com(0x08);delay1(5);
wr_com(0x01);delay1(5);
wr_com(0x06);delay1(5);
wr_com(0x0c);delay1(5);
}
init_play()
{
uint m;
lcd_init();
wr_com(0x80);
for(m=0;m<16;m++)
{wr_dat(str1[m]);}
wr_com(0xCE);
wr_dat(0xDF);
wr_com(0xCF);
wr_dat(0x43);
//display(str2);
}
/******************************ds18b20***************************************/
void delay_18B20(unsigned int i)
{
while(i--);
}
void ds1820rst()
{ unsigned char x=0;
DQ = 1;
delay_18B20(4);
DQ = 0;
delay_18B20(100);
DQ = 1;
delay_18B20(40);
}
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()
{uchar a,b;
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0x44);
ds1820rst();
ds1820wr(0xcc);
ds1820wr(0xbe);
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
if(tvalue<0x0fff)
tflag=0;
else
{tvalue=~tvalue+1;
tflag=1;
}
tvalue=tvalue*(6.25);
return(tvalue);
}
/*******************************************************************/
void ds1820disp()
{
uchar flagdat;
uint k;
disdata[0]=tvalue/10000+0x30;
disdata[1]=tvalue%10000/1000+0x30;
disdata[2]=tvalue%1000/100+0x30;
disdata[3]=0x2e;
disdata[4]=tvalue%100/10+0x30;
disdata[5]=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;
}
}
wr_com(0xc2);
wr_dat(flagdat);
wr_com(0x80+0x45);
for(k=0;k<6;k++)
{
wr_dat(disdata[k]);
}
}
void main()
{ init_play();
while(1)
{read_temp();
ds1820disp();
}
}
文章评论(0条评论)
登录后参与讨论