標(biāo)題: 基于51單片機(jī)和溫濕度傳感器SH11的仿真實(shí)驗(yàn)程序 [打印本頁(yè)]

作者: 1737857606    時(shí)間: 2021-4-2 16:06
標(biāo)題: 基于51單片機(jī)和溫濕度傳感器SH11的仿真實(shí)驗(yàn)程序
基于51單片機(jī)和溫濕度傳感器SH11的仿真實(shí)驗(yàn)實(shí)驗(yàn)元件:
51單片機(jī)、SH11傳感器、LCD1602
實(shí)驗(yàn)平臺(tái):
protues8.6、keiil4
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  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設(shè)置 START****************************************
  14. #define LCD_DB        P0
  15.         sbit         LCD_RS=P2^0;   //P2^0是p2.0的意思;LCD_RS與P2.0等效起來(lái),對(duì)LCD_RS 讀寫,就是對(duì)P2.0讀寫 好處在于LCD_RS含義直接明了,寫程序多了就會(huì)知道有必要de
  16.         sbit         LCD_RW=P2^1;   //P2^1是p2.1的意思
  17.         sbit         LCD_E=P2^2;    //P2^2是p2.2的意思

  18. /******定義函數(shù)****************/
  19. #define uchar unsigned char
  20. #define uint unsigned int
  21. void LCD_init(void);                          //初始化函數(shù)
  22. void LCD_write_command(uchar command);        //寫指令函數(shù)
  23. void LCD_write_data(uchar dat);               //寫數(shù)據(jù)函數(shù)
  24. void LCD_disp_char(uchar x,uchar y,uchar dat);//在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-15),y(1-2)
  25. void LCD_disp_str(uchar x,uchar y,uchar *str); //LCD1602顯示字符串函數(shù)
  26. void delay_n10us(uint n);                     //延時(shí)函數(shù)


  27. /*--------------------------------------
  28. ;模塊名稱:LCD_init();
  29. ;功    能:初始化LCD1602
  30. ;占用資源:--
  31. ;參數(shù)說(shuō)明:--
  32. ;創(chuàng)建日期:2008.08.15
  33. ;版    本:FV1.0(函數(shù)版本Function Version)
  34. ;修改日期:--
  35. ;修改說(shuō)明:--
  36. ;-------------------------------------*/
  37. void LCD_init(void)
  38. {
  39. delay_n10us(10);
  40. LCD_write_command(0x38);//設(shè)置8位格式,2行,5x7
  41. delay_n10us(10);
  42. LCD_write_command(0x0c);//整體顯示,關(guān)光標(biāo),不閃爍
  43. delay_n10us(10);
  44. LCD_write_command(0x06);//設(shè)定輸入方式,增量不移位
  45. delay_n10us(10);
  46. LCD_write_command(0x01);//清除屏幕顯示
  47. delay_n10us(100);       //延時(shí)清屏,延時(shí)函數(shù),延時(shí)約n個(gè)10us
  48. }


  49. /*--------------------------------------
  50. ;模塊名稱:LCD_write_command();
  51. ;功    能:LCD1602寫指令函數(shù)
  52. ;占用資源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
  53. ;參數(shù)說(shuō)明:dat為寫命令參數(shù)
  54. ;創(chuàng)建日期:2008.08.15
  55. ;版    本:FV1.0(函數(shù)版本Function Version)
  56. ;修改日期:--
  57. ;修改說(shuō)明:--
  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);  //實(shí)踐證明,我的LCD1602上,用for循環(huán)1次就能完成普通寫指令。
  67. LCD_E=0;
  68. delay_n10us(10);  //實(shí)踐證明,我的LCD1602上,用for循環(huán)1次就能完成普通寫指令。
  69. }


  70. /*--------------------------------------
  71. ;模塊名稱:LCD_write_data();
  72. ;功    能:LCD1602寫數(shù)據(jù)函數(shù)
  73. ;占用資源: P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
  74. ;參數(shù)說(shuō)明:dat為寫數(shù)據(jù)參數(shù)
  75. ;創(chuàng)建日期:2008.08.15
  76. ;版    本:FV1.0(函數(shù)版本Function Version)
  77. ;修改日期:--
  78. ;修改說(shuō)明:--
  79. ;-------------------------------------*/
  80. void LCD_write_data(uchar dat)
  81. {
  82. delay_n10us(10);
  83. LCD_RS=1;          //數(shù)據(jù)
  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顯示一個(gè)字符函數(shù),在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-15),y(1-2)。
  94. ;占用資源:--
  95. ;參數(shù)說(shuō)明:X為1602的列值(取值范圍是0-15),y為1602的行值(取值范圍是1-2),dat為所要顯示字符對(duì)應(yīng)的地址參數(shù)。
  96. ;創(chuàng)建日期:2008.08.15
  97. ;版    本:FV1.0(函數(shù)版本Function Version)
  98. ;修改日期:--
  99. ;修改說(shuō)明:--
  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顯示字符串函數(shù),在某個(gè)屏幕起始位置{X(0-15),y(1-2)}上顯示一個(gè)字符串。
  114. ;占用資源:--
  115. ;參數(shù)說(shuō)明:X為1602的列值(取值范圍是0-15),y為1602的行值(取值范圍是1-2),str為所要顯示字符串對(duì)應(yīng)的指針參數(shù)。
  116. ;創(chuàng)建日期:2008.08.16
  117. ;版    本:FV1.0(函數(shù)版本Function Version)
  118. ;修改日期:--
  119. ;修改說(shuō)明:--
  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. ;功    能:延時(shí)函數(shù),延時(shí)約n個(gè)10us
  138. ;占用資源:--
  139. ;參數(shù)說(shuō)明:--
  140. ;創(chuàng)建日期:2008.08.15
  141. ;版    本:FV1.1(函數(shù)版本Function Version)
  142. ;修改日期:2008.08.26
  143. ;修改說(shuō)明:修改為較精確的延時(shí)函數(shù),"_nop_()"延時(shí)1us@12M晶振
  144. ;-------------------------------------*/
  145. void delay_n10us(uint n)  //延時(shí)n個(gè)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設(shè)置 END****************************************


  154. //*********************第二部分DHT90設(shè)置   START****************************************
  155. sbit SCK  = P2^6;      //定義通訊時(shí)鐘端口
  156. sbit DATA = P2^7;      //定義通訊數(shù)據(jù)端口

  157. typedef union  
  158. { unsigned int i;      //定義了兩個(gè)共用體
  159.   float f;
  160. } value;

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


  162. #define noACK 0             //用于判斷是否結(jié)束通訊
  163. #define ACK   1             //結(jié)束數(shù)據(jù)傳輸
  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. /****************定義函數(shù)****************/
  171. void s_transstart(void);               //啟動(dòng)傳輸函數(shù)
  172. void s_connectionreset(void);          //連接復(fù)位函數(shù)
  173. char s_write_byte(unsigned char value);//DHT90寫函數(shù)
  174. char s_read_byte(unsigned char ack);   //DHT90讀函數(shù)
  175. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//測(cè)量溫濕度函數(shù)
  176. void calc_dht90(float *p_humidity ,float *p_temperature);//溫濕度補(bǔ)償



  177. /*--------------------------------------
  178. ;模塊名稱:s_transstart();
  179. ;功    能:啟動(dòng)傳輸函數(shù)
  180. ;占用資源:--
  181. ;參數(shù)說(shuō)明:--
  182. ;創(chuàng)建日期:2008.08.15
  183. ;版    本:FV1.0(函數(shù)版本Function Version)
  184. ;修改日期:--
  185. ;修改說(shuō)明:--
  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. ;功    能:連接復(fù)位函數(shù)
  211. ;占用資源:--
  212. ;參數(shù)說(shuō)明:--
  213. ;創(chuàng)建日期:2008.08.15
  214. ;版    本:FV1.0(函數(shù)版本Function Version)
  215. ;修改日期:--
  216. ;修改說(shuō)明:--
  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寫函數(shù)
  237. ;占用資源:--
  238. ;參數(shù)說(shuō)明:--
  239. ;創(chuàng)建日期:2008.08.15
  240. ;版    本:FV1.0(函數(shù)版本Function Version)
  241. ;修改日期:--
  242. ;修改說(shuō)明:--
  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個(gè)上升沿將被DHT90自動(dòng)下拉為低電平。
  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讀函數(shù)
  268. ;占用資源:--
  269. ;參數(shù)說(shuō)明:--
  270. ;創(chuàng)建日期:2008.08.15
  271. ;版    本:FV1.0(函數(shù)版本Function Version)
  272. ;修改日期:--
  273. ;修改說(shuō)明:--
  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;                      //如果是校驗(yàn)(ack==0),讀取完后結(jié)束通訊
  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. ;功    能:測(cè)量溫濕度函數(shù)
  299. ;占用資源:--
  300. ;參數(shù)說(shuō)明:--
  301. ;創(chuàng)建日期:2008.08.15
  302. ;版    本:FV1.0(函數(shù)版本Function Version)
  303. ;修改日期:--
  304. ;修改說(shuō)明:--
  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. ;功    能:溫濕度補(bǔ)償函數(shù)
  327. ;占用資源:--
  328. ;參數(shù)說(shuō)明:--
  329. ;創(chuàng)建日期:2008.08.15
  330. ;版    本:FV1.0(函數(shù)版本Function Version)
  331. ;修改日期:--
  332. ;修改說(shuō)明:--
  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設(shè)置   END****************************************

  359. //*********主函數(shù)*****************
  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. //*********初始化溫度顯示區(qū)*********
  370.         LCD_disp_str(2,1,"TTT.TC");

  371. //*********初始化濕度顯示區(qū)*********
  372.         LCD_disp_str(2,2,"RRR.R%");

  373.         delay_n10us(20000);     //延時(shí)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');          //顯示溫度個(gè)位
  387.                         LCD_disp_char(6,1,(wendu%10)+'0');              //顯示溫度小數(shù)點(diǎn)后第一位

  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');           //顯示濕度個(gè)位
  392.                         LCD_disp_char(6,2,(shidu%10)+'0');               //顯示濕度小數(shù)點(diǎn)后第一位
  393.           }
  394.           //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------      
  395.                   delay_n10us(80000);                                //延時(shí)約0.8s
  396.         }
  397. }
復(fù)制代碼


全部資料51hei下載地址:
基于51單片機(jī)和SH11的仿真實(shí)驗(yàn).zip (24.78 KB, 下載次數(shù): 38)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1