#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include "lcd1602.h"
//
typedef unsigned char U8;
typedef signed char S8;
typedef unsigned int U16;
typedef signed int S16;
typedef unsigned long U32;
typedef signed long S32;
typedef float F32;
typedef double F64;
//
#define uchar unsigned char
#define uint unsigned int
sbit DHT = P2^0 ;
bit flagtemp;// U8count ,k;
bit flagsendtemp;
U8 U8FLAG;
U8 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;
U8 U8comdata;
U8 outdata[5];
U8 indata[5];
U8 count, count_r=0;
U8 str[5];
U16 DHTData1 ,DHTData2;
U8 data tab[6]={0x00,0x00,0x00,0x00,0x00,oxoo};
U8 code dis1[]={"Humdity: %"};
U8 code dis2[]={"Temper: c"};
void Delay(U16 j)
{ U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!DHT)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(DHT)U8temp=1;
U8FLAG=2;
while((DHT)&&U8FLAG++);
if(U8FLAG==1)break;
U8comdata<<=1;
U8comdata|=U8temp; //0
}//end i
}
//----????8λ== U8T_data_H------
//----????8λ== U8T_data_L------
//----????8λ== U8RH_data_H-----
//----????8λ== U8RH_data_L-----
//----У?? 8λ == U8checkdata-----
//----????????????????----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
{
//????????18ms
DHT=0;
Delay(5);
DHT=1;
//?????????????????? ???????40us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//??????????? ?ж?????????
DHT=1;
//?ж???????е????????? ?粻????????????????????????
if(!DHT) //T !
{
U8FLAG=2;
//?ж???????? 80us ?????????????????
while((!DHT)&&U8FLAG++);
U8FLAG=2;
//?ж???????? 80us ????????緢????????????????
while((DHT)&&U8FLAG++);
//?????????
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
DHT=1;
//????У??
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(1)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
DHTData1=U8RH_data_H;
DHTData1<<=8;
DHTData1|=U8RH_data_L;
tab[0]=DHTData1/100+0x30;
tab[1]=DHTData1%100/10+0x30;
tab[2]=DHTData1%10+0x30;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
DHTData2=U8T_data_H;
DHTData2<<=8;
DHTData2|=U8T_data_L;
if(DHTData2&0x8000)
{
flagtemp=1;
DHTData2&=0x7FFF;
}
tab[3]=DHTData2/100+0x30;
tab[4]=DHTData2%100/10+0x30;
tab[5]=DHTData2%10+0x30;
}//fi
}//fi
}
void display()
{
U8 m;
m=0;
writecmd(0x80);
while(dis1[m]!='\0')
{
writedata(dis1[m]);
m++;
}
writecmd(0x89);
writecmd(tab[0]);
writecmd(tab[1]);
writecmd('.');
writecmd(tab[2]);
m=0;
writecmd(0xc0);
while(dis1[m]!='\0')
{
writedata(dis2[m]);
m++;
}
writecmd(0xc8);
if(flagtemp)writedata('-');
else writedata('+');
writecmd(0xc9);
writecmd(tab[3]);
writecmd(tab[4]);
writecmd('.');
writecmd(tab[5]);
}
//----------------------------------------------
//main()????????: AT89C51 11.0592MHz ?????
//???????????,?????? 9600
//----------------------------------------------
void main()
{
U8 buff[4];
U8 timecount;
init_LCD();
while(1)
{
RH();
display();
Delay(2000);
}
}
|