amitumi1003
Newbie level 3
sht11 problem
the prob is it alwayes show the same value even i change the data or sck code no.
the result is alwayes 37.22 4.25 64.13 .1.
pls help me.........
here is the code
pls help[/url]
the prob is it alwayes show the same value even i change the data or sck code no.
the result is alwayes 37.22 4.25 64.13 .1.
pls help me.........
here is the code
Code:
#include<18F8720.h>
#include <math.h>
#include<stdlib.h>
typedef union
{
unsigned int i;
float f;
} value;
#byte PORT_G =0x00FD
#byte PORT_f =0x0000
#byte PORT_c =0x00BF
#byte PORT_D = 0x0000
#byte PORT_E = 0x0002
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600,rcv=PIN_G2, xmit=PIN_G1, STREAM = COMM_B)
enum {TEMP,HUMI};
#define sht11_data PIN_F2
#define sht11_sck PIN_F3
#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
/********************************************************************************/
/********************************************************************************/
//Send the calculated data to the pc
/********************************************************************************/
/********************************************************************************/
void sendDataToPC(char *data,int len)
{
int i;
output_high(PIN_G0);
delay_ms(10);//wait
for(i=0;i<len;i++)
{
fprintf(COMM_B,"%c",*data);// write in the file
data++;
}
delay_ms(10); //wait
output_low(PIN_G0);
delay_ms(10);
}
int checkLength(int value)
{
int len=0;
while(value!=0)
{
value=value/10;
len++;
}
return len;
}
void floatToInt(double value)
{
int a,rem,len=0,lent;
char arr[5];
char *data,temp;
double d=100.0;
a=abs(value);
len=checkLength(a);
lent=len;
while(a!=0)
{
rem=a%10;
arr[len-1]=rem+48;
a=a/10;
len--;
}
data=arr;
sendDataToPC(data,lent);
temp='.';
sendDataToPC(&temp,1);
a=abs(value);
value=value-a;
value=value*d;
a=abs(value)+1;
len=checkLength(a);
lent=len;
while(a!=0)
{
rem=a%10;
arr[len-1]=rem+48;
a=a/10;
len--;
}
data=arr;
sendDataToPC(data,lent);
temp=' ';
sendDataToPC(&temp,2);
}
/*******************************************************************************/
/*******************************************************************************/
// writes a byte on the Sensibus and checks the acknowledge
/*******************************************************************************/
/*******************************************************************************/
char sht11_write_byte(unsigned char value)
{
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
}
/********************************************************************************/
/********************************************************************************/
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
/********************************************************************************/
/********************************************************************************/
char sht11_read_byte(unsigned char ack)
{
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;
}
/********************************************************************************/
/********************************************************************************/
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
/********************************************************************************/
/********************************************************************************/
void sht11_transstart(void)
{
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);
}
/********************************************************************************/
/********************************************************************************/
// communication reset: DATA-line=1 and at least 9 SCK cycles
// followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
/********************************************************************************/
/********************************************************************************/
void sht11_connectionreset(void)
{
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
}
/********************************************************************************/
/********************************************************************************/
// resets the sensor by a softreset
/********************************************************************************/
/********************************************************************************/
char sht11_softreset(void)
{
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
}
/********************************************************************************/
/********************************************************************************/
// reads the status register with checksum (8-bit)
/********************************************************************************/
/********************************************************************************/
char sht11_read_statusreg()
{
unsigned char error=0;
unsigned char p_value,p_checksum;
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
}
/********************************************************************************/
/********************************************************************************/
// writes the status register with checksum (8-bit)
/********************************************************************************/
/********************************************************************************/
char sht11_write_statusreg(unsigned char *p_value)
{
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
}
/********************************************************************************/
/********************************************************************************/
// makes a measurement (humidity/temperature) with checksum
/********************************************************************************/
/********************************************************************************/
char sht11_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
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);
}
/********************************************************************************/
/********************************************************************************/
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
/********************************************************************************/
/********************************************************************************/
void sth11_calc(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,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]
}
/********************************************************************************/
/********************************************************************************/
// calcul de l'humidite en entier (sans calcul float)
/********************************************************************************/
/********************************************************************************/
int sht11_calc_humid_int( int16 w_humidity)
{
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);
}
/********************************************************************************/
/********************************************************************************/
// calculates dew point
// input: humidity [%RH], temperature [°C]
// output: dew point [°C]
/********************************************************************************/
/********************************************************************************/
float sht11_calc_dewpoint(float h,float t)
{
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;
}
/*********************************************************************************/
/*********************************************************************************/
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [.C]
// 4. calculate dew point [.C]
// 5. print temperature, humidity, dew point
/**********************************************************************************/
/**********************************************************************************/
void main()
{
value humi_val,temp_val;
float dew_point;
unsigned char error,checksum,temp;
sht11_connectionreset();
while(1)
{
error=0;
error+=sht11_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=sht11_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0)
sht11_connectionreset(); //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
sth11_calc(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=sht11_calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
//send final data to serial interface (UART)
//printf(“temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n”,temp_val.f,humi_val.f,dew_point);
//sendDataToPC(temp_val.f,0x04);
//sendDataToPC(humi_val.f,0x04);
//sendDataToPC(dew_point,0x04);*/
floatToInt(temp_val.f);
floatToInt(humi_val.f);
floatToInt(dew_point);
floatToInt(error);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
delay_ms(1000);
temp='\r';
sendDataToPC(&temp,1);
//temp='\n';
//sendDataToPC(&temp,1);
}
}
pls help[/url]