#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#define uint unsigned int
#define uchar unsigned char
uint WtkCount=0;
bit WtkFlag=0;
sbit DATA_PIN=P1^5;
//温湿度定义
uchar ucharFLAG,uchartemp;
uchar shidu_shi,shidu_ge,wendu_shi,wendu_ge=4;
uchar ucharT_data_H,ucharT_data_L,ucharRH_data_H,ucharRH_data_L,ucharcheckdata;
uchar ucharT_data_H_temp,ucharT_data_L_temp,ucharRH_data_H_temp,ucharRH_data_L_temp,ucharcheckdata_temp;
uchar ucharcomdata;
uchar shidu,wendu;
//延时函数
void Delay_us() //1 us延时
{
nop;
nop; nop; nop; nop; nop; nop; nop; nop;
}
void Delay_10us() //10 us延时
{
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
}
//温湿度传感
void COM(void) // 温湿写入
{
uchar i;
for(i=0;i<8;i++)
{
ucharFLAG=2;
DATA_PIN=0;
DATA_PIN=1;
while((!DATA_PIN)&&ucharFLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
uchartemp=0;
if(DATA_PIN)uchartemp=1;
ucharFLAG=2;
while((DATA_PIN)&&ucharFLAG++);
DATA_PIN=0;
DATA_PIN=1;
if(ucharFLAG==1)break;
ucharcomdata<<=1;
ucharcomdata|=uchartemp;
}
}
void DHT11(void) //温湿传感启动
{
DATA_PIN=0; //拉低总线
Timer0_Delay1ms(19); //>18MS
DATA_PIN=1; //总线拉高,此时已改变IO状态为读
// P0DIR &= ~0x01; //重新配置IO口方向
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
DATA_PIN=1;
if(!DATA_PIN) //等待传感器拉低总线
{
ucharFLAG=2;
while((!DATA_PIN)&&ucharFLAG++);
ucharFLAG=2;
while((DATA_PIN)&&ucharFLAG++); //传感器开始发送数据
COM();
ucharRH_data_H_temp=ucharcomdata;
COM();
ucharRH_data_L_temp=ucharcomdata;
COM();
ucharT_data_H_temp=ucharcomdata;
COM();
ucharT_data_L_temp=ucharcomdata;
COM();
ucharcheckdata_temp=ucharcomdata;
DATA_PIN=1; //接收完成,拉高总线
uchartemp=(ucharT_data_H_temp+ucharT_data_L_temp+ucharRH_data_H_temp+ucharRH_data_L_temp);
if(uchartemp==ucharcheckdata_temp)
{
ucharRH_data_H=ucharRH_data_H_temp;
ucharRH_data_L=ucharRH_data_L_temp;
ucharT_data_H=ucharT_data_H_temp;
ucharT_data_L=ucharT_data_L_temp;
ucharcheckdata=ucharcheckdata_temp;
}
//只使用了高八位数据(整数),低八位数据为小数
wendu_shi=ucharT_data_H/10;
wendu_ge=ucharT_data_H%10;
shidu_shi=ucharRH_data_H/10;
shidu_ge=ucharRH_data_H%10;
}
}
void main (void)
{
Set_All_GPIO_Quasi_Mode;
LED=1;
while(1)
{
DHT11();
wendu=(wendu_shi*10)+wendu_ge;
shidu=(shidu_shi*10)+shidu_ge;
}
}
|