📄 sht10.c
字号:
#include"SHT10.h"
#include <reg52.h>
#include<intrins.h>
#include<delay.h>
sbit SCK = P2^0; //define clock interface
sbit DATA = P2^1; //define data interface
typedef union
{ unsigned int i;
float f;
} value;
/********************************************************************
函数名称:transStartSHT10 启动传输
入口参数:无
返回参数:无
*********************************************************************/
void TransStartSHT10(void)
{
DATA=1; SCK=0;
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
/*********************************************************************
函数名称:connectionResetSHT10 重新连接
入口参数:无
返回参数:无
*********************************************************************/
void ConnectionResetSHT10(void)
{
uchar i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
SCK=0;
}
TransStartSHT10(); //transmission start
}
/********************************************************************
函数名称:writeSHT10 写数据
入口参数:value
返回参数:error (0成功/1失败)
********************************************************************/
char WriteSHT10(uchar value)
{
uchar i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by DHT90),DATA在第9个上升沿将被DHT90自动下拉为低电平。
_nop_();_nop_();_nop_();
SCK=0;
DATA=1; //release DATA-line
return error; //error=1 in case of no acknowledge //返回:0成功,1失败
}
/********************************************************************
函数名称:readSHT10 读数据
入口参数:ack
返回参数:val(ack=0时读数据,返回val)
*********************************************************************/
char ReadSHT10(uchar ack)
{
uchar i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for SENSI-BUS
if (DATA) val=(val | i); //read bit
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
if(ack==1)DATA=0; //in case of "ack==1" pull down DATA-Line
else DATA=1; //如果是校验(ack==0),读取完后结束通讯
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
DATA=1; //release DATA-line
return val;
}
/*********************************************************************
函数名称:measureSHT10 测量温度
入口参数:*p_value,*p_checksum,mode
返回参数:error(0成功/1失败)
*********************************************************************/
char MeasureSHT10(uchar *p_value, uchar *p_checksum, uchar mode)
{
unsigned error=0;
uchar i;
TransStartSHT10(); //transmission start
switch(mode){ //send command to sensor
case 01 : error+=WriteSHT10(MEASURE_TEMP); break;
case 02 : error+=WriteSHT10(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<110;i++) {delay(2);if(DATA==0) break;} //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =ReadSHT10(ACK); //read the first byte (MSB)
*(p_value+1)=ReadSHT10(ACK); //read the second byte (LSB)
*p_checksum =ReadSHT10(noACK); //read checksum
return error;
}
/********************************************************************
函数名称: calcSHT10 计算温度和相对湿度值
入口参数:*p_humidity ,*p_temperature
返回参数:无
********************************************************************/
void CalcSHT10(float *p_humidity ,float *p_temperature)
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
{ const float C1=-4.0; // for 12 Bit
const float C2=0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=0.01; // for 14 Bit @ 5V
const float T2=0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [C]
t_C=t*0.01 - 40; //calc. temperature from ticks to [C]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [C]
*p_humidity=rh_true; //return humidity[%RH]
}
/********************************************************************
函数名称: getSHT10()
入口参数:float temp,float humi
返回参数:无
********************************************************************/
void GetSHT10(float *temp,float *humi)
{
value humi_val,temp_val;
uchar error,checksum;
error=0;
error+=MeasureSHT10((uchar*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=MeasureSHT10((uchar*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) ConnectionResetSHT10(); //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
CalcSHT10(&humi_val.f,&temp_val.f); //calculate humidity, temperature
*temp=temp_val.f;
*humi=humi_val.f;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -