基于单片机SHT11温湿度传感器电路图于程序

上传人:jun****875 文档编号:17807137 上传时间:2020-12-07 格式:DOC 页数:12 大小:132.41KB
返回 下载 相关 举报
基于单片机SHT11温湿度传感器电路图于程序_第1页
第1页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序_第2页
第2页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序_第3页
第3页 / 共12页
点击查看更多>>
资源描述
基于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位温度/*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 code 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+); /*函数功能: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=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=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 command) uchar dat; SHT11_Start(); SHT11_Sendbyte(command); SHT11_Answer(); dat=SHT11_Receivebyte(); SHT11_End(); return(dat);/*函数名称:SHT11_Measure(uchar command,uint time);函数功能:设置SHT11检测功能,并返回相应的检测结果函数说明:command形参用于设定温度检测还是湿度检测,time形参用于设定检测过程中的等待时间,以确定检测结果的位数11ms/55ms/210ms 分别对应8位/12位/14位 */uint SHT11_Measure(uchar command,uchar time) uint dat=0; uchar data_high,data_low; SHT11_Start(); SHT11_Sendbyte(command); 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; 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_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;sbit 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 Temp2Con) 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); 还有些地方不是很完美
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!