SHT10程序代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*************定义接口******************
P2.6------SCK (SHT10)
P2.7------DATA (SHT10)
*****************************************/ sbit SCK = P2^6; //定义通讯时钟端口
sbit DATA = P2^7; //定义通讯数据端口
;-------------------------------------*/
char s_write_byte(unsigned char value)
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
// calculates dew point // input: humidity [%RH], temperature [℃]
// output: dew point [℃]
{ float logEx,dew_p3;7.5*t/(237.3+t)+(log10(h)-2);
typedef union { unsigned int i;
//定义了两个共用体
float f;
} value; enum {TEMP,HUMI};
//枚举测量温度或湿度
#define noACK 0
//用于判断是否结束通讯
#define ACK 1
//结束数据传输
//adr command r/w
float calc_dewpoint(float h,float t); //计算露点
//char s_softreset(void);
//char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum);
//char s_write_statusreg(unsigned char *p_value);
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
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;
//error=1 in case of no acknowledge
}
/*-------------------------------------;模块名称:s_read_byte();
;功 能:SHT10 读函数
;占用资源:--
;参数说明:--
;创建日期:2012.04.10
;版 本:FV1.0(函数版本 Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/
char s_read_byte(unsigned char ack)
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
温湿度传感器的 SHT10 代码如下: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 功能说明:SHT10 与上位机(串口调试助手)的温湿度显示 KeilC 程序 微处理器:STC89C52 编译环境:Keil uVision V4 作 者:Ding ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;修改日期:--
;修改说明:--
;-------------------------------------*/
void s_connectionreset(void)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//the physical possible range
*p_temperature=t_C;
//return temperature [℃]
*p_humidity=rh_true;
//return humidity[%RH]
}
/*-------------------------------------;模块名称:calc_dewpoint();
char s_read_byte(unsigned char ack); //SHT10 读函数
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿
度函数
void calc_sht10(float *p_humidity ,float *p_temperature);//温湿度补偿
}
//---------------------------------------------------------------------------------char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) //---------------------------------------------------------------------------------// reads the status register with checksum (8-bit) {
#define RESET
0x1e //000 1111 0
/****************定义函数****************/
void s_transstart(void);
//启动传输函数
void s_connectionreset(void);
//连接复位函数
char s_write_byte(unsigned char value);//SHT10 写函数
s_connectionreset();
//reset communication
error+=s_write_byte(RESET);
//send RESET-command to sensor
return error;
//error=1 in case of no response form the sensor
;功 能:露点,空气湿度达到饱和时的温度
;占用资源:--
;参数说明:--
;创建日期:2012.04.10
;版 本:FV1.0(函数版本 Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/
float calc_dewpoint(float h,float t)
float rh=*p_humidity; float t=*p_temperature; float rh_lin; float rh_true; float t_C;
// rh: Humidity [Ticks] 12 Bit
// t:
Temperature [Ticks] 14 Bit
// rh_lin: Humidity linear
;*************************************************************/ #include <reg52.h> //Microcontroller specific library, e.g. port definitions #include <intrins.h> //Keil library (is used for _nop()_ operation) #include <math.h> //Keil library #include <stdio.h> //Keil library
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_();
//
_____________________________________________________
________
// DATA:
|_______|
//
_________
___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
// rh_true: Temperature compensated humidity
// t_C : Temperature [℃]
t_C=t*0.01 - 40;
//calc. temperature from ticks to [℃]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
} /*****************主函数*********************/
//----------------------------------------------------------------------------------
SCK=0;
}
/*-------------------------------------;模块名称:s_connectionreset();
;功 能:连接复位函数
;占用资源:--
;参数说明:--
;创建日期:2012.04.10
;版 本:FV1.0(函数版本 Function Version)
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
unsigned char error=0;
//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 SHT11)
SCK=0;
return error;
/*-------------------------------------;模块名称:s_write_byte();
;功 能:SHT10 写函数
;占用资源:--
;参数说明:--
;创建日期:2012.04.10
;版 本:FV1.0(函数版本 Function Version)
;修改日期:--
;修改说明:--
{
unsigned char i;
DATA=1; SCK=0;
//Initial state
for(i=0;i<9;i++)
//9 SCK cycles
{ SCK=1;
SCK=0;
}
s_transstart();
//transmission start
}
/*
//----------------------------------------------------------------------------------
相关文档
最新文档