找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3291|回复: 3
收起左侧

单片机测温测湿+调用风扇降湿降温+设置温湿上下限+报警

[复制链接]
ID:582867 发表于 2019-7-12 21:13 | 显示全部楼层 |阅读模式
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^1; //模式
  9. sbit k2=P3^0; //加
  10. sbit k3=P3^2; //减

  11. sbit DHT11_DQ_OUT=P2^0;//湿度
  12. sbit bee=P1^5;//蜂鸣器
  13. sbit IRIN=P3^2;

  14. sbit dq=P3^7;//温度
  15. sbit dj=P1^0;//电机
  16. uint8 mode=0,xian;
  17. char temph=50,templ=20;//自己可以改
  18. char humih=80,humil=20;
  19. uint8 temp,humi;
  20. uint8 flag;          //设定报警标志
  21. uint8 a,c,tempvalue;
  22. uint8 code num[10]="0123456789";

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


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

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

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

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


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


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

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

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




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


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

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

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

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

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

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

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

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

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

  430. void baojinpros()        //报警处理
  431. {
  432.         if(tempvalue>=temph||humi>=humih)         //检测温度或者湿度高于设定上限值 降温湿
  433.         {
  434.         bee=~bee;        //降温湿指示灯
  435. dj=~dj;
  436.         }
  437.         if(tempvalue<=templ||humi<=humil)        //检测温度或者湿度低于设定下限值  升温湿
  438.         {
  439.                 bee=~bee;        //升高温湿指示灯
  440.                 dj=~dj;
  441.         }
复制代码

        详细可以看附件,不懂可以咨询。我也萌新互相学习。

设计.7z

24.04 KB, 下载次数: 70, 下载积分: 黑币 -5

这是我一次小制作大神见笑了希望能帮当一些初学者参考

回复

使用道具 举报

ID:582867 发表于 2019-7-12 21:15 | 显示全部楼层
有问题大佬指点小弟。
回复

使用道具 举报

ID:89286 发表于 2019-7-15 10:51 | 显示全部楼层
thanks for sharing
回复

使用道具 举报

ID:880098 发表于 2021-1-24 15:50 | 显示全部楼层
里面只有一个仿真文件和这个网页上面的代码哦,还能再分享点资料吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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