????????利用DS18B20实时检测温度并显示在LCD1602显示屏上,同时可以通过K1,K2,K3,K4设置最高温度和最低温度利用AT24C02可以实现掉电不丢失,最后当检测温度大于或小于最高温时,会在LCD1602显示屏上显示OV:H或OV:L。
????????AT89C52、LCD1602显示屏、4只按键、AT24C02、DS18B20。
?
????????AT24C02芯片:2K位串行CMOS E2PROM, 内部含有256个8位字节,AT24C02是一种可以实现掉电不丢失的存储器,可用于保存单片机运行时想要永久保存的数据信息。
存储介质:EEPROM;通讯接口:I2C总线。
??
????????DS18B20数字温度传感器:输出的是数字信号,具有体积小,硬件开销低,抗干扰能力强,精度高的特点。[1]? DS18B20数字温度传感器接线方便,封装成后可应用于多种场合。通讯接口:单总线协议进行通信(One-Wire)
?
unsigned char onewire_init(void)
{
unsigned char i;
unsigned char ackbit;
onewire_dq = 1;
onewire_dq = 0;
i = 247;
while (--i); //delay 500us
onewire_dq = 1;
i = 32;
while (--i); //delay 70us
ackbit = onewire_dq;
i = 247;
while (--i); //delay 500us
return ackbit;
}
unsigned char onewire_receivebit(void)
{
unsigned char i;
unsigned char Bit;
onewire_dq = 0;
i = 2;
while (--i); //delay 9us
onewire_dq = 1;
i = 2;
while (--i); //delay 9us
Bit = onewire_dq;
i = 22;while (--i); //delay 50us
return Bit;
}
unsigned char onewire_sendbit(unsigned char Bit)
{
unsigned char i;
onewire_dq = 0;
i = 4;
while (--i); //delay 14us
onewire_dq = Bit;
i = 22;
while (--i); //delay 50us
onewire_dq = 1;
}
?
#include <REGX52.H>
#include "LCD1602.h"
#include "onewire.h"
#include "DS18B20.h"
#include "key.h"
#include "AT24C02.h"
float t,tshow;
char tlow,thigh;
unsigned char keynum;
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
}
while (--i);
}
void main()
{
LCD_Init();
LCD_ShowString(1,1,"T:");
LCD_ShowString(2,1,"TH:");
LCD_ShowString(2,9,"TL:");
thigh = AT24C02_ReadByte(0);
tlow = AT24C02_ReadByte(1);
while(1)
{
LCD_ShowSignedNum(2,4,thigh,3);
LCD_ShowSignedNum(2,12,tlow,3);
keynum = key();
ds18b20_convert();
t = ds18b20_readt();
if(t<0)
{
LCD_ShowChar(1,3,'-');
tshow= -t;
}
else{
LCD_ShowChar(1,3,'+');
tshow = t;
}
LCD_ShowNum(1,4,tshow,3);
LCD_ShowChar(1,7,'.');
LCD_ShowNum(1,8,(unsigned long)(tshow*100)%100,2);
if(keynum)
{
if(keynum==1)
{
thigh++;
if(thigh>125)
{
thigh=125;
}
}
if(keynum==2)
{
thigh--;
if(thigh<=tlow)
{
thigh++;
}
}
if(keynum==3)
{
tlow++;
if(tlow>=thigh)
{
tlow--;
}
}
if(keynum==4)
{
tlow-- ;
if(tlow<-55)
{
tlow = -55;
}
}
if(tshow>thigh)
{
LCD_ShowString(1,13,"OV:H");
}
else if(tshow<tlow){
LCD_ShowString(1,13,"OV:L");
}
else{
LCD_ShowString(1,13," ");
}
AT24C02_WriteByte(0,thigh);
Delay5ms();
AT24C02_WriteByte(1,tlow);
Delay5ms();
}
}
}
????????由测试图中的LCD1602显示屏可以得出:当前室温是24.18℃,最高温限制为21℃,最低温限制为-2℃。?