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

上传人:b****4 文档编号:5729199 上传时间:2023-05-09 格式:DOCX 页数:12 大小:93.46KB
下载 相关 举报
基于单片机SHT11温湿度传感器电路图于程序.docx_第1页
第1页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第2页
第2页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第3页
第3页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第4页
第4页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第5页
第5页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第6页
第6页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第7页
第7页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第8页
第8页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第9页
第9页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第10页
第10页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第11页
第11页 / 共12页
基于单片机SHT11温湿度传感器电路图于程序.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《基于单片机SHT11温湿度传感器电路图于程序.docx》由会员分享,可在线阅读,更多相关《基于单片机SHT11温湿度传感器电路图于程序.docx(12页珍藏版)》请在冰点文库上搜索。

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

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

基于89C51单片机SHT11温湿度传感器电路图于程序作者:

X志杰

SHT11.h文件:

#ifndef__SHT11_H__

#define__SHT11_H__

/*************************

SHT11相关命令

**************************/

#defineucharunsignedchar

#defineuintunsignedint

#defineTEM_TEST0x03//温度检测命令

#defineHUM_TEST0x05//湿度检测命令

#defineREG_READ0x07//读寄存器

#defineREG_WRITE0x06//写寄存器

#defineFUNCTION_SET0x01//设置SHT11的工作精度为8位/湿度12位温度

/**************************

SHT11端口定义

***************************/

sbitSHT11_DATA=P3^1;

sbitSHT11_SCK=P3^0;

sbitP33=P3^3;

sbitP32=P3^2;

sbitP36=P3^6;

sbitP37=P3^7;

ucharflag_tempeture=0;//显示温度位置的标志

ucharflag_humidity=0;//显示湿度位置的标志

ucharcodestr1[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//温度图标

ucharcodestr6_sht11[]="%RH";

ucharcodestr4_sht11[]="humi=";

ucharcodestr2_sht11[]="temp=";

ucharcodestr7_sht11[]="";//清除没不要的显示

/***************************

函数名称:

Delay()

函数功能:

SHT11内部延时

****************************/

voidDelay()

{

;

;

}

/***************************

函数名称:

Delay_Ms()

函数功能:

SHT11检测等待延时

函数说明:

11ms/55ms/210ms分别对应8位/12位/14位测量结果

对应的形参为N则延时Nms

****************************/

voidDelay_Ms(uintms)//ms延时函数(AT89C5111.0592MHz)

{

uinti;

ucharj;

for(i=0;i

{

for(j=0;j<200;j++);

for(j=0;j<102;j++);

}

}

/**************************

函数功能:

SHT11启动时序

***************************/

voidSHT11_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(uchardat)

函数功能:

向SHT11发送8bite数据

******************************/

voidSHT11_Sendbyte(uchardat)

{

uchari;

SHT11_SCK=0;

Delay();

for(i=0;i<8;i++)

{

if(dat&0x80)

{

SHT11_DATA=1;

Delay();

}

else

{

SHT11_DATA=0;

Delay();

}

dat=dat<<1;

SHT11_SCK=1;

Delay();

SHT11_SCK=0;

}

}

/*********************************

函数名称SHT11_Answer():

函数功能:

检测SHT11的响应信号(在第九个时钟周期)

***********************************/

voidSHT11_Answer()

{

SHT11_SCK=1;

Delay();

while(SHT11_DATA==1);

SHT11_SCK=0;

SHT11_DATA=1;

}

/************************************

函数名称:

SHT11_Test_Finish()

函数功能:

检测SHT11温湿度检测是否完毕

*************************************/

voidSHT11_Test_Finish()

{

while(SHT11_DATA==1);

}

/************************************

函数名称:

SHT11_Receivebyte()

函数功能:

从SHT11接收8bite数据

*************************************/

ucharSHT11_Receivebyte()

{

uchari;

uchardat;

SHT11_SCK=0;

Delay();

for(i=0;i<8;i++)

{

SHT11_SCK=1;

Delay();

dat=dat<<1;

if(SHT11_DATA)

{

dat=dat|0x01;

Delay();

}

else

{

dat=dat&0xfe;

Delay();

}

SHT11_SCK=0;

Delay();

}

SHT11_DATA=1;//释放数据总线

return(dat);

}

/***********************************

函数名称:

MCU_Answer()

函数功能:

单片机向SHT11发送应答信号

*************************************/

voidMCU_Answer()

{

SHT11_SCK=0;

Delay();

SHT11_DATA=0;

Delay();

SHT11_SCK=1;

Delay();

SHT11_SCK=0;

Delay();

SHT11_DATA=1;//释放数据总线这条指令非常重要不加的话导致单片机不能读取低8位

}

/***********************************

函数名称:

SHT11_End()

当接收两个8byte数据后部接收CRC校验码

************************************/

voidSHT11_End()

{

SHT11_DATA=1;

SHT11_SCK=1;

Delay();

SHT11_SCK=0;

Delay();

}

/*************************************

函数名称:

voidSHT11_Write_Register(ucharmand,uchardat)

函数说明:

向SHT11的状态寄存器设置功能

mand为REG_WRITE0x06写寄存器

dat为设置SHT11的功能可以设置检测的数据位数

*/

voidSHT11_Write_Register(ucharmand,uchardat)

{

SHT11_Start();

SHT11_Sendbyte(mand);

SHT11_Answer();

SHT11_Sendbyte(dat);

SHT11_Answer();

}

/***************************************

函数名称:

ucharSHT11_Read_Register(ucharmand)

函数说明:

mand为REG_READ0x07//读寄存器

返回值为状态寄存器的值

位6显示当前检测完一次数据后电源供电情况

当位6为0时表明VDD>2.47V当位6为1时表明VDD<2.47V即电量不足

位0表明当前的测量分辨率

当位0为1时表明测量精度:

8位/湿度12位温度

当位0为0时表明测量精度:

12位湿度14位温度

默认为0

*******************************************/

ucharSHT11_Read_Register(ucharmand)

{

uchardat;

SHT11_Start();

SHT11_Sendbyte(mand);

SHT11_Answer();

dat=SHT11_Receivebyte();

SHT11_End();

return(dat);

}

/***************************************

函数名称:

SHT11_Measure(ucharmand,uinttime);

函数功能:

设置SHT11检测功能,并返回相应的检测结果

函数说明:

mand形参用于设定温度检测还是湿度检测,

time形参用于设定检测过程中的等待时间,以确定检测结果的位数

11ms/55ms/210ms分别对应8位/12位/14位

****************************************/

uintSHT11_Measure(ucharmand,uchartime)

{

uintdat=0;

uchardata_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=(dat<<8)|data_low;

return(dat);

}

/****************************************

函数名称:

Convert_Tempeture12bit(uintdat);

函数功能:

将检测到的数据转化为相应的温度数据

函数说明:

温度转换公式--T=d1+d2*SOt

公式中的参数d1=-40,d2=0.04

适用于12位测量精度

*/

floatSHT11_Convert_Tempeture12bit(uintdat)

{

floattempeture1;

tempeture1=-40+0.04*dat;

if(tempeture1>23)

tempeture1=tempeture1+1;

if(tempeture1>55)

tempeture1=tempeture1+1;

if(P37==1)

{

if(tempeture1>=16&&tempeture1<30)

{

P33=1;

P32=0;

}

else

{

P33=0;

P32=1;

}

}

return(tempeture1);

}

/*****************************************

函数名称:

SHT11_Convert_Humidity8bit(uintdat,floattemp)

函数功能:

将检测到的数据转化为相应的湿度数据

函数说明:

相对湿度转换公式-----RHline=C1+C2*SOrh+C3*SOrh*SOrh(检测数据的线性化SOrh为单片机接收到的数据)

-----RHtrue=(tempeture-25)*(t1+t2*SOrh)+RHline

公式中的参数:

C1=-4,C2=0,648,C3=-0.00072

t1=0.01,t2=0.00128

适用于8位测量精度

*/

uintSHT11_Convert_Humidity8bit(uintdat,floattemp)

{

floatRHline,RHtrue;

uintr;

RHline=-4+0.648*dat-0.00072*dat*dat;

RHtrue=(temp-25)*(0.01+0.00128*dat)+RHline;

r=(RHtrue-3)*10+0.5;

if(P37==0)

{

if(r>=400&&r<600)

{

P33=1;

}

else

{

P33=0;

}

if(r>=600)

{

P32=1;

}

else

{

P32=0;

}

}

return(r);

}

#endif

Display.c文件:

#include

#include"SHT11.h"

#defineucharunsignedchar

#defineuintunsignedint

#defineTEM_TEST0x03//温度检测命令

#defineHUM_TEST0x05//湿度检测命令

#defineREG_READ0x07//读寄存器

#defineREG_WRITE0x06//写寄存器

#defineFUNCTION_SET0x01//设置SHT11的工作精度为8位/湿度12位温度

ucharDispData[4]={0,1,2,3};

codeucharDispSegmentP0[10]={0x3f,0x06,0x1b,0x0f,0x26,0x2d,0x3d,0x07,0x3f,0x2f};

codeucharDispSegmentP2[10]={0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x00,0x22,0x22};

//0123456789%

codeucharDispCtrl[4]={0xef,0xdF,0xbf,0x7F};

sbitP16=P1^6;

sbitP26=P2^6;

sbitP34=P3^4;

voidTemp_delay(unsignedintj)

{

uchari;

for(i=100;i>0;i--)

{

for(j;j>0;j--);

}

}

voidShow(uchar*Buffer)

{

uchari;

for(i=0;i<4;i++)

{

P1=DispCtrl[i];

P0=DispSegmentP0[*Buffer];

P2=DispSegmentP2[*Buffer];

if(P16==0)

P26=1;

Temp_delay

(2);

Buffer++;

}

}

voidDispConvert(uchar*DispAddr,uintTemp2Con)

{

DispAddr[0]=Temp2Con/1000;

DispAddr[1]=(Temp2Con-DispAddr[0]*1000)/100;

DispAddr[2]=(Temp2Con-DispAddr[0]*1000-DispAddr[1]*100)/10;

DispAddr[3]=Temp2Con-DispAddr[0]*1000-DispAddr[1]*100-DispAddr[2]*10;

}

voidmain()

{

uinttemp;

uintdat;

uintHum;

//floatf;

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@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2