#include "ioCC2530.h"
#include "sht11.h"
/*******************************************
函数名称:void Delay(unsigned char n)
功能描述:延时 n us
参数 :n --------延时的时间长度
返回值:无
********************************************/
void Delay(unsigned char n) //delay n us
{
unsigned char i;
unsigned int j;
for(i = 0; i < n; i++)
for(j = 0; j < 150; j++);
}
/******************************************************************
函数名称:char s_write_byte(unsigned char value)
功能描述:主机向SHT11中写入一个字节的数据,并带有从机的应答
参数 :value --------写入的数据
返回值:error-------error=0,应答成功;error=1,应答失败
*****************************************************************/
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
SHT11_DATA_OUT();
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value) SHT11_DATA=1; //masking value2 with i , write to SENSI-BUS
else SHT11_DATA=0;
SHT11_CLK=1; //clk for SENSI-BUS
Delay(5); //pulswith approx. 5 us
SHT11_CLK=0;
Delay(1);
}
SHT11_DATA=1; //release SHT11_DATA-line
SHT11_DATA_IN();
SHT11_CLK=1; //clk #9 for ack
error=SHT11_DATA; //check ack (SHT11_DATA will be pulled down by SHT11)
SHT11_CLK=0;
return error; //error=1 in case of no acknowledge
}
/******************************************************************
函数名称:char s_read_byte(unsigned char ack)
功能描述:主机从SHT11中读取一个字节的数据,并带有主机给从机的应答
参数 :ack --------ack=1,非应答;ack=0;应答
返回值:val-------将读取到的数据返回
*****************************************************************/
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;
SHT11_DATA_OUT();
SHT11_DATA=1; //release SHT11_DATA-line
SHT11_DATA_IN();
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SHT11_CLK=1;
Delay(5); //clk for SENSI-BUS
if (SHT11_DATA) val=(val | i); //read bit
else val = (val | 0);
SHT11_CLK=0;
Delay(1);
}
SHT11_DATA_OUT();
SHT11_DATA=!ack; //in case of "ack==1" pull down SHT11_DATA-Line
SHT11_CLK=1; //clk #9 for ack
Delay(5); //pulswith approx. 5 us
SHT11_CLK=0;
SHT11_DATA=1; //release SHT11_DATA-line
return val;
}
/******************************************************************
函数名称:void s_transstart(void)
功能描述:主机给从机的启动信号
参数 :无
返回值:无
*****************************************************************/
void s_transstart(void)
{
SHT11_DATA_OUT();
SHT11_DATA=1;
SHT11_CLK=0; //Initial state
Delay(5);
SHT11_CLK=1;
Delay(2);
SHT11_DATA=0;
Delay(3);
SHT11_CLK=0;
Delay(5);
SHT11_CLK=1;
Delay(3);
SHT11_DATA=1;
Delay(2);
SHT11_CLK=0;
}
/******************************************************************
函数名称:s_connectionreset(void)
功能描述:连接复位:第九个脉冲之后(包含第九个)
参数 :无
返回值:无
*****************************************************************/
void s_connectionreset(void)
{
unsigned char i;
SHT11_DATA_OUT();
SHT11_DATA=1;
SHT11_CLK=0; //Initial state
Delay(2);
for(i=0;i<9;i++) //9 SHT11_CLK cycles
{
SHT11_CLK=1;
SHT11_CLK=0;
}
s_transstart(); //transmission start
}
/******************************************************************
函数名称:char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
功能描述:测量温湿度
参数 :*p_value--------存放温湿度值得缓冲区
*p_checksum---------------校验位
mode---------------测量温度/湿度的操作选项
返回值:error---------error=0,测量成功;否则失败
*****************************************************************/
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
char error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode)
{ //send command to sensor
case 0 : error+=s_write_byte(MEASURE_TEMP); break;
case 1 : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++) {
Delay(5);
if(SHT11_DATA==0) break; //wait until sensor has finished the measurement
}
if(SHT11_DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value+1) =s_read_byte(ACK1); //read the first byte (MSB)
*(p_value)=s_read_byte(ACK1); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK1); //read checksum
return error;
}
/******************************************************************
函数名称:s_connectionreset(void)void calc_sht11(float *p_humidity ,float *p_temperature)
功能描述:将得出的温湿度数据转换为实际值
参数 :*p_humidity----------实际湿度值缓冲区
*p_temperature------------实际温度值缓冲区
返回值:无
*****************************************************************/
void calc_sht11(float *p_humidity ,float *p_temperature)
{
const float C1 = -2.0468; //12位湿度精度 修正因子
const float C2 = +0.0367;//12位湿度精度 修正因子
const float C3=-0.0000015955; //12位湿度精度 修正因子
const float T1=+0.01;//14位温度精度 5v条件修正因子
const float T2= +0.00008;//14位温度精度 5v条件修正因子
float rh = *p_humidity;//rh 12位湿度
float t=*p_temperature;//t 14位 温度
float rh_lin;//rh_lin 湿度极限值
float rh_true;//rh_true 湿度真实值
float t_C;//t_C 温度
t_C = t*0.01-40.1;//补偿温度
rh_lin = C3*rh*rh+C2*rh+C1;//相对湿度非线性补偿
rh_true = (t_C-25)*(T1+T2*rh)+rh_lin;//相对湿度对于温度依赖性补偿
if(rh_true>100)rh_true=100;//湿度最大修正
if(rh_true<0.1)rh_true=0.1;//湿度最小修正
*p_temperature = t_C;//返回温度结果
*p_humidity=rh_true;//返回湿度结果
}