//***************************************************************************************
// 函数体定义区
//***************************************************************************************
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned char error=0;
unsigned int i;
s_transstart(); //传输启动
switch(mode) //send command to sensor
{
case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //写命令 测量温度
case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //写命令 测量湿度 error=1 没有回答信号
default : break;
}
P2DIR&=0xfe; //in
for (i=0;i<65535;i++)
if(READ==0)
break; //wait until sensor has finished the measurement
if(READ==1) error+=1; // or timeout (~2 sec.) is reached
else
{
*(p_value) =s_read_byte(ACK); //读高八位 (MSB)
*(p_value+1)=s_read_byte(ACK); //读低八位 (LSB)
*p_checksum =s_read_byte(noACK); //读 校验
}
return error;
}
char s_read_byte(unsigned char ack)
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
P2DIR|=BIT0; //out
SDA_H; //release DATA-line
P2DIR&=0xfe; //in
for (i=0x80;i>0;i/=2) //除法 就是右移
{
SCK_H;
if (READ) val=(val | i); //一次读一位数据
SCK_L;
}
// READ=!ack; //in case of "ack==1" pull down DATA-Line
P2DIR|=BIT0;
if(ack)
{
SDA_L; //?????????????????????????????
}
else
{
SDA_H;
}
_nop_();
SCK_H; //clk #9 for ack
_nop_(); // 5 us
SCK_L;
SDA_H; //拉高
return val;
}
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
SDA_H;
SCK_L; // transmission start 传输启动
_nop_();
SCK_H;
_nop_();
SDA_L;
_nop_();
SCK_L;
_nop_();
SCK_H;
_nop_();
SDA_H;
_nop_();
SCK_L;
_nop_();
}
//--------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature) //数据处理
{
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
t_C=t*0.01 - 40.0; //calc. temperature from ticks to
rh_lin=C3*rh*rh + C2*rh + C1; //计算相对湿度
rh_true=(t_C-25.0)*(T1+T2*rh)+rh_lin; //温度补偿后的数据
if(rh_true>100.0)rh_true=100.0; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.10; //the physical possi××e range
*p_temperature=t_C; //返回温度值
*p_humidity=rh_true; //返回湿度值
}
//--------------------------------------------------------------------------------------
char s_write_byte(unsigned char value)// 写字节函数
{
unsigned char i,error=0;