搜档网
当前位置:搜档网 › sht71

sht71

*时钟线:P4.5
*数据线:P4.6
*采用温度14位,湿度12位
*******************************************************************************/
#define SHT_SCK BIT5
#define SHT_DAT BIT6

#define SHT_SCK_OUT P4DIR|=SHT_SCK //时钟线为输出
#define SHT_SCK_1 P4OUT|=SHT_SCK //SHT_SCK=1
#define SHT_SCK_0 P4OUT&=~SHT_SCK //SHT_SCK=0

#define SHT_DAT_OUT P4DIR|=SHT_DAT //数据线为输出
#define SHT_DAT_IN P4DIR&=~SHT_DAT //数据线为输入
#define SHT_DAT_1 P4OUT|=SHT_DAT //SHT_DAT=1
#define SHT_DAT_0 P4OUT&=~SHT_DAT //SHT_DAT=0
#define Read_SHT_Dat (P4IN>>6)&0x01



//SHT传输起始
void SHT_Tra_Sta(void);
//SHT传输重启
void SHT_Tra_Res(void);
//向SHT写入一个字节
uchar SHT_Wri_Byte(uchar value);
//从SHT读入一个字节
uchar SHT_Rea_Byte(uchar ack);
//SHT软件复位
//uchar SHT_Sof_Res(void);
//读状态寄存器
//uchar SHT_Rea_Sta(uchar *p_value, uchar *p_checksum);
//写状态寄存器
//uchar SHT_Wri_Sta(uchar *p_value);
//开始测量
uchar SHT_Mea_TH(uchar *p_value,uchar *p_checksum,uchar mode);
//计算温、湿度值
void SHT_Cal_TH(float *p_humidity,float *p_temperature);
//计算露点
float SHT_Cal_DP(float h,float t);



//SHT传输起始
//------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DAT: |_______|
// ___ ___
// SCK : ___| |___| |______
void SHT_Tra_Sta(void)
{
SHT_SCK_OUT;
SHT_DAT_OUT;
SHT_DAT_1;
nop();
SHT_SCK_0;
nop();
SHT_SCK_1;
nop();
SHT_DAT_0;
nop();
SHT_SCK_0;
nop();
SHT_SCK_1;
nop();
SHT_DAT_1;
nop();
SHT_SCK_0;
nop();
}


//SHT传输重启
//------------------------------------------------------------------------------
// communication reset: DAT_line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DAT: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
void SHT_Tra_Res(void)
{
uchar temp;

SHT_SCK_OUT;
SHT_DAT_OUT;
SHT_DAT_1;
nop();
SHT_SCK_0;
nop();
for(temp=0;temp<9;temp++)
{
SHT_SCK_1;
nop();
SHT_SCK_0;
nop();
}
SHT_Tra_Sta();
}


//向SHT写入一个字节
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
uchar SHT_Wri_Byte(uchar value)
{
uchar temp,error=0;

SHT_SCK_OUT;
SHT_DAT_OUT;
for(temp=0x80;temp>0;temp/=2)
{
if(temp&value) SHT_DAT_1;
else SHT_DAT_0;
nop();
SHT_SCK_1;
nop();
SHT_SCK_0;
nop();
}

//等待响应
//SHT_DAT_OUT;
SHT_DAT_1;
SHT_DAT_IN;
nop();
SHT_SCK_1;
nop();
error=Read_SHT_Dat;
SHT_SCK_0;
nop();
//error=1时无响应
return error;
}


//从SHT读入一个字节
//------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"


uchar SHT_Rea_Byte(uchar ack)
{
uchar temp,val=0,read_temp;

//释放数据线
SHT_SCK_OUT;
SHT_DAT_OUT;
SHT_DAT_1;
nop();

SHT_DAT_IN;
for(temp=0x80;temp>0;temp/=2)
{
SHT_SCK_1;
nop();
read_temp=Read_SHT_Dat;
if(read_temp) val|=temp;
SHT_SCK_0;
nop();
}

//是否停止传输
SHT_DAT_OUT;
//继续
if(ack) SHT_DAT_0;
//停止
else SHT_DAT_1;
SHT_SCK_1;
nop();
SHT_SCK_0;
nop();
SHT_DAT_1;
nop();
return val;
}


//SHT软件复位
//------------------------------------------------------------------------------
// resets the sensor by a softreset
uchar SHT_Sof_Res(void)
{
uchar error=0;

SHT_Tra_Res(); //reset communication
error+=SHT_Wri_Byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}


//读状态寄存器
//------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
uchar SHT_Rea_Sta(uchar *p_value,uchar *p_checksum)
{
uchar error=0;

SHT_Tra_Sta(); //transmission start
error=SHT_Wri_Byte(STATUS_REG_R); //send command to sensor
*p_value=SHT_Rea_Byte(SHT_ACK); //read status register (8-bit)
*p_checksum=SHT_Rea_Byte(SHT_noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}


//写状态寄存器
//------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
uchar SHT_Wri_Sta(uchar *p_value)
{
uchar error=0;

SHT_Tra_Sta(); //transmission start
error+=SHT_Wri_Byte(STATUS_REG_W); //send command to sensor
error+=SHT_Wri_Byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}


//开始测量
//------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
uchar SHT_Mea_TH(uchar *p_value,uchar *p_checksum,uchar mode)
{
uchar error=0,read_temp;
ulong temp;

//transmission start
SHT_Tra_Sta();
//send command to sensor
switch(mode)
{
case 0:error+=SHT_Wri_Byte(MEASURE_TEMP);break;
case 1:error+=SHT_Wri_Byte(MEASURE_HUMI);break;
default:break;
}

/*准备接收*/
//数据线为输出
SHT_DAT_OUT;
SHT_DAT_1;
//数据线为输入
SHT_DAT_IN;
/*wait until sensor has finished the measurement or timeout (~2 sec.) is reached*/
for(temp=0;temp<4294967295;temp++)
{
read_temp=Read_SHT_Dat;
if(read_temp==0) break;
}
read_temp=Read_SHT_Dat;
if(read_temp) error+=1;
//read the first byte (MSB)
*(p_value+1)=SHT_Rea_Byte(SHT_ACK);
//read the second byte (LSB)
*(p_value)=SHT_Rea_Byte(SHT_ACK);
//read checksum
*p_checksum=SHT_Rea_Byte(SHT_noACK);
return error;
}


//计算温、湿度值
//------------------------------------------------------------------------------
// calculates temperature [℃] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ti

cks] (14 bit)
// output: humi [%RH]
// temp [℃]
void SHT_Cal_TH(float *p_humidity,float *p_temperature)
{
//for 12 Bit
static float C1=-4.0;
//for 12 Bit
static float C2=+0.0405;
//for 12 Bit
static float C3=-0.0000028;
static float T1=+0.01;
static float T2=+0.00008;
//rh:Humidity [Ticks] 12 Bit
float rh=*p_humidity;
//t:Temperature [Ticks] 14 Bit
float t=*p_temperature;
//rh_lin:Humidity linear
float rh_lin;
//rh_true:Temperature compensated humidity
float rh_true;
//t_C:Temperature [℃]
float t_C;

//calc. temperature from ticks to [c]
t_C=t*0.01-39.64;
//calc. humidity from ticks to [%RH]
rh_lin=C3*rh*rh+C2*rh+C1;
//calc. temperature compensated humidity [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;
/*cut if the value is outside of the physical possi××e range*/
if(rh_true>100) rh_true=100;
if(rh_true<0.1) rh_true=0.1;

//return temperature [℃]
*p_temperature=t_C;
//return humidity[%RH]
*p_humidity=rh_true;
}


//计算露点
//------------------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [℃]
// output: dew point [℃]
float SHT_Cal_DP(float h,float t)
{
float logEx;
float 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;
}

相关主题