温湿度计——DHT11(液晶显示)

1062液晶显示温度和湿度,第一行显示湿度,第二行显示温度。

原理是DHT11输出一个八位二进制数,前四位是湿度,后四位是温度。单片机用P3.0来接收。

代码:

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char 
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden = P3^4;
sbit lcdrs = P3^5;
typedef unsigned char unint8;
typedef unsigned char unint16;
unsigned char table[]={
"0123456789"
};
unsigned char str1[]="00.0";
unsigned char str2[]="00.0";
sbit TRH = P3^0;//温湿度传感器DHT11数据接入
sbit ST  = P2^1;//状态显示,是为了方便调试,具体使用的时候可以不要
sbit LCD_RS = P2^6;            
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;
void disp(unint8 str1,unint8 str2,unint8 str3,unint8 str4,unint8 str5,unint8 str6);
unint8 TH_data,TL_data,RH_data,RL_data,CK_data;
unint8 TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
unint8 com_data,untemp,temp;
unint8 respond;
uchar num;
void delayNOP()
{
         _nop_();
         _nop_();
         _nop_();
         _nop_();
}

// 毫秒级延时子程序
void delay_ms(unsigned char ms)
{       
          unsigned char i;
          while(ms--)
          {
                    for(i = 0; i< 150; i++)
                    {
                              _nop_();
                              _nop_();
                              _nop_();
                              _nop_();
                    }
          }
}

//5us级延时程序
void delay_us()
{
          unint8 i;
          i--;
          i--;
          i--;
          i--;
          i--;
          i--;
}
void write_com(uchar com)
{
 lcdrs=0;
 P0=com;
 delay_ms(5);
 lcden=1;
 delay_ms(5);
 lcden=0;
}
void write_date(uchar date)
{
 lcdrs=1;
 P0=date;
 delay_ms(5);
 lcden=1;
 delay_ms(5);
 lcden=0;
}
void init()
{
 dula=0;
 wela=0;
 lcden=0;
 write_com(0x38);
 write_com(0x0e);
 write_com(0x06);
 write_com(0x01);
 write_com(0x80+0x10);
}
char receive()
{    
          unint8 i;
          ST=0;
          com_data=0;
          for(i=0;i<=7;i++)    
         {
                    respond=2;
                    while((!TRH)&&respond++);
                    delay_us();
                    delay_us();
                    delay_us();
                    if(TRH)
                    {
                              temp=1;
                              respond=2;
                              while((TRH)&&respond++); 
                    } 
                    else
                              temp=0;
                    com_data<<=1;
                    com_data|=temp;    
          }
          return(com_data); 
}
void read_TRH()
{
         //主机拉低18ms 
         TRH=0;
         delay_ms(18);
         TRH=1;
         //DATA总线由上拉电阻拉高主机延时20us
         delay_us();
         delay_us();
         delay_us();
         delay_us();
         TRH=1;
         //判断DHT11是否有低电平响应信号如不响应则跳出,响应则向下运行   
         if(!TRH)   
         {
                respond=2;
                //判断DHT11发出 80us 的低电平响应信号是否结束 
                while((!TRH)&& respond++);
                respond=2;
                //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
                while(TRH && respond++);
                //数据接收状态  
                RH_temp = receive();
                RL_temp = receive();
                TH_temp = receive();
                TL_temp = receive();
                CK_temp = receive();
                TRH=1;ST=1;     
                //数据校验 
                untemp=(RH_temp+RL_temp+TH_temp+TL_temp);
                if(untemp==CK_temp)
                {
                         RH_data = RH_temp;
                         RL_data = RL_temp;
                         TH_data = TH_temp; 
                         TL_data = TL_temp;
                         CK_data = CK_temp;
                }
        }
         str1[0] = table[RH_data/10]; 
         str1[1] = table[RH_data%10];
         str1[3] = table[RL_data/10]; 
         str2[0] =table[TH_data/10];
         str2[1] =table[TH_data%10];
         str2[3] = table[TL_data/10]; 
}
//主函数
void main()
{
     init();
  dula=0;
  wela=0; 
     delay_us();
          while(1)
          {
              read_TRH();
     delay_ms(100);
     write_com(0x80);
   for(num=0;num<=3;num++){
    write_date(str1[num]);
    delay_ms(5);
   }
   write_com(0x80+0x40);
   for(num=0;num<=3;num++){
    write_date(str2[num]);
    delay_ms(5);
   }
   write_date('C');
         }     
}
 


你可能感兴趣的:(c,Date,table,delay)