SHT11仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h> //头文件
- #include <intrins.h>
- #include <stdio.h> //
- #include <math.h> //Keil library
- //**************************************
- #define uchar unsigned char //定义一下方便使用
- #define uint unsigned int //定义一下方便使用
- #define ulong unsigned long //定义一下方便使用
- #define TEMP_ML 0x03 //000 0001 1 温度命令
- #define HUMI_ML 0x05 //000 0010 1 温度命令
- unsigned char error ;//全局错误变量
- unsigned char ack ;//全局应答变量
- //float temp_zi ;//全局应答变量
- //float humi_zi ;//全局应答变量
- unsigned char temp_h ;//全局应答变量
- unsigned char temp_LL ;//全局应答变量
- unsigned int xianzhi_t=0;//温度显值
- unsigned int xianzhi_h=0;//湿度显值
- uchar setzhi_h,setzhi_l;
- bit setbz_h,setbz_l,setkey;
- sbit DATA =P2^6;//数据
- sbit SCK=P2^7;//时钟
- sbit hot =P2^0;// 加热
- sbit motor =P2^1;// 电机
- sbit speek =P2^2;// 声音
- sbit set =P0^0;// 设置
- sbit setup =P0^1;// 设置+
- sbit setdown =P0^2;// 设置-
- sbit gwei =P3^4;//个位
- sbit swei =P3^3;//十位
- sbit bwei =P3^2;//百位
- sbit qwei =P3^1;//千位
- unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//共阳
- //*******************************基本驱动程
- /////////////////
- //////////////////////
- char read() //读一个字节 返回应答信号
- //----------------------------------------------------------------------------------
- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
- {
- unsigned char i,val=0;
- temp_LL=0;
- temp_h=0;
- DATA=1; //释放数据总线
- for (i=0x80;i>0;i/=2) //位移8位
- { SCK=1; //上升沿读入
- if (DATA) val=(val | i); //确定值
- SCK=0;
- }
- DATA=0; //读应答信号,有应答为1,为应答为0 通过CPU下拉为应答
- SCK=1; //第9个脉冲
- _nop_(); _nop_(); _nop_(); //pulswith approx. 5 us
- SCK=0;
- DATA=1; //释放数据总线
- temp_h=val;
- val=0;
- ////低8位/////////////////////////////
- DATA=1; //释放数据总线
- for (i=0x80;i>0;i/=2) //位移8位
- { SCK=1; //上升沿读入
- if (DATA) val=(val | i); //确定值
- SCK=0;
- }
- DATA=1;//0; //不需要应答 通过CPU下拉为应答
- SCK=1; //第9个脉冲
- _nop_(); _nop_(); _nop_(); //pulswith approx. 5 us
- SCK=0;
- DATA=1; //释放数据总线
- temp_LL=val;
- }
- ////////////
- char write(unsigned char value) //写一个字节 返回应答信号
- //---------------------------------------------------------
- {
- unsigned char i ;
- ack=0;
- for (i=0x80;i>0;i/=2) //释放数据总线
- { if (i & value) DATA=1; //写入值
- else DATA=0;
- SCK=1; //上升沿写入
- _nop_(); _nop_(); _nop_(); //延时
- SCK=0;
- }
- DATA=1; //释放数据总线
- SCK=1; //第9个脉冲
- if (DATA==1) ack=1;
- //读应答信号
- SCK=0;
- return ack; //error=1 表示没有应答
- }
- ////////
- void start_sht11(void) //启动
- //--------------------------------------------------------
- {
- DATA=1; SCK=0; //数据为1,SCK=0
- _nop_();
- SCK=1; //第一个脉冲
- _nop_();
- DATA=0; //数据跌落
- _nop_ ();
- SCK=0; //完成一个脉冲
- _nop_(); _nop_(); _nop_();
- SCK=1; //再一个脉冲
- _nop_();
- DATA=1; //数据变为1
- _nop_();
- SCK=0; //完成该脉冲
- }
- //////////////////////////////////
- void sht_rest(void) //复位
-
- {
- unsigned char i;
- DATA=1; SCK=0; //数据为1 时钟为0
- for(i=0;i<9;i++) //9 个脉冲为 复位
- { SCK=1;
- SCK=0;
- }
- start_sht11(); //启动
- }
- ////////////////////////////////
- //测量温度或者是温度,返回校验值
- text_a(unsigned char ml)
- {
- unsigned int i;
- start_sht11(); //启动
- write(ml);//写入测温度
- if (ack==1)
- {
- sht_rest() ;//复位
- write(ml);//写入测温度
- }
-
- //判断是否处于忙
- // DATA=1;//释放数据总线
- //for (i=0;i<65535;i++) if(DATA==0) break;
- for (i=0;i<55535;i++){ if(DATA==0) break;else {xianshi();} }
- read();//读温度
- }
- /////////温湿度处理//////
- text_jishuan_temp11()
- {
- error=0;
- ack=0;
- sht_rest() ;//复位
- text_a(TEMP_ML);
- text_jishuan_temp();
- key();
- text_a(HUMI_ML);
-
- text_jishuan_humi();
- }
- /////
- //////////计算温度////
- text_jishuan_temp()
- {
- float aa=0,bb=0,temp_zi;
- int abcd=0;
- aa=(float)temp_h*256+(float)temp_LL;
- temp_zi=0.01*aa-40;
- //
- if (temp_zi<0)
- {
- temp_zi=0;
- }
- temp_zi=temp_zi*10;
- xianzhi_t=(int)temp_zi;//给显示值
- }
- ///////计算湿度//////
- text_jishuan_humi()
- {
- float aa=0,bb=0,humi_zi;
- int abcd=0;
- aa=(float)temp_h*256+(float)temp_LL;
- bb=aa*aa*2.8/1000000;
- aa=0.0405*aa;
- aa=aa-4-bb;
- humi_zi=aa;
- //
- humi_zi=humi_zi*10;
- xianzhi_h=(int)humi_zi;
- }
- ///////延时///////
- delay(int i)
- {
- while(--i);
-
- }
- ///////显示处理///////
- xianshi()
- {
- int abcd=0;
- int i;
- for (i=0;i<1;i++)
- {
- abcd=xianzhi_h;
- gwei=1;
- swei=1;
- bwei=1;
- qwei=1;
- P1=dispcode[abcd/100];
- qwei=0;
- delay(40);
- qwei=1;
- abcd=abcd%100 ;
- P1=dispcode[abcd/10];
- bwei=0;
- delay(40);
- bwei=1;
- if(setbz_h^setbz_l)
- {
- if(setbz_h) abcd=setzhi_h;
- if(setbz_l) abcd=setzhi_l;
- P1=dispcode[abcd/10];
- swei=0;
- delay(40);
- swei=1;
- P1=dispcode[abcd%10];
- gwei=0;
- delay(40);
- gwei=1;
- }
- else
- {
- abcd=xianzhi_t;
- P1=dispcode[abcd/100];
- swei=0;
- delay(40);
- swei=1;
- abcd=abcd%100 ;
- P1=dispcode[abcd/10];
- gwei=0;
- delay(40);
- gwei=1;
- }
- }
- }
- doing()
- {
- char xianzhi_mi;
- xianzhi_mi=xianzhi_t/10;
- if((xianzhi_mi<setzhi_h)&(xianzhi_t>setzhi_l)) { motor=0;hot=0;speek=0;}
- if(xianzhi_mi>setzhi_h) { motor=1;hot=0;speek=1;}
- if(xianzhi_mi<setzhi_l) { motor=0;hot=1;speek=1;}
- }
- key()
- {
- if(set&setkey)
- {
- setkey=0;
- if(setbz_l) {setbz_l=0;setbz_h=0;}
- else
- { if(!setbz_h) setbz_h=1;
- else {setbz_h=0;setbz_l=1;}
- }
- }
- if(!set) setkey=1;
- if(setup==0)
- {
- if(setbz_h==1)
- { if (setzhi_h<=99) setzhi_h++;}
- if(setbz_l==1)
- { if ((setzhi_l<setzhi_h)&(setzhi_l<=99)) setzhi_l++;}
- }
- if(setdown==0)
- {
- if(setbz_h==1)
- { if ((setzhi_h>setzhi_l)&(setzhi_h>=1)) setzhi_h--;}
- if(setbz_l==1)
- { if (setzhi_l>=1) setzhi_l--;}
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
SHT11温湿度传感器仿真.rar
(76.84 KB, 下载次数: 78)
|