找回密码
 注册

QQ登录

只需一步,快速开始

搜索

51单片机+SHT10湿度传感器Proteus仿真程序 湿度检测仪

[复制链接]
eng 发表于 2021-6-25 02:38:17 | 显示全部楼层 |阅读模式
本文尾部附件有代码和仿真程序以供参考 1.gif
1.png
单片机源程序如下:
  1. #include <reg52.h>
  2. #include <intrins.h>  
  3. #include <math.h>    //Keil library   
  4. #include <stdio.h>   //Keil library  

  5. //*********************第一部分LCD1602设置

  6. //START****************************************
  7. #define LCD_DB        P0
  8. sbit         LCD_RS=P1^0;   //P2^0是p2.0的意思;LCD_RS
  9. sbit         LCD_RW=P1^1;   //P2^1是p2.1的意思
  10. sbit         LCD_E=P1^2;    //P2^2是p2.2的意思

  11. /******定义函数****************/
  12. #define uchar unsigned char
  13. #define uint unsigned int
  14. typedef unsigned long  U32;
  15. typedef signed   long  S32;      /* defined for signed 32-bits integer variable           有符号32位整型变量 */
  16. typedef float          F32;
  17. void LCD_init(void);                          //初始化函数
  18. void LCD_write_command(uchar command);        //写指令函数
  19. void LCD_write_data(uchar dat);               //写数据函数
  20. void LCD_disp_char(uchar x,uchar y,uchar dat);//在某个屏幕位置上显示一个字符,X(0-15),y(1-2)
  21. void LCD_disp_str(uchar x,uchar y,uchar *str); //LCD1602显示字符串函数
  22. void delay_n10us(uint n);                     //延时函数



  23. /*--------------------------------------  
  24. ;模块名称:LCD_init();  
  25. ;功    能:初始化LCD1602  
  26. ;占用资源:--
  27. ;参数说明:--
  28. ;创建日期:2008.08.15  
  29. ;版    本:FV1.0(函数版本Function Version)
  30. ;修改日期:--
  31. ;修改说明:--
  32. ;-------------------------------------*/  
  33. void LCD_init(void)
  34. {
  35.         delay_n10us(10);
  36.         LCD_write_command(0x38);//设置8位格式,2行,5x7
  37.         delay_n10us(10);
  38.         LCD_write_command(0x0c);//整体显示,关光标,不闪烁
  39.         delay_n10us(10);
  40.         LCD_write_command(0x06);//设定输入方式,增量不移位
  41.         delay_n10us(10);
  42.         LCD_write_command(0x01);//清除屏幕显示
  43.         delay_n10us(100);       //延时清屏,延时函数,延时约n个10us
  44. }


  45. /*--------------------------------------  
  46. ;模块名称:LCD_write_command();  
  47. ;功    能:LCD1602写指令函数  
  48. ;占用资源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
  49. ;参数说明:dat为写命令参数
  50. ;创建日期:2008.08.15  
  51. ;版    本:FV1.0(函数版本Function Version)
  52. ;修改日期:--
  53. ;修改说明:--
  54. ;-------------------------------------*/  
  55. void LCD_write_command(uchar dat)
  56. {
  57.         delay_n10us(10);
  58.         LCD_RS=0;         //指令
  59.         LCD_RW=0;         //写入
  60.         LCD_E=1;          //允许
  61.         LCD_DB=dat;
  62.         delay_n10us(10);  //实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
  63.         LCD_E=0;
  64.         delay_n10us(10);  //实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
  65. }


  66. /*--------------------------------------  
  67. ;模块名称:LCD_write_data();  
  68. ;功    能:LCD1602写数据函数  
  69. ;占用资源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
  70. ;参数说明:dat为写数据参数
  71. ;创建日期:2008.08.15  
  72. ;版    本:FV1.0(函数版本Function Version)
  73. ;修改日期:--
  74. ;修改说明:--
  75. ;-------------------------------------*/
  76. void LCD_write_data(uchar dat)
  77. {
  78.         delay_n10us(10);
  79.         LCD_RS=1;          //数据
  80.         LCD_RW=0;          //写入
  81.         LCD_E=1;           //允许
  82.         LCD_DB=dat;
  83.         delay_n10us(10);
  84.         LCD_E=0;
  85.         delay_n10us(10);
  86. }


  87. /*--------------------------------------  
  88. ;模块名称:LCD_disp_char();  
  89. ;功    能:LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符

  90. ,X(0-15),y(1-2)。
  91. ;占用资源:--
  92. ;参数说明:X为1602的列值(取值范围是0-15),y为1602的行值(取值范围

  93. 是1-2),dat为所要显示字符对应的地址参数。
  94. ;创建日期:2008.08.15  
  95. ;版    本:FV1.0(函数版本Function Version)
  96. ;修改日期:--
  97. ;修改说明:--
  98. ;-------------------------------------*/
  99. void LCD_disp_char(uchar x,uchar y,uchar dat)
  100. {
  101.           uchar address;
  102.           if(y==1)
  103.          address=0x80+x;
  104.           else
  105.          address=0xc0+x;
  106.           LCD_write_command(address);
  107.           LCD_write_data(dat);
  108. }



  109. /*--------------------------------------  
  110. ;模块名称:LCD_disp_str();  
  111. ;功    能:LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y

  112. (1-2)}上显示一个字符串。
  113. ;占用资源:--
  114. ;参数说明:X为1602的列值(取值范围是0-15),y为1602的行值(取值范围

  115. 是1-2),str为所要显示字符串对应的指针参数。
  116. ;创建日期:2008.08.16  
  117. ;版    本:FV1.0(函数版本Function Version)
  118. ;修改日期:--
  119. ;修改说明:--  
  120. ;-------------------------------------*/
  121. void LCD_disp_str(uchar x,uchar y,uchar *str)
  122. {
  123.           uchar address;
  124.           if(y==1)
  125.          address=0x80+x;
  126.           else
  127.          address=0xc0+x;
  128.           LCD_write_command(address);
  129.           while(*str!='\0')
  130.           {  
  131.             LCD_write_data(*str);   
  132.             str++;
  133.           }
  134. }


  135. /*--------------------------------------  
  136. ;模块名称:delay_n10us();  
  137. ;功    能:延时函数,延时约n个10us
  138. ;占用资源:--
  139. ;参数说明:--
  140. ;创建日期:2008.08.15  
  141. ;版    本:FV1.1(函数版本Function Version)
  142. ;修改日期:2008.08.16
  143. ;修改说明:修改为较精确的延时函数
  144. ;-------------------------------------*/
  145. void delay_n10us(uint n)
  146. {        
  147.         uint i;            
  148.         for(i=n;i>0;i--)
  149.         {
  150.         _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); //延时10us@12M晶振
  151. }
  152. }                                    

  153. //*********************第一部分LCD1602设置

  154. //END****************************************


  155. //*********************第二部分DHT90设置   

  156. //START****************************************
  157. bit set_temp_up=0;
  158. bit set_temp_down=0;
  159. bit set_humidity_up=0;
  160. bit set_humidity_down=0;


  161. sbit SCK  = P3^2;      //定义通讯时钟端口
  162. sbit DATA = P3^3;      //定义通讯数据端口
  163. sbit D1=P3^4;   //定义温度报警端口
  164. sbit D2=P3^5;           //定义湿度报警端口
  165. sbit D3=P3^6;   //定义温度报警端口
  166. sbit D4=P3^7;           //定义湿度报警端口
  167. sbit key_set=P1^3;//设置功能选择键
  168. sbit key_up=P1^4;//数字键加+
  169. sbit key_down=P1^5;//数字键减-

  170. uchar selectnum=0,downnum=0,checknum;
  171. uchar value_shi,value_ge,downnum_shi,downnum_ge;
  172. uchar  shidu_shi,shidu_ge,wendu_shi,wendu_ge;
  173. sbit PWMZ = P2^0;      //定义调速端口
  174. sbit PWMF = P2^1;      //定义调速端口
  175. sbit PWMZ2 = P2^3;      //定义调速端口
  176. sbit PWMF2 = P2^4;      //定义调速端口
  177. sbit Alarm = P2^5;
  178. bit temp_alarm_flag=1;
  179. bit rh_alarm_flag=1;
  180. unsigned char CYCLE;  //定义周期 该数字X基准定时时间 如果是10 则周期是10 x 0.1ms
  181. unsigned char PWM_ON ;//定义高电平时间
  182. uchar flag;
  183. unsigned char CYCLE2;  //定义周期 该数字X基准定时时间 如果是10 则周期是10 x 0.1ms
  184. unsigned char PWM_ON2 ;//定义高电平时间
  185. uchar flag2;
  186. uchar temp_uplimit,temp_lowlimit,humidity_uplimit,humidity_lowlimit;
  187. unsigned int Alarm_temp_up=260,Alarm_temp_low=240,Alarm_humidity_up=700,Alarm_humidity_low=500;
  188. unsigned int wendu,shidu;
  189. typedef union   
  190. {
  191.         unsigned int i;      //定义了两个共用体
  192.           float f;  
  193. } value;  

  194. enum {TEMP,HUMI};      //TEMP=0,HUMI=1
  195.   

  196. #define noACK 0             //用于判断是否结束通讯
  197. #define ACK   1             //结束数据传输
  198.                             //adr  command  r/w  
  199. #define STATUS_REG_W 0x06   //000   0011    0  
  200. #define STATUS_REG_R 0x07   //000   0011    1  
  201. #define MEASURE_TEMP 0x03   //000   0001    1  
  202. #define MEASURE_HUMI 0x05   //000   0010    1  
  203. #define RESET        0x1e   //000   1111    0  

  204. /****************定义函数****************/
  205. void s_transstart(void);               //启动传输函数
  206. void s_connectionreset(void);          //连接复位函数
  207. char s_write_byte(unsigned char value);//DHT90写函数
  208. char s_read_byte(unsigned char ack);   //DHT90读函数
  209. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数
  210. void calc_dht90(float *p_humidity ,float *p_temperature);//温湿度补偿
  211. /*--------------------------------------  
  212. ;模块名称:s_transstart();  
  213. ;功    能:启动传输函数
  214. ;占用资源:--
  215. ;参数说明:--
  216. ;创建日期:2008.08.15  
  217. ;版    本:FV1.0(函数版本Function Version)
  218. ;修改日期:--
  219. ;修改说明:--
  220. ;-------------------------------------*/   
  221. void s_transstart(void)  
  222. // generates a transmission start   
  223. //       _____         ________  
  224. // DATA:      |_______|  
  225. //           ___     ___  
  226. // SCK : ___|   |___|   |______  
  227. {   
  228.    DATA=1; SCK=0;                   // 初始化状态
  229.                                     // 对DATA SCK高低电平变化
  230.    _nop_();  
  231.    SCK=1;  
  232.    _nop_();  
  233.    DATA=0;  
  234.    _nop_();  
  235.    SCK=0;   
  236.    _nop_();_nop_();_nop_();  
  237.    SCK=1;  
  238.    _nop_();  
  239.    DATA=1;         
  240.    _nop_();  
  241.    SCK=0;         
  242. }  

  243. /*--------------------------------------  
  244. ;模块名称:s_connectionreset();  
  245. ;功    能:连接复位函数
  246. ;占用资源:--
  247. ;参数说明:--
  248. ;创建日期:2008.08.15  
  249. ;版    本:FV1.0(函数版本Function Version)
  250. ;修改日期:--
  251. ;修改说明:--
  252. ;-------------------------------------*/  
  253. void s_connectionreset(void)  
  254. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart  
  255. //       _____________________________________________________________  
  256. // DATA:                                                      |_______|  
  257. //          _    _    _    _    _    _    _    _    _        ___     ___  
  258. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______  
  259. {   
  260.   unsigned char i;   
  261.   DATA=1; SCK=0;                    //初始信号状态  
  262.   for(i=0;i<9;i++)                  //执行9个时钟信号
  263.   {  
  264.     SCK=1;
  265.     SCK=0;  
  266.   }  
  267.   s_transstart();                   // 调用启动传输函数
  268. }  
  269. void delay1ms(uint z) //这是一个毫秒级别的显示函数
  270. {
  271.         uint x,y;
  272.         for(x=z;x>0;x--)
  273.                 for(y=110;y>0;y--);
  274. }

  275. /*--------------------------------------  
  276. ;模块名称:s_write_byte();  
  277. ;功    能:DHT90写函数
  278. ;占用资源:--
  279. ;参数说明:--
  280. ;创建日期:2008.08.15  
  281. ;版    本:FV1.0(函数版本Function Version)
  282. ;修改日期:--
  283. ;修改说明:--
  284. ;-------------------------------------*/  
  285. char s_write_byte(unsigned char value)  
  286. //----------------------------------------------------------------------------------  
  287. // writes a byte on the Sensibus and checks the acknowledge   
  288. {   
  289.           unsigned char i,error=0;   
  290.           for (i=0x80;i>0;i/=2)             //shift bit for masking  
  291.           {   
  292.             if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS  
  293.             else DATA=0;                          
  294.             SCK=1;                          //clk for SENSI-BUS  
  295.             _nop_();_nop_();_nop_();        //pulswith approx. 5 us      
  296.             SCK=0;  
  297.           }  
  298.           DATA=1;                           //release DATA-line  
  299.           SCK=1;                            //clk #9 for ack   
  300.           error=DATA;                       //check ack (DATA will be pulled down by DHT90),DATA在第9个上升沿将被DHT90自动下拉为低电平。  
  301.           _nop_();_nop_();_nop_();
  302.           SCK=0;
  303.           DATA=1;                           //release DATA-line  
  304.           return error;                     //error=1 in case of no acknowledge //返回:0成功,1失败
  305. }  

  306.   

  307. /*--------------------------------------  
  308. ;模块名称:s_read_byte();  
  309. ;功    能:DHT90读函数
  310. ;占用资源:--
  311. ;参数说明:--
  312. ;创建日期:2008.08.15  
  313. ;版    本:FV1.0(函数版本Function Version)
  314. ;修改日期:--
  315. ;修改说明:--
  316. ;-------------------------------------*/  
  317. char s_read_byte(unsigned char ack)   
  318. // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"   
  319. {   
  320.           unsigned char i,val=0;  
  321.           DATA=1;                           //初始化  
  322.           for (i=0x80;i>0;i/=2)            //开始读取数据
  323.           {
  324.                 SCK=1;                           
  325.             if (DATA) val=(val | i);            
  326.                 _nop_();_nop_();_nop_();         
  327.             SCK=0;               
  328.           }  
  329.           if(ack==1)DATA=0;                 //如果是校验(ack==0),表示还没读取数据完成
  330.           else DATA=1;                      //如果是校验(ack==0),读取完后结束通讯
  331.           _nop_();_nop_();_nop_();          //延时 5 us  
  332.           SCK=1;                            //SCK拉高
  333.           _nop_();_nop_();_nop_();          //延时 5 us   
  334.           SCK=0;                                     //SCK拉低
  335.           _nop_();_nop_();_nop_();          //pulswith approx. 5 us  
  336.           DATA=1;                           //返回初始状态
  337.           return val;  
  338. }  


  339.   

  340. /*--------------------------------------  
  341. ;模块名称:s_measure();  
  342. ;功    能:测量温湿度函数
  343. ;占用资源:--
  344. ;参数说明:--
  345. ;创建日期:2008.08.15  
  346. ;版    本:FV1.0(函数版本Function Version)
  347. ;修改日期:--
  348. ;修改说明:--
  349. ;-------------------------------------*/  
  350. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)  
  351. // makes a measurement (humidity/temperature) with checksum  
  352. {   
  353.           unsigned error=0;  
  354.           unsigned int i;  
  355.   
  356.           s_transstart();                   //启动传输函数   
  357.           switch(mode)
  358.         {                     //发送命令到传感器  
  359.             case TEMP  : error+=s_write_byte(MEASURE_TEMP); break;  
  360.             case HUMI  : error+=s_write_byte(MEASURE_HUMI); break;  
  361.             default     : break;     
  362.           }  
  363.           for (i=0;i<65535;i++) if(DATA==0) break; //直到测量温度 湿度完毕  
  364.           if(DATA) error+=1;                // 判断是否在测量过程中发送错误  
  365.           *(p_value)  =s_read_byte(ACK);    //读取第一个字节


  366.           *(p_value+1)=s_read_byte(ACK);    //读取第2个字节  
  367.           *p_checksum =s_read_byte(noACK);  //读取校验码
  368.           return error;            //返回错误 标志
  369. }  
  370.   

  371. /*--------------------------------------  
  372. ;模块名称:calc_dht90();  
  373. ;功    能:温湿度补偿函数
  374. ;占用资源:--
  375. ;参数说明:--
  376. ;创建日期:2008.08.15  
  377. ;版    本:FV1.0(函数版本Function Version)
  378. ;修改日期:--
  379. ;修改说明:--
  380. ;-------------------------------------*/  
  381. void calc_dht90(float *p_humidity ,float *p_temperature)
  382. {
  383.         const float C1=-4.0;              // 定义C1为浮点数类型
  384.           const float C2=+0.0405;           //  定义C2为浮点数类型
  385.           const float C3=-0.0000028;        //  定义C3为浮点数类型
  386.           const float T1=+0.01;              // 定义T1为浮点数类型
  387.           const float T2=+0.00008;           // 定义T1为浮点数类型

  388.           float rh=*p_humidity;             // 定义rh为浮点数类型
  389.     float t=*p_temperature;           // 定义t为浮点数类型
  390.            float rh_lin;                     // 定义rh_lin为浮点数类型
  391.           float rh_true;                    // 定义rh_true为浮点数类型
  392.           float t_C;                        // 定义t_C为浮点数类型
  393.     t_C=t*0.01 - 40;                  //温度补偿
  394.           rh_lin=C3*rh*rh + C2*rh + C1;     //湿度补偿
  395.           rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;  //计算湿度值
  396.           if(rh_true>100)rh_true=100;       //如果测量到的数据大于100,取值为100
  397.           if(rh_true<0.1)rh_true=0.1;       //确定测量精度为一位小数点

  398.           *p_temperature=t_C;               //返回温度值
  399.           *p_humidity=rh_true;              //返回湿度值
  400. }

  401. void Key_function_scan()
  402. {
  403.           if(key_set==0)
  404.         {
  405.                 delay1ms(10);
  406.                 if(key_set==0)
  407.                 {        
  408.                          TR0 = 0;
  409.                         TR1 = 0;         
  410.         
  411.             LCD_disp_str(0,1,"               ");
  412.                 LCD_disp_str(0,2,"               ");//清屏
  413.                         selectnum++;
  414.                     if(selectnum==1)
  415.                         {
  416.                                   set_temp_up=1;//设置温度上限位
  417.                                   set_temp_down=0;
  418.                                   set_humidity_up=0;
  419.                                   set_humidity_down=0;
  420.                                   LCD_disp_str(0,1," Set_Temp_Hight ");
  421.                       LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0');        
  422.                           LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0');         
  423.                                   LCD_disp_char(7,2,'.');               
  424.                                   LCD_disp_char(8,2,(Alarm_temp_up%10)+'0');               
  425.             
  426.                         }
  427.                         if(selectnum==2)
  428.                         {
  429.                      set_temp_down=1;//设置温度下限位
  430.                                  set_temp_up=0;
  431.                                  set_humidity_up=0;
  432.                                  set_humidity_down=0;
  433.                      LCD_disp_str(0,1," Set_Temp_Low   ");
  434.                     LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0');        //显示温度十位
  435.                     LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0');          //显示温度个位
  436.                                 LCD_disp_char(7,2,'.');               
  437.                                 LCD_disp_char(8,2,(Alarm_temp_low%10)+'0');              //显示温度小数点后第一位
  438.             
  439.                         }
  440.                     if(selectnum==3)
  441.                         {
  442.                                  set_humidity_up=1;//设置湿度上限位
  443.                                  set_humidity_down=0;
  444.                                  set_temp_down=0;
  445.                                  set_temp_up=0;
  446.                          LCD_disp_str(0,1," Set_Hum_Hight  ");
  447.                   LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0');        //显示湿度十位
  448.                           LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0');          //显示湿度个位
  449.                                   LCD_disp_char(7,2,'.');               
  450.                                   LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0');              //显示湿度小数点后第一位

  451.                         }
  452.                         if(selectnum==4)
  453.                         {
  454.                       set_humidity_down=1;//设置湿度下限位
  455.                                   set_humidity_up=0;
  456.                                   set_temp_down=0;
  457.                                   set_temp_up=0;
  458.                            LCD_disp_str(0,1," Set_Hum_Low    ");  
  459.                      LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0');         //显示湿度十位
  460.                     LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0');           //显示湿度个位
  461.                                 LCD_disp_char(7,2,'.');
  462.                                 LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0');               //显示湿度小数点后第一位
  463.                         }

  464.                         if(selectnum==5)//返回测试温度和湿度界面
  465.                         {
  466.                                 LCD_disp_str(0,1,"               ");
  467.                     LCD_disp_str(0,2,"               ");//清屏
  468.                   
  469.                     selectnum=0;
  470.                                 set_humidity_up=0;
  471.                                 set_humidity_down=0;
  472.                                 set_temp_down=0;
  473.                                 set_temp_up=0;
  474.                                 LCD_disp_str(0,1,"Temper:         ");
  475.                     LCD_disp_str(0,2,"Humdity:        ");
  476.                         }
  477.                          while(!key_set);//等待按键释放
  478.                 }
  479.         }
  480. //////////////////////////////////////////
  481.         if(key_up==0)
  482.         {
  483.                 delay1ms(10);
  484.                 if(key_up==0)
  485.                 {                 
  486.                     if(set_temp_up==1)
  487.                         { //温度上限加
  488.                       Alarm_temp_up++;
  489.                            if(Alarm_temp_up==999)Alarm_temp_up=0;
  490.                           LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0');        //显示温度十位
  491.                           LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0');          //显示温度个位
  492.                                   LCD_disp_char(7,2,'.');               
  493.                                   LCD_disp_char(8,2,(Alarm_temp_up%10)+'0');              //显示温度小数点后第一位               
  494.                         }

  495.                     if(set_humidity_up==1)
  496.                         { //湿度上限加
  497.                       Alarm_humidity_up++;
  498.                            if(Alarm_humidity_up==999)Alarm_humidity_up=0;
  499.                           LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0');        //显示湿度十位
  500.                           LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0');          //显示湿度个位
  501.                                   LCD_disp_char(7,2,'.');               
  502.                                   LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0');              //显示湿度小数点后第一位               
  503.                         }

  504.             if(set_temp_down==1)
  505.                         {//温度下限
  506.                           Alarm_temp_low++;
  507.                    if(Alarm_temp_low==999)Alarm_temp_low=0;
  508.                       LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0');        //显示温度十位
  509.                           LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0');          //显示温度个位
  510.                                   LCD_disp_char(7,2,'.');               
  511.                                   LCD_disp_char(8,2,(Alarm_temp_low%10)+'0');              //显示温度小数点后第一位   
  512.               
  513.                         }
  514.                     if(set_humidity_down==1)
  515.                         {//湿度下限
  516.                            Alarm_humidity_low++;
  517.                    if(Alarm_humidity_low==999)Alarm_humidity_low=0;
  518.                       LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0');        //显示湿度十位
  519.                           LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0');          //显示湿度个位
  520.                                   LCD_disp_char(7,2,'.');               
  521.                                   LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0');              //显示湿度小数点后第一位
  522.               
  523.                         }

  524.                     while(!key_up);//等待按键释放
  525.       }
  526.          
  527.         }
  528.         //////////////////////////////////

  529.                 if(key_down==0)
  530.         {
  531.                 delay1ms(10);
  532.                 if(key_down==0)
  533.                 {        
  534.                     if(set_temp_down==1)
  535.                         {//温度下限
  536.                             Alarm_temp_low--;
  537.                      if(Alarm_temp_low==0)Alarm_temp_low=999;
  538.                      
  539.                       LCD_disp_char(5,2,(Alarm_temp_low%1000)/100+'0');        //显示温度十位
  540.                     LCD_disp_char(6,2,(Alarm_temp_low%100)/10+'0');          //显示温度个位
  541.                                 LCD_disp_char(7,2,'.');               
  542.                                 LCD_disp_char(8,2,(Alarm_temp_low%10)+'0');              //显示温度小数点后第一位
  543.                         }
  544.                     if(set_humidity_down==1)
  545.                         {//湿度下限
  546.                              Alarm_humidity_low--;
  547.                      if(Alarm_humidity_low==0)Alarm_humidity_low=999;
  548.                         
  549.                         LCD_disp_char(5,2,(Alarm_humidity_low%1000)/100+'0');         //显示湿度十位
  550.                     LCD_disp_char(6,2,(Alarm_humidity_low%100)/10+'0');           //显示湿度个位
  551.                                 LCD_disp_char(7,2,'.');
  552.                                 LCD_disp_char(8,2,(Alarm_humidity_low%10)+'0');               //显示湿度小数点后第一位
  553.                         }

  554.                         if(set_temp_up==1)
  555.                         {//温度
  556.                             Alarm_temp_up--;
  557.                      if(Alarm_temp_up==0)Alarm_temp_up=999;
  558.                        
  559.                       LCD_disp_char(5,2,(Alarm_temp_up%1000)/100+'0');        //显示温度十位
  560.                     LCD_disp_char(6,2,(Alarm_temp_up%100)/10+'0');          //显示温度个位
  561.                                 LCD_disp_char(7,2,'.');               
  562.                                 LCD_disp_char(8,2,(Alarm_temp_up%10)+'0');              //显示温度小数点后第一位
  563.               
  564.                          }
  565.                          if(set_humidity_up==1)
  566.                         {//湿度
  567.                             Alarm_humidity_up--;
  568.                      if(Alarm_humidity_up==0)Alarm_humidity_up=999;
  569.                         
  570.                         LCD_disp_char(5,2,(Alarm_humidity_up%1000)/100+'0');         //显示湿度十位
  571.                     LCD_disp_char(6,2,(Alarm_humidity_up%100)/10+'0');           //显示湿度个位
  572.                                 LCD_disp_char(7,2,'.');
  573.                                 LCD_disp_char(8,2,(Alarm_humidity_up%10)+'0');               //显示湿度小数点后第一位
  574.                         }
  575.                         while(!key_down);//等待按键释放      
  576.                 }
  577.          
  578.            }

  579. /////////////////////////

  580. }      

  581. void Alarm_Limit()
  582. {

  583.    if(wendu<=Alarm_temp_low)//判断温度值是否超出设定范围,如超出LED亮
  584.          {
  585.                 D1=0;
  586.                 D2=1;
  587.                 temp_alarm_flag=0;
  588.                 TR0=1;
  589.                 flag=0;    //反转
  590.         PWMZ = 0;

  591.          }
  592.          else
  593.          {
  594.       
  595.          }
  596.           if(wendu>=Alarm_temp_up)//判断温度值是否超出设定范围,如超出LED亮
  597.           {
  598.                 D2=0;
  599.                         D1=1;
  600.                 temp_alarm_flag=0;
  601.                 TR0=1;
  602.                 flag=1;    //正转
  603.         PWMF = 0;
  604.           }
  605.          else
  606.          {
  607.       
  608.       
  609.          }
  610.           if(wendu>Alarm_temp_low&&wendu<Alarm_temp_up) //温度在范围内
  611.          {  
  612.              D1=1;
  613.              D2=1;
  614.                  temp_alarm_flag=1;
  615.                  TR0=0;
  616.                  PWMZ = 0;
  617.          PWMF = 0;
  618.          }


  619.     if(shidu<=Alarm_humidity_low)//判断湿度值是否超出设定范围,如超出LED亮
  620.          {
  621.                 D3=0;D4=1;
  622.                 rh_alarm_flag=0;
  623.                 TR1=1;
  624.                 flag2=0;    //反转
  625.         PWMZ2 = 0;
  626.          }
  627.          else
  628.          {
  629.                
  630.          }
  631.            if(shidu>=Alarm_humidity_up)//判断湿度值是否超出设定范围,如超出LED亮
  632.          {
  633.                 D4=0;D3=1;
  634.                 rh_alarm_flag=0;
  635.                 TR1=1;
  636.                 flag2=1;    //正转
  637.         PWMF2 = 0;
  638.          }
  639.          else
  640.          {
  641.                
  642.          }
  643.          if(shidu>Alarm_humidity_low&&shidu<Alarm_humidity_up) //湿度在范围内
  644.          {  
  645.              D3=1;
  646.              D4=1;
  647.                  rh_alarm_flag=1;
  648.                  TR1=0;
  649.                  PWMZ2 = 0;
  650.          PWMF2 = 0;
  651.          }
  652.          
  653.          if(temp_alarm_flag==0||rh_alarm_flag==0)
  654.          {
  655.            Alarm=0;
  656.            delay1ms(10);
  657.            Alarm=1;
  658.          }                    
  659.          else
  660.          {
  661.            Alarm=1;
  662.          }
  663. }


  664. //*********************第二部分DHT90设置   

  665. //END****************************************
  666. void SysInit_two(void)
  667. {
  668. //        TMOD |=0x01;//定时器设置 1ms in 12M crystal
  669.         //TH0=(65536-1000)/256;
  670. //        TL0=(65536-1000)%256;//定时1mS
  671.         //IE= 0x82;  //打开中断
  672.         //TR0=1;
  673.    TMOD=0X11;             //T0 T1都工作在方式1(16位计数器)
  674.         TH0=0x3c;  //50ms
  675.         TL0=0xb0;
  676.         TR0=0;
  677.         ET0=1;

  678.         TH1=0x3C;  //50ms
  679.         TL1=0xB0;
  680.         TR1=0;
  681.         ET1=1;

  682.         EA=1;   
  683. }

  684. //*********主函数*****************

  685. unsigned char                 time_ms1;
  686. value humi_val,temp_val;
  687. unsigned char error,checksum;

  688. void dis()
  689. {
  690. error=0;  
  691.          error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity  测量湿度
  692.          error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature 测量温度  
  693.          if(error!=0) s_connectionreset();                 //in case of an error: connection reset  判断校验是否正确
  694.          else  
  695.          {
  696.                                   humi_val.f=(float)humi_val.i;                   //converts integer to float   湿度转换成浮点数
  697.                     temp_val.f=(float)temp_val.i;                   //converts integer to float   温度转换成浮点数
  698.                     calc_dht90(&humi_val.f,&temp_val.f);            //calculate humidity, temperature          计算出温度 湿度值
  699.                                 wendu=10*temp_val.f;
  700.                

  701.                                  
  702.                     LCD_disp_char(8,1,(wendu%1000)/100+'0');        //显示温度十位
  703.                     LCD_disp_char(9,1,(wendu%100)/10+'0');          //显示温度个位
  704.                                 LCD_disp_str(10,1,".");
  705.                                 LCD_disp_char(11,1,(wendu%10)+'0');              //显示温度小数点后第一位
  706.                                 LCD_disp_char(12,1,0xdf);
  707.                                 LCD_disp_str(13,1,"C");

  708.                                 shidu=10*humi_val.f;

  709.                     LCD_disp_char(8,2,(shidu%1000)/100+'0');         //显示湿度十位
  710.                     LCD_disp_char(9,2,(shidu%100)/10+'0');           //显示湿度个位
  711.                                 LCD_disp_str(10,2,".");
  712.                                 LCD_disp_char(11,2,(shidu%10)+'0');               //显示湿度小数点后第一位
  713.                                 LCD_disp_str(12,2,"%");
  714. ……………………

  715. …………限于本文篇幅 余下代码请下载附件…………
复制代码

湿度检测仪仿真.zip (104 KB, 售价: 2 E币)

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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-27 08:35

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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