找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3824|回复: 8
收起左侧

51单片机做的恒温恒湿孵化程序 DHT11传感器

[复制链接]
ID:234266 发表于 2017-9-20 08:24 | 显示全部楼层 |阅读模式
如果要将此应用项目移植到开发板上,只需要根据此项目的电路图修改管脚,改成我们开发板对应的管脚上。温湿度传感器使用的是DHT11,所以数据口按照原理图内接。温度传感器接口也需要修改成我们开发板对应的接口
51单片机做的恒温恒湿孵化程序。传感器用的TH11
孵化环境温湿度监控系统设计原理图如下:
0.png

单片机源程序如下:
  1. #include<reg52.h>
  2. #include "intrins.h"
  3. typedef unsigned char uint8;
  4. typedef unsigned int uint16;
  5. sbit rs=P2^6;         // 数据命令选择
  6. sbit rw=P2^5;         //读写选择
  7. sbit e=P2^7;          //使能

  8. sbit k1=P3^3; //模式
  9. sbit k2=P2^1; //加
  10. sbit k3=P2^2; //减

  11. sbit DHT11_DQ_OUT=P3^2;
  12. sbit led1=P3^6;
  13. sbit led2=P3^7;

  14. sbit dq=P2^0;

  15. uint8 mode=0,xian;
  16. char temph=50,templ=20;
  17. char humih=80,humil=20;
  18. uint8 temp,humi;
  19. uint8 flag;          //设定报警标志
  20. uint8 a,c,tempvalue;
  21. uint8 code num[10]="0123456789";

  22. uint8 code str1[]="Temp:";  //温度
  23. uint8 code str2[]="Humi:";  //湿度
  24. uint8 code str3[]="Error";  
  25. uint8 code str4[]="Success    ";
  26. uint8 code str5[]="%RH";
  27. uint8 code str6[]="TempH:";          //设定温度上限显示
  28. uint8 code str7[]="TempL:";          //设定温度下限显示
  29. uint8 code str8[]="HumiH:";           //设定湿度上限显示
  30. uint8 code str9[]="HumiL:";           //设定湿度下限显示


  31. void delay(uint16 i)
  32. {
  33.         while(i--);
  34. }

  35. void delay_ms(uint16 i)
  36. {
  37.         while(i--)
  38.                 delay(90);
  39. }

  40. void wrc(uint8 c)         //写命令
  41. {
  42.         delay(1000);
  43.         rs=0;
  44.         rw=0;
  45.         e=0;
  46.         P0=c;
  47.         e=1;
  48.         delay(10);
  49.         e=0;
  50. }
  51. void wrd(uint8 dat)          //写数据
  52. {
  53.         delay(1000);
  54.         rs=1;
  55.         rw=0;
  56.         e=0;
  57.         P0=dat;
  58.         e=1;
  59.         delay(10);
  60.         e=0;
  61.         rs=0;
  62. }
  63. void lcd_init()           // LCD1602初始化
  64. {
  65.         delay(1000);
  66.         wrc(0x38);
  67.         wrc(0x38);         //功能设置命令,选择8位总线,双行显示  5*7点阵字符
  68.         wrc(0x38);
  69.         wrc(0x06);        //光标和显示模式设置  光标右移  整屏不移动
  70.         wrc(0x0c);        //显示开关控制  开显示  无光标 光标不闪烁
  71.         wrc(0x01);        //清零指令  固定的
  72. }
  73. //复位DHT11
  74. void DHT11_Rst()           
  75. {                 
  76.     DHT11_DQ_OUT=0;         //拉低DQ
  77.     delay_ms(20);            //拉低至少18ms
  78.     DHT11_DQ_OUT=1;         //DQ=1
  79.         delay(3);             //主机拉高20~40us
  80. }

  81. //等待DHT11的回应
  82. //返回1:未检测到DHT11的存在
  83. //返回0:存在
  84. uint8 DHT11_Check()            
  85. {   
  86.         uint8 retry=0;         
  87.     while (DHT11_DQ_OUT&&retry<100)//DHT11会拉低40~50us
  88.         {
  89.                 retry++;
  90.                 _nop_();
  91.         };         
  92.         if(retry>=100)return 1;
  93.         else retry=0;
  94.     while (!DHT11_DQ_OUT&&retry<100)//DHT11拉低后会再次拉高40~50us
  95.         {
  96.                 retry++;
  97.                 _nop_();
  98.         };
  99.         if(retry>=100)return 1;            
  100.         return 0;
  101. }


  102. //DHT11初始化
  103. //返回0:初始化成功,1:失败
  104. uint8 DHT11_Init()
  105. {
  106.         DHT11_Rst();         
  107.         return DHT11_Check();        
  108. }


  109. //从DHT11读取一个位
  110. //返回值:1/0
  111. uint8 DHT11_Read_Bit(void)                          
  112. {
  113.          uint8 retry=0;
  114.         while(DHT11_DQ_OUT&&retry<100)//等待变为低电平 12-14us 开始
  115.         {
  116.                 retry++;
  117.                 _nop_();
  118.         }
  119.         retry=0;
  120.         while((!DHT11_DQ_OUT)&&retry<100)//等待变高电平         26-28us表示0,116-118us表示1
  121.         {
  122.                 retry++;
  123.                 _nop_();
  124.         }
  125.         delay(1);//等待40us
  126.         if(DHT11_DQ_OUT)return 1;
  127.         else return 0;                  
  128. }

  129. //从DHT11读取一个字节
  130. //返回值:读到的数据
  131. uint8 DHT11_Read_Byte(void)   
  132. {        
  133.     uint8 i,dat=0;
  134.         for (i=0;i<8;i++)
  135.         {
  136.                    dat<<=1;
  137.             dat|=DHT11_Read_Bit();
  138.     }                                                   
  139.     return dat;
  140. }

  141. //从DHT11读取一次数据
  142. //temp:温度值(范围:0~50°)
  143. //humi:湿度值(范围:20%~90%)
  144. //返回值:0,正常;1,读取失败
  145. uint8 DHT11_Read_Data(uint8 *temp,uint8 *humi)   
  146. {        
  147.          uint8 buf[5];
  148.         uint8 i;
  149.         DHT11_Rst();
  150.         if(DHT11_Check()==0)
  151.         {
  152.                 for(i=0;i<5;i++)//读取40位数据
  153.                 {
  154.                         buf[i]=DHT11_Read_Byte();
  155.                 }
  156.                 if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
  157.                 {
  158.                         *humi=buf[0];
  159.                         *temp=buf[2];
  160.                 }
  161.                
  162.         }else return 1;
  163.         return 0;            
  164. }




  165. void ds18b20init()                //18b20的初始化
  166. {
  167.         dq=1;
  168.         delay(1);
  169.         dq=0;
  170.         delay(80);
  171.         dq=1;
  172.         delay(5);
  173.         dq=0;
  174.         delay(20);
  175.         dq=1;
  176.         delay(35);
  177. }
  178. void ds18b20wr(uint8 dat)          //18b20写数据
  179. {
  180.         uint8 i;
  181.         for(i=0;i<8;i++)
  182.         {
  183.                 dq=0;
  184.                 dq=dat&0x01;
  185.                 dat>>=1;
  186.                 delay(8);//在时序上只有这一块对时序要求最准确,他的时间必须大于15us
  187.                 dq=1;
  188.                 delay(1);
  189.         }        
  190. }
  191. uint8 ds18b20rd()                  //18b20读数据
  192. {
  193.         uint8 value,i;
  194.         for(i=0;i<8;i++)        
  195.         {
  196.                 dq=0;
  197.                 value>>=1;
  198.                 dq=1;
  199.                 if(dq==1)value|=0x80;
  200.                 delay(8);//在这一块也对时间要求特别准确,整段程序必须大于60us               
  201.         }
  202.         return value;
  203. }
  204. uint8 readtemp()                          //读取温度内需要复位的
  205. {
  206.         uint8 b;
  207.         ds18b20init();                //初始化
  208.         ds18b20wr(0xcc);   //发送忽略ROM指令
  209.         ds18b20wr(0x44);   //发送温度转换指令
  210.         delay(100);
  211.         ds18b20init();           //初始化
  212.         ds18b20wr(0xcc);   //发送忽略ROM指令
  213.         ds18b20wr(0xbe);   //发读暂存器指令
  214.         a=ds18b20rd();         //温度的低八位
  215.         b=ds18b20rd();         //温度的高八位
  216.         b<<=4;                         //ssss s***;s为标志位s=0表示温度值为正数,s=1温度值为负数
  217.         c=b&0x80;                //温度正负标志位确认
  218.         b+=(a&0xf0)>>4;
  219.         a=a&0x0f;          //温度的小数部分
  220.         return b;
  221. }


  222. void key_pros()  //按键处理函数
  223. {
  224.         if(k1==0)
  225.         {
  226.                 delay(1000);
  227.                 if(k1==0)
  228.                 {
  229.                         mode++;
  230.                         if(mode==5)mode=0;
  231.                         wrc(0x01);
  232.                 }
  233.                 while(!k1);
  234.         }
  235.         if(mode==1)                  //对温度上限设定
  236.         {
  237.                 if(k2==0)                  //加
  238.                 {
  239.                         delay(1000);
  240.                         if(k2==0)
  241.                         {
  242.                                 temph++;
  243.                                 if(temph>=80)temph=80;
  244.                         }
  245.                         while(!k2);
  246.                 }
  247.                 if(k3==0)           //减
  248.                 {
  249.                         delay(1000);
  250.                         if(k3==0)
  251.                         {
  252.                                 temph--;
  253.                                 if(temph<=0)temph=0;
  254.                         }
  255.                         while(!k3);
  256.                 }
  257.         }
  258.         if(mode==2)                  //对温度下限设定
  259.         {
  260.                 if(k2==0)                  //加
  261.                 {
  262.                         delay(1000);
  263.                         if(k2==0)
  264.                         {
  265.                                 templ++;
  266.                                 if(templ>=80)templ=80;
  267.                         }
  268.                         while(!k2);
  269.                 }
  270.                 if(k3==0)           //减
  271.                 {
  272.                         delay(1000);
  273.                         if(k3==0)
  274.                         {
  275.                                 templ--;
  276.                                 if(templ<=0)templ=0;
  277.                         }
  278.                         while(!k3);
  279.                 }
  280.         }
  281.         if(mode==3)                  //对湿度上限设定
  282.         {
  283.                 if(k2==0)                  //加
  284.                 {
  285.                         delay(1000);
  286.                         if(k2==0)
  287.                         {
  288.                                 humih++;
  289.                                 if(humih>=80)humih=80;
  290.                         }
  291.                         while(!k2);
  292.                 }
  293.                 if(k3==0)           //减
  294.                 {
  295.                         delay(1000);
  296.                         if(k3==0)
  297.                         {
  298.                                 humih--;
  299.                                 if(humih<=0)humih=0;
  300.                         }
  301.                         while(!k3);
  302.                 }
  303.         }
  304.         if(mode==4)                  //对湿度下限设定
  305.         {
  306.                 if(k2==0)                  //加
  307.                 {
  308.                         delay(1000);
  309.                         if(k2==0)
  310.                         {
  311.                                 humil++;
  312.                                 if(humil>=80)humil=80;
  313.                         }
  314.                         while(!k2);
  315.                 }
  316.                 if(k3==0)           //减
  317.                 {
  318.                         delay(1000);
  319.                         if(k3==0)
  320.                         {
  321.                                 humil--;
  322.                                 if(humil<=0)humil=0;
  323.                         }
  324.                         while(!k3);
  325.                 }
  326.         }
  327. }

  328. void lcd_init_display()           //LCD初始化显示
  329. {
  330.         uint8 i;
  331.         for(i=0;i<5;i++)
  332.         {
  333.                 wrc(0x80+i);
  334.                 wrd(str1[i]);        
  335.         }

  336.         for(i=0;i<5;i++)
  337.         {
  338.                 wrc(0xc0+i);
  339.                 wrd(str2[i]);        
  340.         }
  341. }

  342. void data_pros()        //数据处理函数
  343. {
  344.         uint8 i;              
  345.         uint8 temp_buf[2],humi_buf[2];
  346.         uint8 temphbuf[2],templbuf[2],humihbuf[2],humilbuf[2];
  347.         float dio;
  348.         uint16 k;

  349.         tempvalue=readtemp();
  350.         DHT11_Read_Data(&temp,&humi);
  351.         temp_buf[0]=temp/10+0x30;        
  352.         temp_buf[1]=temp%10+0x30;

  353.         humi_buf[0]=humi/10+0x30;        
  354.         humi_buf[1]=humi%10+0x30;
  355.         
  356.         dio=a*0.0625;
  357.         k=dio*10000;//取小数点后两位有效数字

  358.         temphbuf[0]=temph/10+0x30;
  359.         temphbuf[1]=temph%10+0x30;
  360.         templbuf[0]=templ/10+0x30;
  361.         templbuf[1]=templ%10+0x30;

  362.         humihbuf[0]=humih/10+0x30;
  363.         humihbuf[1]=humih%10+0x30;
  364.         humilbuf[0]=humil/10+0x30;
  365.         humilbuf[1]=humil%10+0x30;

  366.         if(mode==0)
  367.         {
  368.                 lcd_init_display();
  369.                 wrc(0x85);
  370.                 wrd(num[tempvalue%100/10]);
  371.                 wrd(num[tempvalue%100%10]);
  372.                 wrd('.');
  373.                 wrd(num[k/1000]);
  374.                 wrd(0xdf);
  375.                 wrd('C');
  376.         
  377.                 for(i=0;i<2;i++)
  378.                 {
  379.                         wrc(0Xc5+i);
  380.                         wrd(humi_buf[i]);                  
  381.                 }        
  382.                 for(i=0;i<3;i++)
  383.                 {
  384.                         wrc(0Xc7+i);
  385.                         wrd(str5[i]);                  
  386.                 }        
  387.         }
  388.         if(mode==1)                          //温度上限显示
  389.         {
  390.                 wrc(0x80);
  391.                 for(i=0;i<6;i++)
  392.                 {
  393.                         wrd(str6[i]);                  
  394.                 }
  395.                 wrd(temphbuf[0]);
  396.                 wrd(temphbuf[1]);                        
  397.         }
  398.         if(mode==2)                          //温度下限显示
  399.         {
  400.                 wrc(0x80);
  401.                 for(i=0;i<6;i++)
  402.                 {
  403.                         wrd(str7[i]);                  
  404.                 }
  405.                 wrd(templbuf[0]);
  406.                 wrd(templbuf[1]);                        
  407.         }
  408.         if(mode==3)                          //湿度上限显示
  409.         {
  410.                 wrc(0x80);
  411.                 for(i=0;i<6;i++)
  412.                 {
  413.                         wrd(str8[i]);                  
  414.                 }
  415.                 wrd(humihbuf[0]);
  416.                 wrd(humihbuf[1]);                        
  417.         }
  418.         if(mode==4)                          //湿度下限显示
  419.         {
  420.                 wrc(0x80);
  421.                 for(i=0;i<6;i++)
  422.                 {
  423.                         wrd(str9[i]);                  
  424.                 }
  425.                 wrd(humilbuf[0]);
  426.                 wrd(humilbuf[1]);                        
  427.     }
  428. }

  429. void baojinpros()    //报警处理
  430. {
  431.     if(tempvalue>=temph||humi>=humih)     //检测温度或者湿度高于设定上限值 降温湿
  432.     {
  433.         led1=1;        //降温湿指示灯
  434.         led2=0;
  435.     }
  436.     if(tempvalue<=templ||humi<=humil)    //检测温度或者湿度低于设定下限值  升温湿
  437.     {
  438.         led1=0;
  439.         led2=1;       //升高温湿指示灯
  440.     }
  441.     if((tempvalue>templ&&tempvalue<temph)&&(humi>humil&&humi<humih))
  442.     {
  443.         led1=0;
  444.         led2=0;   
  445.     }
  446. }

  447. void main()
  448. {
  449.     uint8 i=0;
  450.     led1=0;
  451.     led2=0;
  452.     lcd_init();
  453.     while(DHT11_Init())    //检测DHT11是否纯在
  454.     {
  455.         for(i=0;i<5;i++)
  456.         {
  457.             wrc(0x80+i);
  458.             wrd(str3[i]);   
  459.         }            
  460.     }
  461.     wrc(0x01);
  462.     lcd_init_display();       //LCD初始化显示
  463.     i=0;
  464.     while(1)
  465.     {   
  466.         i++;
  467.         key_pros();
  468.         baojinpros();    //报警处理
  469.         if(i==15)
  470.         {
  471.             i=0;
  472.             data_pros();       //读取一次DHT11数据最少要大于100ms
  473. ……………………

  474. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
0.png
所有资料51hei提供下载:
孵化环境温湿度监控系统设计.zip (76.69 KB, 下载次数: 112)
回复

使用道具 举报

ID:229762 发表于 2017-10-14 00:13 | 显示全部楼层
增减温度以1℃为最小单位。不知道怎么改。
回复

使用道具 举报

ID:445779 发表于 2018-12-15 16:28 | 显示全部楼层
请问有仿真和原理图文件吗?仿真lcd一直显示error
回复

使用道具 举报

ID:446844 发表于 2018-12-15 16:48 | 显示全部楼层
给很力!
回复

使用道具 举报

ID:445779 发表于 2018-12-18 12:24 | 显示全部楼层

请问有仿真吗,仿真lcd一直显示error,不知道怎么回事
回复

使用道具 举报

ID:445779 发表于 2018-12-18 12:38 | 显示全部楼层

请问lcd显示error怎么回事,有仿真吗
回复

使用道具 举报

ID:79874 发表于 2019-3-13 07:18 来自手机 | 显示全部楼层
能提供仿真和PCB
回复

使用道具 举报

ID:79874 发表于 2019-3-13 07:18 来自手机 | 显示全部楼层
能提供仿真和PCB源文件吗
回复

使用道具 举报

ID:438321 发表于 2019-5-21 10:41 | 显示全部楼层
哥哥,可以更全一下吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表