SHT11程序代码.docx
《SHT11程序代码.docx》由会员分享,可在线阅读,更多相关《SHT11程序代码.docx(13页珍藏版)》请在冰豆网上搜索。
SHT11程序代码
SHT11程序代码
#ifndef__SHT11_H__
#define__SHT11_H__
/*************************
SHT11相关命令
**************************/
#defineTEM_TEST0x03//温度检测命令
#defineHUM_TEST0x05//湿度检测命令
#defineREG_READ0x07//读寄存器
#defineREG_WRITE0x06//写寄存器
#defineFUNCTION_SET0x01//设置SHT11的工作精度为8位/湿度12位温度
/**************************
SHT11端口定义
***************************/
sbitSHT11_DATA=P2^0;
sbitSHT11_SCK=P2^2;
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)
{
uinti,j;
for(i=ms;i>0;i--)
for(j=112;j>0;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;
}
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(ucharcommand,uchardat)
函数说明:
向SHT11的状态寄存器设置功能
command为REG_WRITE0x06写寄存器
dat为设置SHT11的功能可以设置检测的数据位数
***************************************/
voidSHT11_Write_Register(ucharcommand,uchardat)
{
SHT11_Start();
SHT11_Sendbyte(command);
SHT11_Answer();
SHT11_Sendbyte(dat);
SHT11_Answer();
}
/***************************************
函数名称:
ucharSHT11_Read_Register(ucharcommand)
函数说明:
command为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(ucharcommand)
{
uchardat;
SHT11_Start();
SHT11_Sendbyte(command);
SHT11_Answer();
dat=SHT11_Receivebyte();
SHT11_End();
return(dat);
}
/***************************************
函数名称:
SHT11_Measure(ucharcommand,uinttime);
函数功能:
设置SHT11检测功能,并返回相应的检测结果
函数说明:
command形参用于设定温度检测还是湿度检测,
time形参用于设定检测过程中的等待时间,以确定检测结果的位数
11ms/55ms/210ms分别对应8位/12位/14位
****************************************/
uintSHT11_Measure(ucharcommand,uchartime)
{
uintdat=0;
uchardata_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=(dat<<8)|data_low;
return(dat);
}
/****************************************
函数名称:
Convert_Tempeture(uintdat);
函数功能:
将检测到的数据转化为相应的温度数据
函数说明:
温度转换公式--T=d1+d2*SOt
公式中的参数d1=-40,d2=0.01
适用于14位测量精度
*****************************************/
floatSHT11_Convert_Tempeture14bit(uintdat)
{
floattempeture1;
tempeture1=-40+0.01*dat;
if(tempeture1>100.0)
{
flag_tempeture=1;
}
elseif(tempeture1<0.0)
{
flag_tempeture=1;
}
else
{
flag_tempeture=0;
}
return(tempeture1);
}
/*****************************************
函数名称:
SHT11_Convert_Humidity(uintdat,floattemp)
函数功能:
将检测到的数据转化为相应的湿度数据
函数说明:
相对湿度转换公式-----RHline=C1+C2*SOrh+C3*SOrh*SOrh(检测数据的线性化SOrh为单片机接收到的数据)
-----RHtrue=(tempeture-25)*(t1+t2*SOrh)+RHline
公式中的参数:
C1=-4,C2=0,0405,C3=-0.0000028
t1=0.01,t2=0.00008
适用于12位测量精度
******************************************/
floatSHT11_Convert_Humidity12bit(uintdat,floattemp)
{
floatRHline,RHtrue;
RHline=-4+0.0405*dat-0.0000028*dat*dat;
RHtrue=(temp-25)*(0.01+0.00008*dat)+RHline;
if(RHtrue<10.0)
{
flag_humidity=1;
}
else
{
flag_humidity=0;
}
return(RHtrue);
}
/****************************************
函数名称:
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>100.0)
{
flag_tempeture=1;
}
else
{
flag_tempeture=0;
}
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
适用于12位测量精度
floatSHT11_Convert_Humidity8bit(uintdat,floattemp)
{
floatRHline,RHtrue;
RHline=-4+0.648*dat-0.00072*dat*dat;
RHtrue=(temp-25)*(0.01+0.00128*dat)+RHline;
if(RHtrue<10.0)
{
flag_humidity=1;
}
else
{
flag_humidity=0;
}
return(RHtrue);
}
******************************************/
#endif