#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
char sj,a=0,b=0,c,d;
uchar flag,count;
uchar Temp_value2;
uchar TempBufferTH[7],TempBufferTL[7],Temp_Buffer[9];
sbit k1 =P2^1;
sbit k2 =P2^2;
sbit k3 =P2^3;
sbit k4 =P2^4;
sbit DQ =P2^5;
sbit spk =P3^4;
sbit relay =P2^0;
//sbit gx =P2^7;
sbit RS =P3^5;
sbit RW =P3^6;
sbit EN =P3^7;
sfr DBPort =0x80;
uchar code str1[]=" "; //可以省略,但相应位置必须清屏
uchar code str2[]="RESET ALERT CODE ";
uchar code str3[]="LOOK ALERT CODE ";
uchar code str4[]="TH: ";
uchar code str7[]="TL: ";
uchar code str5[]=">H ";
uchar code str6[]="<L ";
uchar code str9[]="DS18B20 OK ";
uchar code str10[]="TEMP: ";
uchar code str11[]="DS18B20 ERROR ";
uchar code str12[]="TEMP: ^_^||| ";
void delay(uchar z) //1ms*Z延时函数
{ uchar i,j;
for(i=z;i>0;i--)
for(j=120;j>0;j--)
{;}
}
void SPK_BZ() //声响函数
{
uchar i,j,k;
for(k=0;k<1;k++)
{
for(i=0;i<160;i++)
{
spk=~spk;
for(j=0;j<100;j++);
}
delay(200);
}
}
/////////////////////////////////////////////////////////
void delay_18B20(unsigned int i) //(i/100)延时函数
{
while(i--);
}
init_Ds18b20(void)
{
flag=0;
DQ = 1; //DQ复位
delay_18B20(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay_18B20(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay_18B20(14);
flag=DQ; //稍做延时后 如果flag=0则初始化成功 flag=1则初始化失败
delay_18B20(20);
DQ =1;
return flag;
}
void Write_Ds18b20(uchar dat) //写
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(5);
DQ = 1;
dat>>=1;
}
}
unsigned char Read_18b20() // 读
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay_18B20(4);
}
return(dat);
}
///////////////////////////LCD1602(20*2)/////////////////////////////////////////////////////////
void WR_COMM(uchar c) //写命令
{delay(100);
EN=0;
RS=0;
RW=0;
EN=1;
P0=c;
delay(10);
}
void WR_DATA(uchar c) //写数据
{delay(100);
EN=0;
RS=1;
RW=0;
EN=1;
P0=c;
delay(10);
}
unsigned char LCD_Wait()
{
RS=0;
RW=1;
delay(1);
EN=1;
delay(1);
EN=0;
return DBPort;
}
void LCD_Write(bit style, unsigned char input)
{
EN=0;
RS=style;
RW=0;
delay(1);
DBPort=input;
delay(1);//注意顺序
EN=1;
delay(1);//注意顺序
EN=0;
delay(1);
LCD_Wait();
}
void ShowChar(uchar pos,uchar c)
{ uchar p;
if(pos<0x14)
p=pos+0x80;
else p=pos+0xac;
WR_COMM(p);
WR_DATA(c);
}
void ShowString(uchar line,uchar*prt)
{ uchar L,i;
L=line*0x14;
for(i=0;i<20;i++)
{ShowChar(L++,*(prt+i));
}
}
void Goto(unsigned char x, unsigned char y)
{
if(y==0)
LCD_Write(0,0x80|x);
if(y==1)
LCD_Write(0,0x80|(x-0x40));
}
void Print(unsigned char *str)
{
while(*str!='\0')
{
LCD_Write(1,*str);
str++;
}
}
void MEU_ERROR()
{
ShowString(0,str11);
Goto(0,1);
Print(str12);
}
void init_Lcd(void) //LCD初始化
{
WR_COMM(0x38);
delay(5);
WR_COMM(0x38);
delay(5);
WR_COMM(0x0c);
delay(5);
WR_COMM(0x01);
delay(5);
WR_COMM(0x80);
}
//////////////////////////////////////////////////////////////////
void RE_TEMP() //读取温度值,报警值
{
init_Ds18b20();
Write_Ds18b20(0xcc);
Write_Ds18b20(0x44);
delay_18B20(100);
init_Ds18b20();
Write_Ds18b20(0xcc);
Write_Ds18b20(0xbe);
delay_18B20(100);
a=Read_18b20();//温度低8位
b=Read_18b20();//温度高8位
Temp_value2=b<<=8;
Temp_value2|=a;
Temp_value2*=0.0625; // DS18B20 0.0625最小刻度
Temp_value2=Temp_value2*10; //四舍五入
sj=Temp_value2/10;
}
void Temptostr()
{
Temp_Buffer[0]=Temp_value2/10+'0'; //0的ASCll 为 48
Temp_Buffer[1]=Temp_value2/10%10+'0';
Temp_Buffer[2]='.'; //实时温度
Temp_Buffer[3]=Temp_value2%10+'0';
Temp_Buffer[4]=0xdf;
Temp_Buffer[5]='C';
Temp_Buffer[6]='\0';
}
void TempTHtostr()
{
TempBufferTH[0]=c/100+'0';
TempBufferTH[1]=c%100/10+'0';
TempBufferTH[2]=c%10+'0';
TempBufferTH[3]=0xdf;
TempBufferTH[4]='C';
TempBufferTH[5]='\0';
}
void TempTLtostr()
{
TempBufferTL[0]=d/100+'0';
TempBufferTL[1]=d%100/10+'0';
TempBufferTL[2]=d%10+'0';
TempBufferTL[3]=0xdf;
TempBufferTL[4]='C';
TempBufferTL[5]='\0';
}
void LOOK_ALARM() //查看报警温度函数
{
TempTHtostr();
TempTLtostr();
ShowString(0,str3);
Goto(0,1);
Print(str4);
Goto(10,1);
Print(str7);
Goto(3,1);
Print(TempBufferTH);
Goto(13,1);
Print(TempBufferTL);
delay(20);
}
void RESET_ALARM() //设置报警值
{
TempTHtostr();
TempTLtostr();
ShowString(0,str2);
Goto(0,1);
Print(str4);
Goto(10,1);
Print(str7);
Goto(3,1);
Print(TempBufferTH);
Goto(13,1);
Print(TempBufferTL);
delay(20);
}
void MEU_OK() //OK
{
RE_TEMP();
Temptostr();
ShowString(0,str9);
Goto(0,1);
Print(str10);
Goto(5,1);
Print(Temp_Buffer);
delay(20);
}
void KEY_VALUE() //k1
{
uchar cn=4;
while(cn--)
{
if(k1==0)
{ delay(10);
if(k1==0)
{
SPK_BZ();
count++;
if(count>4)
{count=0;}
}
while(k1==0);
}
}
if(k4!=1)
{ count=0;
SPK_BZ();
}
}
void GN() //功能键
{
KEY_VALUE();
switch(count)
{
case 0:
{ WR_COMM(0x01);
MEU_OK();
if(sj>c)
{
Goto(12,1);
Print(str5);
relay=1;
while(k1!=0)
{ SPK_BZ();}
}
else
{ if(sj<d)
{
Goto(12,1);
Print(str6);
relay=0;
while(k1!=0)
{SPK_BZ();}
}
else
{ Goto(12,1);
Print("^_^");
}
while(k1!=0);
}
} break;
case 1:
{ WR_COMM(0x01);
LOOK_ALARM();
while(k4!=0&&k1!=0);
} break;
case 2:
{ WR_COMM(0x01);
RESET_ALARM();
while(k4!=0&&k1!=0);
} break;
case 3: //调TH
{
if(k2==0)
{ delay(10);
if(k2==0)
{ SPK_BZ();
c++;
if(c>128)
{ c=-5;}
RESET_ALARM();
}
while(k2==0)
{
SPK_BZ();
c++;
if(c>128)
{ c=-5;}
RESET_ALARM();
}
}
else if(k3==0)
{ delay(10);
if(k3==0)
{ SPK_BZ();
c--;
if(c<-5)
{ c=128;}
RESET_ALARM();
}
while(k3==0)
{
SPK_BZ();
c--;
if(c<-5)
{ c=128;}
RESET_ALARM();
}
}
}break;
case 4: //调TL
{
if(k2==0)
{ delay(10);
if(k2==0)
{ SPK_BZ();
d++;
if(d>128)
{ d=-5;}
RESET_ALARM();
}
while(k2==0)
{
SPK_BZ();
d++;
if(d>128)
{ d=-5;}
RESET_ALARM();
}
}
if(k3==0)
{ delay(10);
if(k3==0)
{ SPK_BZ();
d--;
if(d<-5)
{ d=128;}
RESET_ALARM();
}
while(k3==0)
{ SPK_BZ();
d--;
if(d<-5)
{ d=128;}
RESET_ALARM();