|
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char U8; /* ???8????? */
typedef unsigned int U16; /* ???16????? */
sbit sclk = P1^0;
//sbit io= P1^1;
//sbit rst= P1^2;
sbit en=P2^5; //?????
sbit rs=P2^3; //?????????
sbit rd=P2^4;
sbit DQ=P2^2; //DS18B20
sbit buzz = P1^3;
sbit P3_2 = P2^7; //DHT11???
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
sbit key4 = P3^7;
sbit led1 = P1^2;//RED
sbit led2 = P1^4;//GREEN
sbit led3 = P1^5;//YELLOW
uchar tplsb,tpmsb; //
uint temp,temp1;
U8 U8FLAG;
U8 U8comdata;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,
U8RH_data_L_temp,U8checkdata_temp;
#define uchar unsigned char
#define uint unsigned int
#define CONTROL 0x80
#//define TIMING 0x81
#define DATA0LOW 0x8C
#define DATA0HIGH 0x8D
#define DATA1LOW 0x8E
#define DATA1HIGH 0x8F
uchar DataLow0,DataHigh0,DataLow1,DataHigh1,commad;
bit keyflag=0;
uchar tem,temlow=20,temhigh=40;
uchar code ASCII[]="0123456789%c.";
uchar show;
/*************************????*******************************/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned int t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void delayus()
{
_nop_();
_nop_();
_nop_();
_nop_();
}
void delay1(int ms)
{
unsigned char y ;
while(ms--)
{
for(y=0;y<250;y++)
{
_nop_() ;
_nop_() ;
_nop_() ;
_nop_() ;
}
}
}
void delay2(){;;;}
/**************************LCD1602??*****************************/
void write1602_com(uint com)
{
rs=0;
rd=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write1602_date(uchar date)
{
rs=1;
rd=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void write1602(uchar add,uchar dat)
{
write1602_com(add);
write1602_date(dat);
}
/*------------------------------------------------
???????
------------------------------------------------*/
void write1602_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
write1602_com(0x80 + x);
}
else
{
write1602_com(0xC0 + x);
}
while (*s)
{
write1602_date( *s);
s ++;
}
}
/*------------------------------------------------
??????
------------------------------------------------*/
void write1602_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 1)
{
write1602_com(0x80 + x);
}
else
{
write1602_com(0xC0 + x);
}
write1602_date( Data);
}
void Clear1602(void) //????
{
write1602_com(0x01);
DelayMs(5);
}
void init1602(void) //1602???
{
en=0;
write1602_com(0x38);
write1602_com(0x0c);
write1602_com(0x06);
write1602_com(0x01);
}
/*********************DS18B20??***********************************/
/*******************?????????DS18B20**************/
void reset(void)
{
uint i;
DQ=0;
i=100;
while(i--);
DQ=1;// ?????
i=4;
while(i--);
}
/********************* ??????*********************/
void wait(void)
{
uint i;
while(DQ);
while(~DQ); // ???????
i=4;
while(i--);
}
bit readbit(void)
{
uint i;
bit b;
DQ=0;
i++;
DQ=1;
i++;i++;// ??15us??,???????15us,DS18B20???????
b=DQ;
i=10;
while(i--);
return (b);
}
/******************* ?????????**************************/
uchar readbyte(void)
{
uchar i,j,b;
b=0;
for(i=0;i<8;i++)
{
j=readbit();
b=(j<<7)|(b>>1);
}
return(b);
}
/******************????????,???1??0?????************/
void writebyte(uchar b)
{
uint i;
uchar j;
bit btmp;
for(j=0;j<8;j++)
{
btmp=b&0x01;
b=b>>1; // ????(??????)
if(btmp)
{
DQ=0;
i++;i++;
DQ=1;
i=10;
while(i--);// ???1?????60us
}
else
{
DQ=0;
i=10;
while(i--); // ????60us?120us??
DQ=1;
i++;i++;
}
}
}
/************************* ??????********************/
void convert(void)
{
reset();// ??????,???DS18B20
wait();//??DS18B20??????
writebyte(0xcc);// skip rom ??
writebyte(0x44);// convert T ??
}
/********************* ?????****************************/
uint readtemp(void)
{
float tt;
reset();
wait();
delay(1);
writebyte(0xcc);// skip rom ??
writebyte(0xbe);// read scratchpad ??
tplsb=readbyte();// ???????(???4??????"??"??)
tpmsb=readbyte();// ???????(???5?????)
temp=tpmsb;
temp<<=8;
temp=temp|tplsb;
tt=temp*0.0625;
temp=tt*10+0.5;
return (temp);
}
/******************????**********************/
void ds18B20disp(uint t)
{
uchar a1,a2,a3,a4;
a1=t/100;
a2=t%100;
a3=a2/10;
a4=a2%10;
write1602_com(0x80+0x40);//????9????????????????????
write1602_date(a1+0x30);
delay(1);
write1602_date(a3+0x30);
delay(1);
write1602_date('.');
delay(1);
write1602_date(a4+0x30);
write1602_date('C');
delay(1);
}
/********************????????************/
void keyscan()
{
unsigned char turn=0;
bit change=0;
// led1=0;
led2=0;
if(key4==0)
{
DelayMs(10);
if(key4==0)
{
show++;
}
while(!key4);
Clear1602();
}
if(show==1)
{
timedisp();
if(keyflag==0)
{
readtime();
write_second();
write_minute();
write_hour();
write_day();
write_month();
write_year();
write_week();
}
}
if(show==2)
{
unsigned char *pa,*pb;
pa="Temperature";
pb=" ";
write1602_String(0,0,pa);
write1602_String(0,1,pb);
show=0;
}
if(key1==0)
{
led3=0;
DelayMs(10);
if(key1==0)
{
turn++;
}
while(!key1);
Clear1602();
}
while(turn)
{
if(key1==0)
{
DelayMs(90);
if(key1==0)
{
turn++;
}
while(!key1);
Clear1602();
}
if(turn==1)
{
write1602_String(0,0,"Temperature Set");
write1602_String(0,1,"min max");
if(key4==0)
{
DelayMs(10);
if(key4==0)
{
change=~change;
}
while(!key4);
}
if(change==0)
{
write1602_String(4,2,">");
write1602_String(13,2," ");
write1602_Char(5, 2, ASCII[ temlow/10 ] );
write1602_Char(6, 2, ASCII[ temlow%10 ] );
write1602_Char(14, 2, ASCII[ temhigh/10 ] );
write1602_Char(15, 2, ASCII[ temhigh%10 ] );
if(!key2)
{
DelayMs(90);
if(key2==0)
{
if(temlow>=0&&temlow<99)
temlow++;
}
while(!key2);
}
if(key3==0)
{
DelayMs(90);
if(key3==0)
{
if(temlow>0&&temlow<=99)
temlow--;
}
while(!key3);
}
write1602_Char(5, 2, ASCII[ temlow/10 ] );
write1602_Char(6, 2, ASCII[ temlow%10 ] );
write1602_Char(14, 2, ASCII[ temhigh/10 ] );
write1602_Char(15, 2, ASCII[ temhigh%10 ] );
}
if(change==1)
{
write1602_String(4,2," ");
write1602_String(13,2,">");
write1602_Char(5, 2, ASCII[ temlow/10 ] );
write1602_Char(6, 2, ASCII[ temlow%10 ] );
write1602_Char(14,2, ASCII[ temhigh/10 ] );
write1602_Char(15,2, ASCII[ temhigh%10 ] );
if(!key2)
{
DelayMs(90);
if(key2==0)
{
if(temhigh>=0&&temhigh<99)
temhigh++;
}
while(!key2);
}
if(key3==0)
{
DelayMs(90);
if(key3==0)
{
if(temhigh>0&&temhigh<=99)
temhigh--;
}
while(!key3);
}
write1602_Char(5, 2, ASCII[ temlow/10 ] );
write1602_Char(6, 2, ASCII[ temlow%10 ] );
write1602_Char(14,2, ASCII[ temhigh/10 ] );
write1602_Char(15,2, ASCII[ temhigh%10 ] );
}
}
if(turn>=2)
{
unsigned char *pa,*pb;
pa="Temperature";
pb=" ";
write1602_String(0,0,pa);
write1602_String(0,1,pb);
turn=0;
}
}
}
void main()
{
uint t;
unsigned char *pa,*pb;
pa="Temperature";
pb=" ";
init1602();
// init_1302();
// TSL2561_Write(CONTROL,0x03);
convert();
delay(1000);// 启动温度转换,延时750ms
readtemp(); // 读取温度
write1602_String(0,0,pa);
write1602_String(0,1,pb);
buzz =0;
t=readtemp();
while(1)
{
led3=1;
if(show==0||show==2)
{
convert();
ds18B20disp(readtemp());
}
if((((temp/10)%100)>=temhigh)||(((temp/10)%100)<=temlow))
{
buzz=1;//蜂鸣器报警
led1=0;//红灯亮、
led2=1;
delay(1000);
}
if((((temp/10)%100)<temhigh)||(((temp/10)%100)>temlow))
{
buzz=0;//蜂鸣器报警
led1=1;//红灯亮
}
keyscan();
// warning();
}
}
|
|