找回密码
 立即注册

QQ登录

只需一步,快速开始

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

SHT11温湿度传感器仿真和源码

[复制链接]
ID:350702 发表于 2018-6-13 09:28 | 显示全部楼层 |阅读模式
SHT11仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png 0.png

单片机源程序如下:
  1. #include <reg52.h>  //头文件
  2. #include <intrins.h>
  3. #include <stdio.h> //
  4. #include <math.h>    //Keil library  

  5. //**************************************

  6. #define uchar unsigned char //定义一下方便使用
  7. #define uint  unsigned int //定义一下方便使用
  8. #define ulong unsigned long //定义一下方便使用


  9. #define TEMP_ML 0x03   //000   0001    1 温度命令
  10. #define HUMI_ML 0x05   //000   0010    1 温度命令
  11. unsigned char error  ;//全局错误变量
  12. unsigned char ack  ;//全局应答变量
  13. //float temp_zi  ;//全局应答变量
  14. //float humi_zi  ;//全局应答变量
  15. unsigned char temp_h  ;//全局应答变量
  16. unsigned char temp_LL  ;//全局应答变量

  17. unsigned  int  xianzhi_t=0;//温度显值
  18. unsigned  int  xianzhi_h=0;//湿度显值


  19. uchar setzhi_h,setzhi_l;
  20. bit   setbz_h,setbz_l,setkey;


  21. sbit DATA =P2^6;//数据
  22. sbit SCK=P2^7;//时钟
  23. sbit hot =P2^0;// 加热
  24. sbit motor =P2^1;// 电机
  25. sbit speek =P2^2;// 声音
  26. sbit set =P0^0;//  设置
  27. sbit setup =P0^1;//  设置+
  28. sbit setdown =P0^2;//  设置-

  29. sbit        gwei =P3^4;//个位
  30. sbit        swei =P3^3;//十位
  31. sbit        bwei =P3^2;//百位
  32. sbit        qwei =P3^1;//千位
  33. unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//共阳



  34. //*******************************基本驱动程



  35. /////////////////
  36. //////////////////////


  37. char read() //读一个字节 返回应答信号
  38. //----------------------------------------------------------------------------------
  39. // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
  40. {
  41.   unsigned char i,val=0;
  42.   temp_LL=0;
  43.   temp_h=0;
  44.   DATA=1;                           //释放数据总线
  45.   for (i=0x80;i>0;i/=2)             //位移8位
  46.   { SCK=1;                          //上升沿读入
  47.     if (DATA) val=(val | i);        //确定值  
  48.     SCK=0;                                          
  49.   }
  50.    DATA=0;                        //读应答信号,有应答为1,为应答为0 通过CPU下拉为应答
  51.   SCK=1;                            //第9个脉冲
  52.    _nop_(); _nop_(); _nop_();          //pulswith approx. 5 us
  53.   SCK=0;                                                   
  54.   DATA=1;                           //释放数据总线
  55.   temp_h=val;
  56.   val=0;

  57. ////低8位/////////////////////////////

  58.     DATA=1;                           //释放数据总线
  59.   for (i=0x80;i>0;i/=2)             //位移8位
  60.   { SCK=1;                          //上升沿读入
  61.     if (DATA) val=(val | i);        //确定值  
  62.     SCK=0;                                          
  63.   }
  64.    DATA=1;//0;                        //不需要应答 通过CPU下拉为应答
  65.   SCK=1;                            //第9个脉冲
  66.    _nop_(); _nop_(); _nop_();          //pulswith approx. 5 us
  67.   SCK=0;                                                   
  68.   DATA=1;                           //释放数据总线
  69.   temp_LL=val;

  70. }
  71. ////////////


  72.   char write(unsigned char value) //写一个字节 返回应答信号
  73. //---------------------------------------------------------
  74. {
  75.   unsigned char i ;
  76.    ack=0;  
  77.   for (i=0x80;i>0;i/=2)             //释放数据总线
  78.   { if (i & value) DATA=1;          //写入值
  79.     else DATA=0;                        
  80.     SCK=1;                          //上升沿写入
  81.      _nop_(); _nop_(); _nop_();        //延时        
  82.     SCK=0;
  83.   }
  84.   DATA=1;                           //释放数据总线
  85.   SCK=1;                            //第9个脉冲
  86.   if  (DATA==1) ack=1;
  87.                          //读应答信号
  88.   SCK=0;        
  89.   return ack;                     //error=1 表示没有应答
  90. }
  91. ////////

  92. void start_sht11(void)  //启动
  93. //--------------------------------------------------------
  94. {  
  95.    DATA=1; SCK=0;                   //数据为1,SCK=0
  96.     _nop_();
  97.    SCK=1;                          //第一个脉冲
  98.     _nop_();
  99.    DATA=0;                         //数据跌落
  100.     _nop_ ();
  101.    SCK=0;                         //完成一个脉冲
  102.     _nop_(); _nop_(); _nop_();
  103.    SCK=1;                         //再一个脉冲
  104.     _nop_();
  105.    DATA=1;                        //数据变为1                  
  106.     _nop_();
  107.    SCK=0;                         //完成该脉冲                  
  108. }


  109. //////////////////////////////////
  110. void sht_rest(void)  //复位
  111.    
  112. {  
  113.   unsigned char i;
  114.   DATA=1; SCK=0;                    //数据为1 时钟为0
  115.   for(i=0;i<9;i++)                  //9 个脉冲为 复位
  116.   { SCK=1;
  117.     SCK=0;
  118.   }
  119.   start_sht11();                   //启动
  120. }

  121. ////////////////////////////////


  122.                           //测量温度或者是温度,返回校验值
  123.   text_a(unsigned char ml)
  124.         {
  125.                   unsigned int i;

  126.                   start_sht11();                   //启动
  127.                   write(ml);//写入测温度
  128.                  if (ack==1)
  129.                     {
  130.                             sht_rest() ;//复位
  131.                            write(ml);//写入测温度
  132.              }
  133.       
  134.           //判断是否处于忙

  135.         //         DATA=1;//释放数据总线
  136.       //for (i=0;i<65535;i++) if(DATA==0) break;

  137.          for (i=0;i<55535;i++){ if(DATA==0) break;else         {xianshi();} }


  138.                   read();//读温度


  139.   }


  140. /////////温湿度处理//////

  141.     text_jishuan_temp11()
  142.         {
  143.            error=0;
  144.            ack=0;
  145.       sht_rest() ;//复位

  146.             text_a(TEMP_ML);

  147.       text_jishuan_temp();
  148.            key();
  149.              text_a(HUMI_ML);

  150.       text_jishuan_humi();


  151.           }

  152. /////

  153. //////////计算温度////
  154. text_jishuan_temp()
  155. {
  156.     float aa=0,bb=0,temp_zi;
  157.         int   abcd=0;

  158. aa=(float)temp_h*256+(float)temp_LL;
  159. temp_zi=0.01*aa-40;

  160. //

  161. if  (temp_zi<0)
  162.     {
  163.           temp_zi=0;
  164.     }
  165.    temp_zi=temp_zi*10;
  166.    xianzhi_t=(int)temp_zi;//给显示值

  167.    }
  168.   ///////计算湿度//////
  169.   text_jishuan_humi()
  170. {
  171.     float aa=0,bb=0,humi_zi;
  172.         int   abcd=0;

  173. aa=(float)temp_h*256+(float)temp_LL;

  174. bb=aa*aa*2.8/1000000;
  175. aa=0.0405*aa;
  176. aa=aa-4-bb;
  177. humi_zi=aa;

  178. //
  179.     humi_zi=humi_zi*10;
  180.    xianzhi_h=(int)humi_zi;
  181.    }



  182. ///////延时///////
  183. delay(int i)
  184. {
  185.    while(--i);     
  186.      
  187. }     

  188. ///////显示处理///////
  189. xianshi()
  190. {   
  191.     int   abcd=0;
  192.     int i;
  193.   for (i=0;i<1;i++)
  194. {  
  195. abcd=xianzhi_h;
  196. gwei=1;
  197. swei=1;
  198. bwei=1;
  199. qwei=1;
  200. P1=dispcode[abcd/100];
  201.   qwei=0;
  202. delay(40);
  203.   qwei=1;
  204.   abcd=abcd%100          ;
  205. P1=dispcode[abcd/10];
  206. bwei=0;
  207. delay(40);
  208. bwei=1;

  209. if(setbz_h^setbz_l)
  210. {
  211.     if(setbz_h) abcd=setzhi_h;
  212.     if(setbz_l) abcd=setzhi_l;

  213.          P1=dispcode[abcd/10];
  214.           swei=0;
  215.          delay(40);
  216.           swei=1;
  217.          P1=dispcode[abcd%10];
  218.          gwei=0;
  219.          delay(40);
  220.          gwei=1;  

  221.   }
  222.   else
  223.   {
  224.          abcd=xianzhi_t;
  225.          P1=dispcode[abcd/100];
  226.           swei=0;
  227.          delay(40);
  228.           swei=1;
  229.           abcd=abcd%100        ;
  230.          P1=dispcode[abcd/10];
  231.          gwei=0;
  232.          delay(40);
  233.          gwei=1;  
  234.    }
  235. }
  236. }

  237.   doing()
  238.   {
  239.   char xianzhi_mi;
  240.    xianzhi_mi=xianzhi_t/10;
  241.    if((xianzhi_mi<setzhi_h)&(xianzhi_t>setzhi_l)) { motor=0;hot=0;speek=0;}
  242.    if(xianzhi_mi>setzhi_h)  { motor=1;hot=0;speek=1;}
  243.    if(xianzhi_mi<setzhi_l)  { motor=0;hot=1;speek=1;}
  244.   }


  245. key()
  246. {
  247.   if(set&setkey)
  248.   {
  249.      setkey=0;
  250.      if(setbz_l) {setbz_l=0;setbz_h=0;}
  251.          else
  252.          { if(!setbz_h) setbz_h=1;
  253.            else {setbz_h=0;setbz_l=1;}
  254.           }
  255.    }
  256.    if(!set)  setkey=1;

  257.   if(setup==0)
  258.   {
  259.       if(setbz_h==1)
  260.                { if (setzhi_h<=99) setzhi_h++;}
  261.       if(setbz_l==1)
  262.                { if ((setzhi_l<setzhi_h)&(setzhi_l<=99)) setzhi_l++;}
  263.   }                    

  264. if(setdown==0)
  265.   {
  266.       if(setbz_h==1)
  267.                { if ((setzhi_h>setzhi_l)&(setzhi_h>=1)) setzhi_h--;}
  268.       if(setbz_l==1)
  269.                { if (setzhi_l>=1) setzhi_l--;}
  270. ……………………

  271. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
SHT11温湿度传感器仿真.rar (76.84 KB, 下载次数: 78)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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