在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
芯片精品文章合集(500篇!)    创芯人才网--重磅上线啦!
查看: 14489|回复: 9

[原创] 基于C51单片机的温湿度传感器SHT11的驱动程序

[复制链接]
发表于 2011-4-19 18:54:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
SHT11.h
#ifndef _SHT11_h_
#define _SHT11_h_
#define uchar unsigned char
#define uint unsigned int
#define NOP() _nop_()
#define TEMP 0
#define HUMI 1
sbit DQ=P2^7; //端口定义
sbit SCK=P2^6;
typedef struct
{
float temperature;
float humidity;
float crc_temp;
float crc_humi;
uchar num_temp[5];
uchar num_humi[5];
}SHT_dat;
/*=========================================
启动函数
=========================================*/
void init_SHT()
{
DQ=1;
SCK=0;
NOP();
SCK=1;
NOP();
DQ=0;
NOP();
SCK=0;
NOP();
NOP();
NOP();
SCK=1;
NOP();
DQ=1;
NOP();
SCK=0;
}
/*=========================================
字节传送函数
=========================================*/
uchar write_byte(uchar value)
{
uchar i,error=0;
for(i=0x80;i>0;i/=2) //高位先传送
{
if(i & value)DQ=1; //循环相与,结果即为要发送的位
else DQ=0;
SCK=1;
NOP();
NOP();
NOP();
SCK=0;
}
DQ=1; //释放总线
SCK=1;
if(DQ==1) error=1; //检查应答,确认通讯正常
SCK=0;
return error; //error=1,通讯有误
}
/*=========================================
读数据函数
=========================================*/
uchar read_byte(uchar dat)
{
uchar i,val=0;
DQ=1;
for(i=0x80;i>0;i/=2)
{
SCK=1;
if(DQ) val=(val | i);
SCK=0;
}
DQ=dat;
SCK=1;
NOP();
NOP();
NOP();
SCK=0;
DQ=1;
return val;
}
/*=========================================
复位函数
=========================================*/
void reset_SHT()
{
uchar i;
DQ=1;
SCK=0;
for(i=0;i<9;i++) //DATA保持高电平,SCK时钟出发9次复位
{
SCK=1;
NOP();
SCK=0;
}
init_SHT();
}
/*=========================================
发送指令到SHT11执行温度和湿度的测量转换
=========================================*/
convert_SHT(SHT_dat *s,uchar mode)
{
uchar i,ack=0;
uchar valueM,valueL,checksum;
float com;
do{reset_SHT();
switch(mode){
case TEMP: ack=write_byte(0x03);break;
case HUMI: ack=write_byte(0x05);break;
default: break;}
}while(ack==1);
for(i=0;i<65535;i++) //等待测量结束
{
if(DQ==0)break;
} //若长时间数据线DQ没拉低,则说明测量有错误
valueM=read_byte(0); //数据的高字节
valueL=read_byte(0); //数据的低字节
checksum=read_byte(1); //CRC校验码
com=(float)valueM*256+(float)valueL;
if(mode==TEMP)
{
s->temperature=com;
s->crc_temp=(float)checksum;
}
if(mode==HUMI)
{
s->humidity=com;
s->crc_humi=(float)checksum;
}
}
/*=========================================
温度和湿度补偿及输出温度值和相对湿度值
=========================================*/
void caculation_SHT(SHT_dat *s)
{
const float c1=-4.0;
const float c2=+0.0405;
const float c3=-0.0000028; //以上为12位湿度修正公示取值
const float t1=+0.01;
const float t2=+0.00008; //以上为14位温度修正公示取值
float t=s->temperature;
float rh=s->humidity;
float rh_lin;
float rh_ture;
float t_c;
t_c=t * 0.01 - 40; //温度的补偿
rh_lin=c3*rh*rh + c2*rh + c1; //相对湿度非线性补偿
rh_ture=( t_c - 25 ) * ( t1 + t2*rh ) + rh_lin; //相对湿度对于温度依赖性补偿
if( rh_ture > 100 ) rh_ture=100; //相对湿度最大值修正
if( rh_ture < 0.1 ) rh_ture=0.1; //相对湿度最小值修正
if(t_c<0)t_c=0;
s->temperature=t_c; //保存温度补偿后的结果
s->humidity=rh_ture; //保存相对湿度补偿后的结果
}
/*=========================================
计算绝对湿度值
=========================================
float calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
/*=========================================
浮点数数据处理
=========================================*/
void float_convert(SHT_dat *s)
{
float com;
uint dat;
com=s->temperature;
com*=100;
dat=(uint)com;
s->num_temp[0]=dat/1000+0x30; //十位
s->num_temp[1]=dat%1000/100+0x30; //个位
s->num_temp[2]=0x2e;
s->num_temp[3]=dat%100/10+0x30; //小数点第一位
s->num_temp[4]=dat%10+0x30; //小数点第二位
com=s->humidity;
com*=100;
dat=(uint)com;
s->num_humi[0]=dat/1000+0x30; //十位
s->num_humi[1]=dat%1000/100+0x30; //个位
s->num_humi[2]=0x2e;
s->num_humi[3]=dat%100/10+0x30; //小数点第一位
s->num_humi[4]=dat%10+0x30; //小数点第二位
}
#endif
发表于 2011-4-20 21:31:38 | 显示全部楼层
不错,学习啦!!!!
发表于 2011-5-23 16:34:09 | 显示全部楼层
mark,这个不错,sht11用起来方便。
发表于 2011-6-2 14:04:37 | 显示全部楼层
谢谢分享,正在研究这方面内容
发表于 2011-8-28 10:57:50 | 显示全部楼层
感謝您!
感謝您!
感謝您!
用心認真求進步
專業提昇生產力
國家民族一定強
发表于 2011-9-9 13:03:05 | 显示全部楼层
补充一个SHT11交验的程序:
unsigned char code crc_ta_Array[256]={
  0, 49, 98, 83,196,245,166,151,185,136,219,234,125, 76, 31, 46,
67,114, 33, 16,135,182,229,212,250,203,152,169, 62, 15, 92,109,
134,183,228,213, 66,115, 32, 17, 63, 14, 93,108,251,202,153,168,
197,244,167,150,  1, 48, 99, 82,124, 77, 30, 47,184,137,218,235,
61, 12, 95,110,249,200,155,170,132,181,230,215, 64,113, 34, 19,
126, 79, 28, 45,186,139,216,233,199,246,165,148,  3, 50, 97, 80,
187,138,217,232,127, 78, 29, 44,  2, 51, 96, 81,198,247,164,149,
248,201,154,171, 60, 13, 94,111, 65,112, 35, 18,133,180,231,214,
122, 75, 24, 41,190,143,220,237,195,242,161,144,  7, 54,101, 84,
57,  8, 91,106,253,204,159,174,128,177,226,211, 68,117, 38, 23,
252,205,158,175, 56,  9, 90,107, 69,116, 39, 22,129,176,227,210,
191,142,221,236,123, 74, 25, 40,  6, 55,100, 85,194,243,160,145,
71,118, 37, 20,131,178,225,208,254,207,156,173, 58, 11, 88,105,
  4, 53,102, 87,192,241,162,147,189,140,223,238,121, 72, 27, 42,
193,240,163,146,  5, 52,103, 86,120, 73, 26, 43,188,141,222,239,
130,179,224,209, 70,119, 36, 21, 59, 10, 89,104,255,206,157,172};
unsigned char crc8(unsigned char* CRCBytes,int bytelength )
{       //BYTE crc_ta[512];
    unsigned char crc;
        unsigned char tt,i;

    crc = 0;
    while (bytelength != 0)
    {
            bytelength--;
            crc =  crc_ta_Array[*CRCBytes ^ crc];
            CRCBytes++;

    }
    tt = 0;
    for(i=0;i<8;i++)
    {
        tt>>=1;
        if((crc&0x80)==0x80)
        {
            tt|=0x80;
        }
        crc<<=1;
    }
    return tt;
}
发表于 2011-9-9 13:06:20 | 显示全部楼层
另外,如果使用12bit的温度转换精度的话,计算出来的CRC需要异或0xAB才能用,不知道为什么,这个CRC计算程序主要用于程序判断外部是否连接了SHT11,或者进行SHT11的故障判断使用。
发表于 2012-2-24 20:57:52 | 显示全部楼层
太感谢了,一直在找着东西
发表于 2014-12-3 11:17:04 | 显示全部楼层
謝謝樓主分享
发表于 2015-11-23 16:05:16 | 显示全部楼层
基于C51单片机的温湿度传感器SHT11的驱动程序
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-10 21:07 , Processed in 0.029382 second(s), 10 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表