找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1605|回复: 0
收起左侧

基于51单片机和温湿度传感器SH11的仿真实验程序

[复制链接]
ID:900012 发表于 2021-4-2 16:06 | 显示全部楼层 |阅读模式
基于51单片机和温湿度传感器SH11的仿真实验实验元件:
51单片机、SH11传感器、LCD1602
实验平台:
protues8.6、keiil4
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
51hei.png

单片机源程序如下:
  1. /*************定义接口********************
  2.          P0------DB0~DB7  (LCD1602)     
  3.          P2.0------RS      (LCD1602)
  4.          P2.1------RW      (LCD1602)
  5.          P2.2------E       (LCD1602)
  6.                  P2.6------SCK     (DHT90)
  7.                  P2.7------DATA    (DHT90)
  8. *****************************************/

  9. #include <reg52.h>
  10. #include <intrins.h>
  11. #include <math.h>    //Keil library   
  12. #include <stdio.h>   //Keil library

  13. //*********************第一部分LCD1602设置 START****************************************
  14. #define LCD_DB        P0
  15.         sbit         LCD_RS=P2^0;   //P2^0是p2.0的意思;LCD_RS与P2.0等效起来,对LCD_RS 读写,就是对P2.0读写 好处在于LCD_RS含义直接明了,写程序多了就会知道有必要de
  16.         sbit         LCD_RW=P2^1;   //P2^1是p2.1的意思
  17.         sbit         LCD_E=P2^2;    //P2^2是p2.2的意思

  18. /******定义函数****************/
  19. #define uchar unsigned char
  20. #define uint unsigned int
  21. void LCD_init(void);                          //初始化函数
  22. void LCD_write_command(uchar command);        //写指令函数
  23. void LCD_write_data(uchar dat);               //写数据函数
  24. void LCD_disp_char(uchar x,uchar y,uchar dat);//在某个屏幕位置上显示一个字符,X(0-15),y(1-2)
  25. void LCD_disp_str(uchar x,uchar y,uchar *str); //LCD1602显示字符串函数
  26. void delay_n10us(uint n);                     //延时函数


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


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


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


  91. /*--------------------------------------
  92. ;模块名称:LCD_disp_char();
  93. ;功    能:LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
  94. ;占用资源:--
  95. ;参数说明:X为1602的列值(取值范围是0-15),y为1602的行值(取值范围是1-2),dat为所要显示字符对应的地址参数。
  96. ;创建日期:2008.08.15
  97. ;版    本:FV1.0(函数版本Function Version)
  98. ;修改日期:--
  99. ;修改说明:--
  100. ;-------------------------------------*/
  101. void LCD_disp_char(uchar x,uchar y,uchar dat)
  102. {
  103.   uchar address;
  104.   if(y==1)
  105.          address=0x80+x;
  106.   else
  107.          address=0xc0+x;
  108.   LCD_write_command(address);
  109.   LCD_write_data(dat);
  110. }



  111. /*--------------------------------------
  112. ;模块名称:LCD_disp_str();
  113. ;功    能:LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y(1-2)}上显示一个字符串。
  114. ;占用资源:--
  115. ;参数说明:X为1602的列值(取值范围是0-15),y为1602的行值(取值范围是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.26
  143. ;修改说明:修改为较精确的延时函数,"_nop_()"延时1us@12M晶振
  144. ;-------------------------------------*/
  145. void delay_n10us(uint n)  //延时n个10us@12M晶振
  146. {      
  147.         uint i;           
  148.         for(i=n;i>0;i--)   
  149.         {
  150.         _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
  151.                 }
  152. }                                    

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


  154. //*********************第二部分DHT90设置   START****************************************
  155. sbit SCK  = P2^6;      //定义通讯时钟端口
  156. sbit DATA = P2^7;      //定义通讯数据端口

  157. typedef union  
  158. { unsigned int i;      //定义了两个共用体
  159.   float f;
  160. } value;

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


  162. #define noACK 0             //用于判断是否结束通讯
  163. #define ACK   1             //结束数据传输
  164.                             //adr  command  r/w
  165. #define STATUS_REG_W 0x06   //000   0011    0
  166. #define STATUS_REG_R 0x07   //000   0011    1
  167. #define MEASURE_TEMP 0x03   //000   0001    1
  168. #define MEASURE_HUMI 0x05   //000   0010    1
  169. #define RESET        0x1e   //000   1111    0

  170. /****************定义函数****************/
  171. void s_transstart(void);               //启动传输函数
  172. void s_connectionreset(void);          //连接复位函数
  173. char s_write_byte(unsigned char value);//DHT90写函数
  174. char s_read_byte(unsigned char ack);   //DHT90读函数
  175. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数
  176. void calc_dht90(float *p_humidity ,float *p_temperature);//温湿度补偿



  177. /*--------------------------------------
  178. ;模块名称:s_transstart();
  179. ;功    能:启动传输函数
  180. ;占用资源:--
  181. ;参数说明:--
  182. ;创建日期:2008.08.15
  183. ;版    本:FV1.0(函数版本Function Version)
  184. ;修改日期:--
  185. ;修改说明:--
  186. ;-------------------------------------*/  
  187. void s_transstart(void)
  188. // generates a transmission start  
  189. //       _____         ________
  190. // DATA:      |_______|
  191. //           ___     ___
  192. // SCK : ___|   |___|   |______
  193. {   
  194.    DATA=1; SCK=0;                   //Initial state
  195.    _nop_();
  196.    SCK=1;
  197.    _nop_();
  198.    DATA=0;
  199.    _nop_();
  200.    SCK=0;   
  201.    _nop_();_nop_();_nop_();
  202.    SCK=1;
  203.    _nop_();
  204.    DATA=1;        
  205.    _nop_();
  206.    SCK=0;        
  207. }

  208. /*--------------------------------------
  209. ;模块名称:s_connectionreset();
  210. ;功    能:连接复位函数
  211. ;占用资源:--
  212. ;参数说明:--
  213. ;创建日期:2008.08.15
  214. ;版    本:FV1.0(函数版本Function Version)
  215. ;修改日期:--
  216. ;修改说明:--
  217. ;-------------------------------------*/
  218. void s_connectionreset(void)
  219. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
  220. //       _____________________________________________________         ________
  221. // DATA:                                                      |_______|
  222. //          _    _    _    _    _    _    _    _    _        ___     ___
  223. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
  224. {   
  225.   unsigned char i;  
  226.   DATA=1; SCK=0;                    //Initial state
  227.   for(i=0;i<9;i++)                  //9 SCK cycles
  228.   {
  229.     SCK=1;
  230.     SCK=0;
  231.   }
  232.   s_transstart();                   //transmission start
  233. }


  234. /*--------------------------------------
  235. ;模块名称:s_write_byte();
  236. ;功    能:DHT90写函数
  237. ;占用资源:--
  238. ;参数说明:--
  239. ;创建日期:2008.08.15
  240. ;版    本:FV1.0(函数版本Function Version)
  241. ;修改日期:--
  242. ;修改说明:--
  243. ;-------------------------------------*/
  244. char s_write_byte(unsigned char value)
  245. //----------------------------------------------------------------------------------
  246. // writes a byte on the Sensibus and checks the acknowledge  
  247. {  
  248.   unsigned char i,error=0;   
  249.   for (i=0x80;i>0;i/=2)             //shift bit for masking
  250.   {  
  251.     if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS
  252.     else DATA=0;                        
  253.     SCK=1;                          //clk for SENSI-BUS
  254.     _nop_();_nop_();_nop_();        //pulswith approx. 3 us     
  255.     SCK=0;
  256.   }
  257.   DATA=1;                           //release DATA-line
  258.   SCK=1;                            //clk #9 for ack  
  259.   error=DATA;                       //check ack (DATA will be pulled down by DHT90),DATA在第9个上升沿将被DHT90自动下拉为低电平。
  260.   _nop_();_nop_();_nop_();
  261.   SCK=0;
  262.   DATA=1;                           //release DATA-line
  263.   return error;                     //error=1 in case of no acknowledge //返回:0成功,1失败
  264. }


  265. /*--------------------------------------
  266. ;模块名称:s_read_byte();
  267. ;功    能:DHT11读函数
  268. ;占用资源:--
  269. ;参数说明:--
  270. ;创建日期:2008.08.15
  271. ;版    本:FV1.0(函数版本Function Version)
  272. ;修改日期:--
  273. ;修改说明:--
  274. ;-------------------------------------*/
  275. char s_read_byte(unsigned char ack)  
  276. // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"  
  277. {  
  278.   unsigned char i,val=0;
  279.   DATA=1;                           //release DATA-line
  280.   for (i=0x80;i>0;i/=2)             //shift bit for masking
  281.   { SCK=1;                          //clk for SENSI-BUS
  282.     if (DATA) val=(val | i);        //read bit   
  283.         _nop_();_nop_();_nop_();        //pulswith approx. 3 us
  284.     SCK=0;              
  285.   }
  286.   if(ack==1)DATA=0;                 //in case of "ack==1" pull down DATA-Line
  287.   else DATA=1;                      //如果是校验(ack==0),读取完后结束通讯
  288.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us
  289.   SCK=1;                            //clk #9 for ack
  290.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us  
  291.   SCK=0;                 
  292.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us
  293.   DATA=1;                           //release DATA-line
  294.   return val;
  295. }




  296. /*--------------------------------------
  297. ;模块名称:s_measure();
  298. ;功    能:测量温湿度函数
  299. ;占用资源:--
  300. ;参数说明:--
  301. ;创建日期:2008.08.15
  302. ;版    本:FV1.0(函数版本Function Version)
  303. ;修改日期:--
  304. ;修改说明:--
  305. ;-------------------------------------*/
  306. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  307. // makes a measurement (humidity/temperature) with checksum
  308. {  
  309.   unsigned error=0;
  310.   unsigned int i;

  311.   s_transstart();                   //transmission start
  312.   switch(mode){                     //send command to sensor
  313.     case TEMP  : error+=s_write_byte(MEASURE_TEMP); break;
  314.     case HUMI  : error+=s_write_byte(MEASURE_HUMI); break;
  315.     default     : break;   
  316.   }
  317.   for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
  318.   if(DATA) error+=1;                // or timeout (~2 sec.) is reached
  319.   *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
  320.   *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
  321.   *p_checksum =s_read_byte(noACK);  //read checksum
  322.   return error;
  323. }


  324. /*--------------------------------------
  325. ;模块名称:calc_dht90();
  326. ;功    能:温湿度补偿函数
  327. ;占用资源:--
  328. ;参数说明:--
  329. ;创建日期:2008.08.15
  330. ;版    本:FV1.0(函数版本Function Version)
  331. ;修改日期:--
  332. ;修改说明:--
  333. ;-------------------------------------*/
  334. void calc_dht90(float *p_humidity ,float *p_temperature)
  335. // calculates temperature [C] and humidity [%RH]
  336. // input :  humi [Ticks] (12 bit)
  337. //          temp [Ticks] (14 bit)
  338. // output:  humi [%RH]
  339. //          temp [C]
  340. { const float C1=-4.0;              // for 12 Bit
  341.   const float C2=+0.0405;           // for 12 Bit
  342.   const float C3=-0.0000028;        // for 12 Bit
  343.   const float T1=+0.01;             // for 14 Bit @ 5V
  344.   const float T2=+0.00008;           // for 14 Bit @ 5V

  345.   float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit
  346.   float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  347.   float rh_lin;                     // rh_lin:  Humidity linear
  348.   float rh_true;                    // rh_true: Temperature compensated humidity
  349.   float t_C;                        // t_C   :  Temperature [C]

  350.   t_C=t*0.01 - 40;                  //calc. temperature from ticks to [C]
  351.   rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  352.   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin-3;   //calc. temperature compensated humidity [%RH]
  353.   if(rh_true>100)rh_true=100;       //cut if the value is outside of
  354.   if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  355.   *p_temperature=t_C;               //return temperature [C]
  356.   *p_humidity=rh_true;              //return humidity[%RH]
  357. }




  358. //*********************第二部分DHT90设置   END****************************************

  359. //*********主函数*****************
  360. void main(void)
  361. {
  362.                 value humi_val,temp_val;
  363.         unsigned char error,checksum;
  364.         unsigned int wendu,shidu;
  365.         LCD_init();      
  366.         s_connectionreset();
  367.         LCD_disp_str(0,1,"TE");
  368.                 LCD_disp_str(0,2,"RH");


  369. //*********初始化温度显示区*********
  370.         LCD_disp_str(2,1,"TTT.TC");

  371. //*********初始化湿度显示区*********
  372.         LCD_disp_str(2,2,"RRR.R%");

  373.         delay_n10us(20000);     //延时0.2s

  374.         while(1)
  375.         { error=0;
  376.           error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity
  377.           error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature
  378.           if(error!=0) s_connectionreset();                 //in case of an error: connection reset
  379.           else
  380.           { humi_val.f=(float)humi_val.i;                   //converts integer to float
  381.             temp_val.f=(float)temp_val.i;                   //converts integer to float
  382.             calc_dht90(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
  383.                         wendu=10*temp_val.f;
  384.                         LCD_disp_char(2,1,wendu/1000+'0');              //显示温度百位
  385.             LCD_disp_char(3,1,(wendu%1000)/100+'0');        //显示温度十位
  386.             LCD_disp_char(4,1,(wendu%100)/10+'0');          //显示温度个位
  387.                         LCD_disp_char(6,1,(wendu%10)+'0');              //显示温度小数点后第一位

  388.                         shidu=10*humi_val.f;
  389.                         LCD_disp_char(2,2,shidu/1000+'0');               //显示湿度百位
  390.             LCD_disp_char(3,2,(shidu%1000)/100+'0');         //显示湿度十位
  391.             LCD_disp_char(4,2,(shidu%100)/10+'0');           //显示湿度个位
  392.                         LCD_disp_char(6,2,(shidu%10)+'0');               //显示湿度小数点后第一位
  393.           }
  394.           //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------      
  395.                   delay_n10us(80000);                                //延时约0.8s
  396.         }
  397. }
复制代码
51hei.png

全部资料51hei下载地址:
基于51单片机和SH11的仿真实验.zip (24.78 KB, 下载次数: 34)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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