#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD1602_E=P2^7;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^6;
sbit DQ=P3^7;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
void Lcd1602_Delay1ms(uint c);
void LcdWriteCom(uchar com);
void LcdWriteData(uchar dat);
void LcdInit();
void LcdDisplay();
uchar table1[]="tempris:";
uchar table2[]="high:35 low:25 ";
void zhongduan_123();
uchar shangxian=35,xiaxian=25;
void keyscan();
int wendu;
uchar fg,k;
void delay(uint i);
void delay_50us(uint t);
void main()
{
uchar j;
float tp;
LcdInit();
//zhongduan_123();
LcdWriteCom(0x80+15);
LcdWriteData('C');
LcdWriteCom(0x80);
for(j=0;j<8;j++)
LcdWriteData(table1[j]);
LcdWriteCom(0x80+0x40);
for(j=0;j<16;j++)
LcdWriteData(table2[j]);
while(1)
{
wendu=Ds18b20ReadTemp();
if(wendu< 0)
{
LcdWriteCom(0x80+8);
LcdWriteData('-');
wendu=wendu-1;
wendu=~wendu;
tp=wendu;
wendu=tp*0.0625*100+0.5;
}
else
{
LcdWriteCom(0x80+8);
LcdWriteData('+');
tp=wendu;
wendu=tp*0.0625*100+0.5;
}
LcdDisplay();
keyscan();
}
}
void LcdDisplay(int wendu)
{
unsigned char datas[] = {0, 0, 0, 0, 0,0,0,0,0,0,0,};
datas[0] = wendu / 10000;
datas[1] = wendu % 10000 / 1000;
datas[2] = wendu % 1000 / 100;
datas[3] = wendu % 100 / 10;
datas[4] = wendu % 10;
LcdWriteCom(0x89);
LcdWriteData('0'+datas[0]);
LcdWriteCom(0x80+10);
LcdWriteData('0'+datas[1]);
LcdWriteCom(0x80+11);
LcdWriteData('0'+datas[2]);
LcdWriteCom(0x80+12);
LcdWriteData('.');
LcdWriteCom(0x80+13);
LcdWriteData('0'+datas[3]);
LcdWriteCom(0x80+14);
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); //跳过ROM操作命令
Ds18b20WriteByte(0x44); //温度转换命令
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳过ROM操作命令
Ds18b20WriteByte(0xbe); //发送读取温度命令
}
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先写入转换命令
Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
tmh = Ds18b20ReadByte(); //再读高字节
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
void zhongduan_123()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
}
void liran() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
k++;
if(key1==0)
delay(10);
if(key1==0)
{
fg++;
if(fg==3)
fg=0;
}
if(fg==1)
delay(10);
if(fg==1)
{
if(key2==0)
delay(10);
if(key2==0)
{
shangxian++;
}
if(key3==0)
delay(10);
if(key3==0)
{
shangxian--;
}
if(shangxian>99)
shangxian=99;
if(shangxian<=xiaxian)
shangxian=xiaxian;
}
if(fg==2)
delay(10);
if(fg==2)
{
if(key2==0)
delay(10);
if(key2==0)
{
xiaxian++;
}
if(key3==0)
delay(10);
if(key3==0)
{
xiaxian--;
}
if(xiaxian<0)
xiaxian=0;
if(xiaxian>shangxian)
xiaxian=shangxian;
}
}
void keyscan()
{
while(fg==1)
{
LcdWriteCom(0x80+0x46);
LcdWriteData(shangxian);
delay(10);
}
while(fg==2)
{
LcdWriteCom(0x85+0x49);
LcdWriteData(xiaxian);
delay(10);
}
}
|