#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD1602_E=P2^7;
sbit LCD1602_RW=P2^6;
sbit LCD1602_RS=P2^5;
sbit DQ=P2^2;
void Delay1ms(uint );
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
void Lcd1602_Delay1ms(uint c);
void LcdWriteCom(uchar com);
void LcdWriteData(uchar dat);
void LcdInit();
void LcdDisplay2(int temp);
void LcdDisplay1(int temp);
uchar table1[]="temp1:";
uchar table2[]="temp2:";
uchar table3[2][8]={{0x98,0x00,0x00,0x04,0xe5,0x04,0x0a,0x28},
{152,0,0,4,229,4,10,40}};
uchar m,n;
void main()
{
uchar j;
LcdInit();
LcdWriteCom(0x80+13);
LcdWriteData('C');
LcdWriteCom(0x80+13+0x40);
LcdWriteData('C');
LcdWriteCom(0x80);
for(j=0;j<6;j++)
{
LcdWriteData(table1[j]);
}
LcdWriteCom(0x80+0x40);
for(j=0;j<6;j++)
{
LcdWriteData(table2[j]);
}
while(1)
{
if(n==0)
LcdDisplay1(Ds18b20ReadTemp());
if(n==1)
LcdDisplay2(Ds18b20ReadTemp());
}
}
void LcdDisplay1(int temp)
{
unsigned char datas[] = {0, 0, 0, 0, 0,0,0,0,0,0,0,};
float tp;
if(temp< 0)
{
LcdWriteCom(0x80+6);
LcdWriteData('-');
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else
{
LcdWriteCom(0x80+6);
LcdWriteData('+');
tp=temp;
temp=tp*0.0625*100+0.5;
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
LcdWriteCom(0x80+7);
LcdWriteData('0'+datas[0]);
LcdWriteCom(0x80+8);
LcdWriteData('0'+datas[1]);
LcdWriteCom(0x80+9);
LcdWriteData('0'+datas[2]);
LcdWriteCom(0x80+10);
LcdWriteData('.');
LcdWriteCom(0x80+11);
LcdWriteData('0'+datas[3]);
LcdWriteCom(0x80+12);
LcdWriteData('0'+datas[4]);
}
void LcdDisplay2(int temp)
{
unsigned char datas[] = {0, 0, 0, 0, 0,0,0,0,0,0,0,};
float tp;
if(temp< 0)
{
LcdWriteCom(0x80+6+0x40);
LcdWriteData('-');
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else
{
LcdWriteCom(0x80+6+0x40);
LcdWriteData('+');
tp=temp;
temp=tp*0.0625*100+0.5;
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
LcdWriteCom(0x80+7+0x40);
LcdWriteData('0'+datas[0]);
LcdWriteCom(0x80+8+0x40);
LcdWriteData('0'+datas[1]);
LcdWriteCom(0x80+9+0x40);
LcdWriteData('0'+datas[2]);
LcdWriteCom(0x80+10+0x40);
LcdWriteData('.');
LcdWriteCom(0x80+11+0x40);
LcdWriteData('0'+datas[3]);
LcdWriteCom(0x80+12+0x40);
LcdWriteData('0'+datas[4]);
}
void Lcd1602_Delay1ms(uint c)
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void LcdWriteCom(uchar com)
{
LCD1602_E = 0;
LCD1602_RS = 0;
LCD1602_RW = 0;
P0 = com;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
void LcdWriteData(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1;
LCD1602_RW = 0;
P0 = dat;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
void LcdInit()
{
Lcd1602_Delay1ms(2);
LcdWriteCom(0x38);
Lcd1602_Delay1ms(5);
LcdWriteCom(0x38);
Lcd1602_Delay1ms(5);
LcdWriteCom(0x38);
LcdWriteCom(0x38);
LcdWriteCom(0x08);
LcdWriteCom(0x01);
LcdWriteCom(0x06);
LcdWriteCom(0x0c);
}
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init()
{
uchar i;
DQ = 0;
i = 70;
while(i--);
DQ = 1;
i = 0;
while(DQ)
{
i++;
if(i>5)
{
return 0;
}
Delay1ms(1);
}
return 1;
}
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DQ = 0;
i++;
DQ = dat & 0x01;
i=6;
while(i--);
DQ = 1;
dat >>= 1;
}
}
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DQ = 0;
i++;
DQ = 1;
i++;
i++;
bi = DQ;
byte = (byte >> 1) | (bi << 7);
i = 4;
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
}
int Ds18b20ReadTemp()
{
uchar j;
int wendu ;
uchar tmh, tml;
for(m=0;m<2;m++)
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
Ds18b20Init();
Ds18b20WriteByte(0x55);
for(j=0;j<8;j++)
{
Ds18b20WriteByte(table3[m][j]);
n=m;
}
}
Ds18b20WriteByte(0xbe);
tml = Ds18b20ReadByte();
tmh = Ds18b20ReadByte();
wendu = tmh;
wendu<<= 8;
wendu |= tml;
return wendu;
}
|