#include <reg52.h>
#include <stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0
bit ReadTempFlag;
bit ReadRHFlag;
bit ReadVFlag;
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
sbit DQ=P3^3;
sbit DHT=P3^6;
char dat_t[4],dat_v[4],dat_s[4];
bit ReadTmpFlag,ReadRHFlag,OVERFLOWFLAG;
void Delay_Us(unsigned char t)
{
while(--t);
}
void Delay_Ms(unsigned char t)
{
while(t--)
{
Delay_Us(245);
Delay_Us(245);
}
}
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1;
Delay_Us(5);
DQ = 0;
Delay_Us(200);
Delay_Us(200);
DQ = 1;
Delay_Us(50);
dat=DQ;
Delay_Us(25);
return dat;
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
Delay_Us(25);
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay_Us(25);
DQ = 1;
dat>>=1;
}
Delay_Us(25);
}
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Delay_Ms(10);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
b<<=8;
t=a+b;
return(t);
}
void DHT_Start()
{
DHT = 1;
Delay_Us(2);
DHT = 0;
Delay_Ms(30);
DHT = 1;
Delay_Us(30);
}
uchar DHT_rec_byte()
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
while(!DHT);
Delay_Us(8);
dat<<=1;
if(DHT==1)
dat+=1;
while(DHT);
}
return dat;
}
void DHT_receive()
{
uchar R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
DHT_Start();
if(DHT == 0)
{
while(DHT == 0);
Delay_Us(40);
R_H = DHT_rec_byte();
R_L = DHT_rec_byte();
T_H = DHT_rec_byte();
T_H = DHT_rec_byte();
revise = DHT_rec_byte();
Delay_Us(25);
if((R_H+R_L+T_H+T_L)==revise)
{
RH = R_H;
RL = R_L;
TH = T_H;
TL = T_L;
}
dat_s[0] = '0' + (RH/10);
dat_s[1] = '0' + (RH%10);
dat_s[2] = '0' + (TH/10);
dat_s[3] = '0' + (TH%10);
}
}
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS=0;
RW=1;
EN=0;
_nop_();
EN=1;
return (bit)(DataPort & 0x80);
}
void LCD_Write_Com(unsigned char com)
{
Delay_Ms(5);
RS=0;
RW=0;
EN=1;
DataPort= com;
_nop_();
EN=0;
}
void LCD_Write_Data(unsigned char dt)
{
Delay_Ms(5);
RS=1;
RW=0;
EN=1;
DataPort= dt;
_nop_();
EN=0;
}
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
Delay_Ms(5);
}
void LCD_Write_String(uchar ad,uchar *s)
{
LCD_Write_Com(ad);
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
void LCD_Write_Char(uchar ad,uchar Data)
{
LCD_Write_Com(ad);
LCD_Write_Data( Data);
}
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*ÏÔê¾Ä£ê½éèÖÃ*/
Delay_Ms(5);
LCD_Write_Com(0x38);
Delay_Ms(5);
LCD_Write_Com(0x38);
Delay_Ms(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*ÏÔê¾1رÕ*/
LCD_Write_Com(0x01); /*ÏÔê¾ÇåÆá*/
LCD_Write_Com(0x06); /*ÏÔê¾1a±êòƶˉéèÖÃ*/
Delay_Ms(5);
LCD_Write_Com(0x0C); /*ÏÔ꾿a¼°1a±êéèÖÃ*/
}
void Init_Timer1(void)
{
TMOD |= 0x10;
EA=1;
ET1=1;
TR1=1;
}
void Init_Timer0(void)
{
TMOD= 0x10|0x04;
EA = 1;
ET0=1;
TR0 = 1;
}
void main()
{
int Tmp,v;
float Temp;
LCD_Init();
Delay_Ms(20);
LCD_Clear();
Init_Timer1();
while(1)
{
if(ReadTmpFlag == 1)
{
ReadTmpFlag = 0;
Tmp = ReadTemperature();
Temp=(float)Tmp*0.0625;
sprintf(dat_t,"%2.0f",Temp);
LCD_Write_String(0x80,dat_t);
LCD_Write_Char(0x82,0xDF);
LCD_Write_Char(0x83,'C');
}
if(ReadRHFlag ==1)
{
Init_Timer0();
ReadRHFlag = 0;
if((dat_s[2]==dat_t[0])&&(dat_s[3]==dat_t[1]))
{
LCD_Write_Char(0xC0,dat_s[0]);
LCD_Write_Char(0xC1,dat_s[1]);
LCD_Write_Char(0xC2,'%');
}
else
{
LCD_Write_String(0x86,"Error");
LCD_Write_Char(0x8b,dat_s[0]);
LCD_Write_Char(0x8c,dat_s[1]);
LCD_Write_Char(0x8e,dat_s[2]);
LCD_Write_Char(0x8f,dat_s[3]);
}
}
if(ReadVFlag==1)
{
ReadVFlag = 0;
v = v*5; //1s
sprintf(dat_v,"%2.0f",dat_v);
LCD_Write_String(0xC5,dat_v);
LCD_Write_String(0xCA,"m/s");
TR0=1;
ET0=1;
}
}
}
void Timer0() interrupt 1
{
OVERFLOWFLAG;
TH0 = 0;
TL0 = 0;
}
void Timer1() interrupt 3
{
static uint counter,c,d;
counter++;
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
if(counter == 10)
{
counter = 0;
c++;
d++;
ReadTmpFlag = 1;
}
if(c == 5)
{
c = 0;
ReadRHFlag =1;
}
if(d == 20)//2000ms=2s
{
ET0 =1;
TR0 = 1;
ReadVFlag =1;
TH0 = 0x00;
TL0 = 0x00;
}
if(counter == 22)
{
c =0;
TR0=0;
ET0=0;
}
}
|