1、基于89C51单片机SHT11温湿度传感器电路图于程序作者: 张志杰SHT11、h文件:#ifndef _SHT11_H_#define _SHT11_H_/*SHT11相关命令*/#define uchar unsigned char#define uint unsigned int#define TEM_TEST 0x03/温度检测命令#define HUM_TEST 0x05/湿度检测命令#define REG_READ 0x07/读寄存器#define REG_WRITE 0x06/写寄存器#define FUNCTION_SET 0x01/设置SHT11得工作精度为8位/湿度 12位
2、温度/*SHT11端口定义*/sbit SHT11_DATA=P31;sbit SHT11_SCK=P30;sbit P33=P33;sbit P32=P32;sbit P36=P36;sbit P37=P37;uchar flag_tempeture=0; /显示温度位置得标志uchar flag_humidity=0; /显示湿度位置得标志uchar code str1= 0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00;/温度图标uchar code str6_sht11=%RH ; uchar code str4_sht11=humi=;uchar cod
3、e str2_sht11=temp=;uchar code str7_sht11= ;/清除没不要得显示/*函数名称:Delay函数功能:SHT11内部延时*/void Delay ; ;/*函数名称:Delay_Ms函数功能:SHT11检测等待延时函数说明:11ms/55ms/210ms 分别对应8位/12位/14位 测量结果对应得形参为N 则延时Nms*/void Delay_Ms(uint ms) / ms延时函数 (AT89C51 11、0592MHz) uint i; uchar j; for(i=0;ims;i+) for(j=0;j200;j+); for(j=0;j102;j+
4、); /*函数功能:SHT11启动时序*/void SHT11_Start SHT11_SCK=1; SHT11_DATA=1; Delay; SHT11_DATA=0; Delay; SHT11_SCK=0; Delay; SHT11_SCK=1; Delay; SHT11_DATA=1;/*函数名称:SHT11_Sendbyte(uchar dat)函数功能: 向SHT11发送8bite数据*/void SHT11_Sendbyte(uchar dat) uchar i; SHT11_SCK=0; Delay; for(i=0;i8;i+) if(dat&0x80) SHT11_DATA=
5、1; Delay; else SHT11_DATA=0; Delay; dat=dat1; SHT11_SCK=1; Delay; SHT11_SCK=0; /*函数名称SHT11_Answer:函数功能:检测SHT11得响应信号(在第九个时钟周期)*/void SHT11_Answer SHT11_SCK=1; Delay; while(SHT11_DATA=1); SHT11_SCK=0; SHT11_DATA=1;/*函数名称:SHT11_Test_Finish函数功能:检测SHT11温湿度检测就是否完毕*/void SHT11_Test_Finish while(SHT11_DATA=
6、1);/*函数名称:SHT11_Receivebyte函数功能:从SHT11接收8bite数据*/uchar SHT11_Receivebyte uchar i; uchar dat; SHT11_SCK=0; Delay; for(i=0;i8;i+) SHT11_SCK=1; Delay; dat=dat2、47V 当位6为1时表明VDD2、47V即电量不足 位0表明当前得测量分辨率 当位0为1时表明测量精度:8位/湿度 12位温度 当位0为0时表明测量精度:12位湿度 14位温度 默认为0*/uchar SHT11_Read_Register(uchar mand) uchar dat;
7、 SHT11_Start; SHT11_Sendbyte(mand); SHT11_Answer; dat=SHT11_Receivebyte; SHT11_End; return(dat);/*函数名称:SHT11_Measure(uchar mand,uint time);函数功能:设置SHT11检测功能,并返回相应得检测结果函数说明:mand形参用于设定温度检测还就是湿度检测,time形参用于设定检测过程中得等待时间,以确定检测结果得位数11ms/55ms/210ms 分别对应8位/12位/14位 */uint SHT11_Measure(uchar mand,uchar time) u
8、int dat=0; uchar data_high,data_low; SHT11_Start; SHT11_Sendbyte(mand); SHT11_Answer; Delay_Ms(time); SHT11_Test_Finish; data_high=SHT11_Receivebyte; MCU_Answer; data_low=SHT11_Receivebyte; SHT11_End; dat=(dat|data_high); dat=(dat23) tempeture1=tempeture1+1; if(tempeture155) tempeture1=tempeture1+1;
9、 if(P37=1) if(tempeture1=16&tempeture1=400&r=600) P32=1; else P32=0; return(r);#endifDisplay、c文件:#include #include SHT11、h#define uchar unsigned char#define uint unsigned int#define TEM_TEST 0x03/温度检测命令#define HUM_TEST 0x05/湿度检测命令#define REG_READ 0x07/读寄存器#define REG_WRITE 0x06/写寄存器#define FUNCTION_
10、SET 0x01/设置SHT11得工作精度为8位/湿度 12位温度uchar DispData4 = 0, 1, 2, 3;code uchar DispSegmentP010=0x3f,0x06,0x1b,0x0f,0x26,0x2d,0x3d,0x07,0x3f,0x2f;code uchar DispSegmentP210=0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x00,0x22,0x22; / 0 1 2 3 4 5 6 7 8 9 %code uchar DispCtrl4 = 0xef, 0xdF, 0xbf ,0x7F;sbit P16=P16;s
11、bit P26=P26;sbit P34=P34; void Temp_delay(unsigned int j) uchar i; for(i=100;i0;i) for(j;j0;j); void Show(uchar *Buffer) uchar i; for (i=0; i4; i+) P1 = DispCtrli; P0 = DispSegmentP0*Buffer; P2 = DispSegmentP2*Buffer; if(P16=0) P26=1; Temp_delay(2); Buffer+; void DispConvert(uchar *DispAddr, uint Te
12、mp2Con) DispAddr0 = Temp2Con/1000; DispAddr1 = (Temp2Con DispAddr0*1000)/100; DispAddr2 = (Temp2Con DispAddr0*1000 DispAddr1*100)/10; DispAddr3 = Temp2Con DispAddr0*1000 DispAddr1*100 DispAddr2*10; void main uint temp; uint dat; uint Hum; /float f; while(1) Show(DispData); SHT11_Write_Register(REG_WRITE,FUNCTION_SET); temp=SHT11_Measure(TEM_TEST,0x37); temp=SHT11_Convert_Tempeture12bit(temp); dat=SHT11_Measure(HUM_TEST,0x0b); Hum=SHT11_Convert_Humidity8bit(dat,temp); if(P34=0) P36=1; P37=0; DispConvert(DispData,Hum); else P36=0; P37=1; DispConvert(DispData,temp*10); 还有些地方不就是很完美
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100