找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3037|回复: 6
收起左侧

Sht11湿度测量proteus仿真和单片机源代码

[复制链接]
ID:189579 发表于 2017-4-14 16:40 | 显示全部楼层 |阅读模式
sht11温度测量仿真   大家一起学习学习

sht11的单片机仿真原理图和仿真效果:
0.png

51单片机源代码:
  1. #include<reg52.h>
  2. #include <intrins.h>
  3. #include <math.h>    //Keil library
  4. #include <stdio.h>         //Keil library

  5. #define uchar unsigned char

  6. typedef union                   //定义共同类型
  7. { unsigned int i;           //i表示测量得到的温湿度数据(int 形式保存的数据)
  8. float f;                           //f表示测量得到的温湿度数据(float 形式保存的数据)
  9. } value;


  10. enum {TEMP,HUMI};

  11. sbit DATA = P1^1;
  12. sbit SCK = P1^0;

  13. sbit RS  = P2^0;
  14. sbit RW  = P2^1;
  15. sbit E  = P2^2;
  16. sfr DBPort  = 0x80;     //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口

  17. /********     1602函数声明     ********/
  18. void LCD_Initial();
  19. void GotoXY(unsigned char x, unsigned char y);
  20. void Print(unsigned char *str);
  21. void LCD_Write(bit style, unsigned char input);


  22. /********     SHT11函数声明      ********/
  23. void s_connectionreset(void);
  24. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
  25. void calc_sth10(float *p_humidity ,float *p_temperature);
  26. //float calc_dewpoint(float h,float t);



  27. /***********************************************************************************************************************************************************/

  28. //SHT11程序


  29. #define noACK 0                                //继续传输数据,用于判断是否结束通讯
  30. #define ACK   1             //结束数据传输;
  31.                                                         //地址  命令  读/写
  32. #define STATUS_REG_W 0x06   //000   0011    0
  33. #define STATUS_REG_R 0x07   //000   0011    1
  34. #define MEASURE_TEMP 0x03   //000   0001    1
  35. #define MEASURE_HUMI 0x05   //000   0010    1
  36. #define RESET        0x1e   //000   1111    0



  37. //写字节程序
  38. char s_write_byte(unsigned char value)   
  39. {
  40.         unsigned char i,error=0;
  41.         for (i=0x80;i>0;i/=2)             //shift bit for masking 高位为1,循环右移
  42.         {
  43.                 if (i&value) DATA=1;          //和要发送的数相与,结果为发送的位
  44.             else DATA=0;                        
  45.             SCK=1;                          
  46.             _nop_();_nop_();_nop_();        //延时3us
  47.             SCK=0;
  48.         }
  49.         DATA=1;                           //释放数据线
  50.         SCK=1;                           
  51.         error=DATA;                       //检查应答信号,确认通讯正常
  52.         _nop_();_nop_();_nop_();
  53.         SCK=0;        
  54.         DATA=1;
  55.         return error;                     //error=1 通讯错误
  56. }

  57. //读字节程序
  58. char s_read_byte(unsigned char ack)
  59. //----------------------------------------------------------------------------------
  60. {
  61.         unsigned char i,val=0;
  62.         DATA=1;                           //释放数据线
  63.         for(i=0x80;i>0;i>>=1)             //高位为1,循环右移
  64.         {
  65.                 SCK=1;                        
  66.             if(DATA) val=(val|i);        //读一位数据线的值
  67.             SCK=0;      
  68.         }
  69.         DATA=!ack;                        //如果是校验,读取完后结束通讯;
  70.         SCK=1;                           
  71.         _nop_();_nop_();_nop_();          //延时3us
  72.         SCK=0;   
  73.         _nop_();_nop_();_nop_();      
  74.         DATA=1;                           //释放数据线
  75.         return val;
  76. }

  77. //启动传输
  78. void s_transstart(void)
  79. // generates a transmission start
  80. //       _____         ________
  81. // DATA:      |_______|
  82. //           ___     ___
  83. // SCK : ___|   |___|   |______
  84. {
  85.            DATA=1; SCK=0;                   //准备
  86.            _nop_();
  87.           SCK=1;
  88.            _nop_();
  89.            DATA=0;
  90.            _nop_();
  91.           SCK=0;
  92.            _nop_();_nop_();_nop_();
  93.            SCK=1;
  94.            _nop_();
  95.           DATA=1;     
  96.           _nop_();
  97.            SCK=0;     
  98. }


  99. //连接复位
  100. void s_connectionreset(void)
  101. // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
  102. //       _____________________________________________________         ________
  103. // DATA:                                                      |_______|
  104. //          _    _    _    _    _    _    _    _    _        ___     ___
  105. // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
  106. {
  107.         unsigned char i;
  108.         DATA=1; SCK=0;                    //准备
  109.         for(i=0;i<9;i++)                  //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位
  110.         {
  111.                 SCK=1;
  112.             SCK=0;
  113.         }
  114.         s_transstart();                   //启动传输
  115. }


  116. //软复位程序
  117. char s_softreset(void)
  118. // resets the sensor by a softreset
  119. {
  120.         unsigned char error=0;
  121.         s_connectionreset();              //启动连接复位
  122.         error+=s_write_byte(RESET);       //发送复位命令
  123.         return error;                     //error=1 通讯错误
  124. }


  125. /*
  126. //读状态寄存器
  127. char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
  128. //----------------------------------------------------------------------------------
  129. // reads the status register with checksum (8-bit)
  130. {
  131.         unsigned char error=0;
  132.         s_transstart();                   //transmission start
  133.         error=s_write_byte(STATUS_REG_R); //send command to sensor
  134.         *p_value=s_read_byte(ACK);        //read status register (8-bit)
  135.         *p_checksum=s_read_byte(noACK);   //read checksum (8-bit)
  136.         return error;                     //error=1 in case of no response form the sensor
  137. }

  138. //写状态寄存器
  139. char s_write_statusreg(unsigned char *p_value)
  140. // writes the status register with checksum (8-bit)
  141. {
  142.         unsigned char error=0;
  143.         s_transstart();                   //transmission start
  144.         error+=s_write_byte(STATUS_REG_W);//send command to sensor
  145.         error+=s_write_byte(*p_value);    //send value of status register
  146.         return error;                     //error>=1 in case of no response form the sensor
  147. }                                                                                                                        */                           

  148.            
  149. //温湿度测量
  150. char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
  151. // 进行温度或者湿度转换,由参数mode决定转换内容;
  152. {
  153. //        enum {TEMP,HUMI};                 //已经在头文件中定义
  154.         unsigned error=0;
  155.         unsigned int i;

  156.         s_transstart();                   //启动传输
  157.         switch(mode)                     //选择发送命令
  158.     {        
  159.                 case TEMP : error+=s_write_byte(MEASURE_TEMP); break;                   //测量温度
  160.             case HUMI : error+=s_write_byte(MEASURE_HUMI); break;                   //测量湿度
  161.             default     : break;
  162.         }
  163.         for (i=0;i<65535;i++) if(DATA==0) break; //等待测量结束
  164.         if(DATA) error+=1;                // 如果长时间数据线没有拉低,说明测量错误
  165.         *(p_value) =s_read_byte(ACK);    //读第一个字节,高字节 (MSB)
  166.         *(p_value+1)=s_read_byte(ACK);    //读第二个字节,低字节 (LSB)
  167.         *p_checksum =s_read_byte(noACK); //read CRC校验码
  168.         return error;                                         // error=1 通讯错误
  169. }

  170. //温湿度值标度变换及温度补偿
  171. void calc_sth10(float *p_humidity ,float *p_temperature)
  172. {
  173.         const float C1=-4.0;              // 12位湿度精度 修正公式
  174.         const float C2=+0.0405;           // 12位湿度精度 修正公式
  175.         const float C3=-0.0000028;        // 12位湿度精度 修正公式
  176.         const float T1=+0.01;             // 14位温度精度 5V条件  修正公式
  177.         const float T2=+0.00008;          // 14位温度精度 5V条件  修正公式

  178.         float rh=*p_humidity;             // rh:      12位 湿度
  179.         float t=*p_temperature;           // t:       14位 温度
  180.         float rh_lin;                     // rh_lin: 湿度 linear值
  181.         float rh_true;                    // rh_true: 湿度 ture值
  182.         float t_C;                        // t_C   : 温度 ℃

  183.         t_C=t*0.01 - 40;                  //补偿温度
  184.         rh_lin=C3*rh*rh + C2*rh + C1;     //相对湿度非线性补偿
  185.         rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //相对湿度对于温度依赖性补偿
  186.         if(rh_true>100)rh_true=100;       //湿度最大修正
  187.         if(rh_true<0.1)rh_true=0.1;       //湿度最小修正

  188.         *p_temperature=t_C;               //返回温度结果
  189.         *p_humidity=rh_true;              //返回湿度结果
  190. }

  191. //从相对温度和湿度计算露点
  192. /*float calc_dewpoint(float h,float t)
  193. {
  194.         float logEx,dew_point;
  195.         logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
  196.         dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
  197.         return dew_point;
  198. }                                                                                           */

  199. /***********************************************************************************************************************************************************/
  200. //1602程序


  201. //内部等待函数**************************************************************
  202. unsigned char LCD_Wait(void)
  203. {
  204.     RS=0;
  205.     RW=1;    _nop_();
  206.     E=1;    _nop_();           
  207.     E=0;
  208.     return DBPort;  
  209. }
  210. //向LCD写入命令或数据********************************************************
  211. #define LCD_COMMAND         0      // Command
  212. #define LCD_DATA            1      // Data
  213. #define LCD_CLEAR_SCREEN    0x01      // 清屏
  214. #define LCD_HOMING          0x02      // 光标返回原点
  215. void LCD_Write(bit style, unsigned char input)
  216. {
  217.     E=0;
  218.     RS=style;
  219.     RW=0;        _nop_();
  220.     DBPort=input;   _nop_();//注意顺序
  221.     E=1;        _nop_();//注意顺序
  222.     E=0;        _nop_();
  223.     LCD_Wait();
  224. }

  225. //设置显示模式************************************************************
  226. #define LCD_SHOW            0x04    //显示开
  227. #define LCD_HIDE            0x00    //显示关

  228. #define LCD_CURSOR          0x02    //显示光标
  229. #define LCD_NO_CURSOR       0x00    //无光标        

  230. #define LCD_FLASH           0x01    //光标闪动
  231. #define LCD_NO_FLASH        0x00    //光标不闪动

  232. void LCD_SetDisplay(unsigned char DisplayMode)
  233. {
  234.     LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
  235. }

  236. //设置输入模式************************************************************
  237. #define LCD_AC_UP       0x02
  238. #define LCD_AC_DOWN         0x00      // default

  239. #define LCD_MOVE            0x01      // 画面可平移
  240. #define LCD_NO_MOVE         0x00      //default

  241. void LCD_SetInput(unsigned char InputMode)
  242. {
  243.     LCD_Write(LCD_COMMAND, 0x04|InputMode);
  244. }

  245. //初始化LCD************************************************************
  246. void LCD_Initial()
  247. {
  248.     E=0;
  249.     LCD_Write(LCD_COMMAND,0x38);           //8位数据端口,2行显示,5*7点阵
  250.     LCD_Write(LCD_COMMAND,0x38);
  251.     LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);    //开启显示, 无光标
  252.     LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);   //清屏
  253.     LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);       //AC递增, 画面不动
  254. }

  255. //液晶字符输入的位置************************
  256. void GotoXY(unsigned char x, unsigned char y)
  257. {
  258.     if(y==0)
  259.         LCD_Write(LCD_COMMAND,0x80|x);
  260.     if(y==1)
  261.         LCD_Write(LCD_COMMAND,0x80|(x-0x40));
  262. }

  263. //将字符输出到液晶显示
  264. void Print(unsigned char *str)
  265. {
  266.     while(*str!='\0')
  267.     {
  268.         LCD_Write(LCD_DATA,*str);
  269.         str++;
  270.     }
  271. }



  272. //延时函数
  273. void delay(int z)                //z为毫秒数
  274. {
  275.         int x,y;
  276.         for(x=z;x>0;x--)
  277.                 for(y=125;y>0;y--);
  278. }

  279. /***********************************************************************************************************************************************************/
  280. //主函数

  281. void main()
  282. {
  283.         unsigned int temp,humi;
  284.         value humi_val,temp_val;                //定义两个共同体,一个用于湿度,一个用于温度
  285. //        float dew_point;                            //用于记录露点值
  286.         unsigned char error;                    //用于检验是否出现错误
  287.         unsigned char checksum;                        //CRC                        
  288.         uchar TEMP1[7];                                         //用于记录温度
  289.         uchar HUMI1[6];                                         //用于记录湿度
  290.         


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


0.png
下载:
Sht11湿度测量仿真ver1.1.zip (82.14 KB, 下载次数: 61)

评分

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

查看全部评分

回复

使用道具 举报

ID:194995 发表于 2017-4-30 14:28 | 显示全部楼层
可以的!
回复

使用道具 举报

ID:194995 发表于 2017-4-30 14:40 | 显示全部楼层
请问有没有抗干扰电路啊,这个好像有点误差
回复

使用道具 举报

ID:244326 发表于 2017-11-30 10:01 | 显示全部楼层
有仿真图?
回复

使用道具 举报

ID:255544 发表于 2017-11-30 13:07 | 显示全部楼层
可以的,感觉还可以,不错
回复

使用道具 举报

ID:272969 发表于 2018-1-8 13:20 | 显示全部楼层
SHT11可以拆分开来用AD转换吗
回复

使用道具 举报

ID:304821 发表于 2018-4-10 10:00 | 显示全部楼层
proteus的仿真文件在 proteus8 怎么打开??新手 小白 不太会。。。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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