//------------------------------------------------------------------------------
// Module hygrometre-thermometre sensirion sht11
//
// adaptations JYP 2003
//
//------------------------------------------------------------------------------
#include "math.h"
typedef union
{ unsigned int i;
float f;
} value;
enum {TEMP,HUMI};
#define sht_noACK 0
#define sht_ACK 1
//adr command r/w
#define sht_STATUS_REG_W 0x06 //000 0011 0
#define sht_STATUS_REG_R 0x07 //000 0011 1
#define sht_MEASURE_TEMP 0x03 //000 0001 1
#define sht_MEASURE_HUMI 0x05 //000 0010 1
#define sht_RESET 0x1e //000 1111 0
//------------------------------------------------------------------------------
char sht11_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value)
output_high(sht11_data); //masking value with i , write to SENSI-BUS
else
output_low(sht11_data);
output_high(sht11_sck); //clk for SENSI-BUS
delay_us( 5); //pulswith approx. 5 us
output_low(sht11_sck);
}
output_high(sht11_data); //release DATA-line
output_high(sht11_sck); //clk #9 for ack
error=input(sht11_data); //check ack (DATA will be pulled down by SHT11)
output_low(sht11_sck);
return error; //error=1 in case of no acknowledge
}
//------------------------------------------------------------------------------
char sht11_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;
output_high(sht11_data); //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
output_high(sht11_sck); //clk for SENSI-BUS
if (input(sht11_data)==1)
val=(val | i); //read bit
output_low(sht11_sck);
}
output_bit(sht11_data,!ack); //in case of "ack==1" pull down DATA-Line
output_high(sht11_sck); //clk #9 for ack
delay_us( 5); //pulswith approx. 5 us
output_low(sht11_sck);
output_high(sht11_data); //release DATA-line
return val;
}
//------------------------------------------------------------------------------
void sht11_transstart(void)
//------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
output_high(sht11_data);
output_low(sht11_sck); //Initial state
delay_us( 1);
output_high(sht11_sck);
delay_us( 1);
output_low(sht11_data);
delay_us( 1);
output_low(sht11_sck);
delay_us( 3);
output_high(sht11_sck);
delay_us( 1);
output_high(sht11_data);
delay_us( 1);
output_low(sht11_sck);
}
//------------------------------------------------------------------------------
void sht11_connectionreset(void)
//------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles
// followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
output_high(sht11_data);
output_low(sht11_sck); //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
output_high(sht11_sck);
output_low(sht11_sck);
}
sht11_transstart(); //transmission start
}
//------------------------------------------------------------------------------
char sht11_softreset(void)
//------------------------------------------------------------------------------
// resets the sensor by a softreset
{
unsigned char error=0;
sht11_connectionreset(); //reset communication
error+=sht11_write_byte(sht_RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//------------------------------------------------------------------------------
char sht11_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
sht11_transstart(); //transmission start
error=sht11_write_byte(sht_STATUS_REG_R); //send command to sensor
*p_value=sht11_read_byte(sht_ACK); //read status register (8-bit)
*p_checksum=sht11_read_byte(sht_noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//------------------------------------------------------------------------------
char sht11_write_statusreg(unsigned char *p_value)
//------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
sht11_transstart(); //transmission start
error+=sht11_write_byte(sht_STATUS_REG_W);//send command to sensor
error+=sht11_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//------------------------------------------------------------------------------
char sht11_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
sht11_transstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP : error+=sht11_write_byte(sht_MEASURE_TEMP); break;
case HUMI : error+=sht11_write_byte(sht_MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++)
if(input(sht11_data)==0)
break; //wait until sensor has finished the measurement
if(input(sht11_data)==1)
error+=1; // or timeout (~2 sec.) is reached
*(p_value+1) =sht11_read_byte(sht_ACK); //read the first byte (MSB)
*(p_value)=sht11_read_byte(sht_ACK); //read the second byte (LSB)
*p_checksum =sht11_read_byte(sht_noACK); //read checksum
return error;
}
char sht11_measure_temp(unsigned char *p_value, unsigned char *p_checksum)
{
return sht11_measure( p_value, p_checksum, TEMP);
}
char sht11_measure_humi(unsigned char *p_value, unsigned char *p_checksum)
{
return sht11_measure( p_value, p_checksum, HUMI);
}
//------------------------------------------------------------------------------
void sth11_calc(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,t,rh_lin,rh_true,t_C;
// rh_lin: Humidity linear
// rh_true: Temperature compensated humidity
// t_C : Temperature [°C]
rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
t=*p_temperature; // t: Temperature [Ticks] 14 Bit
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]
}
//--------------------------------------------------------------------
int sht11_calc_humid_int( int16 w_humidity)
//--------------------------------------------------------------------
{
// calcul de l'humidite en entier (sans calcul float)
int32 h1,h2;
h1 = ((int32) w_humidity) * ((int32) w_humidity);
h1 = h1 / (int32)1000;
h1 = h1 * (int32)28;
h2 = ((int32) w_humidity) * (int32)405;
h2 = h2 - h1;
h2 = h2 / (int32)1000;
h2 = h2 - (int32)40;
h2 = h2 / (int32)10;
return (h2);
}
//--------------------------------------------------------------------
int sht11_calc_temp_int( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en entier (sans calcul float)
int16 temp1;
temp1 = w_temperature / (int16)100;
temp1 = temp1 - (int16)40;
return (temp1);
}
//--------------------------------------------------------------------
int sht11_calc_temp_frac10( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en fractionnaire 0.X (sans calcul float)
// exemple si t=25.367 ° renvoie 3
int16 temp1;
temp1 = w_temperature / (int16)10;
temp1 = w_temperature - (int16)400;
temp1 = abs(temp1) - ((int16)10 * abs(sht11_calc_temp_int(w_temperature)));
return (temp1);
}
//--------------------------------------------------------------------
int sht11_calc_temp_frac100( int16 w_temperature)
//--------------------------------------------------------------------
{
// calcul de la temperature en fractionnaire 0.XX (sans calcul float)
// exemple si t=25.367 ° renvoie 36
int16 temp1;
temp1 = w_temperature - (int16)4000;
temp1 = abs(temp1) - ((int16)100 * abs(sht11_calc_temp_int(w_temperature)));
return (temp1);
}
//--------------------------------------------------------------------
float sht11_calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [°C]
// output: dew point [°C]
{
float logEx,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;
}