Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with measuring temperature with sensor SHT71

Status
Not open for further replies.

member_tdh

Member level 5
Joined
Feb 6, 2006
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,187
Hi everybody!

At the moment, I'm working with the sensor SHT71 to measure Temperature and Humidity. But it's not work yet.
I'll post my full code and could anyone help me,please???
I'm verry thanks!!!

//================================= SHT7x =============
void comstart(void)
{
DDRA|=0x02; //DATA-line in output
sht_data=1;
sht_clk=0;
#asm("nop");
sht_clk=1;
#asm("nop");
sht_data=0;
#asm("nop");
sht_clk=0;
#asm("nop");
sht_clk=1;
#asm("nop");
sht_data=1;
#asm("nop");
sht_clk=0;
}

void comreset(void)
{
unsigned char i;

DDRA|=0x02; //DATA-line in output
sht_data=1;
sht_clk=0;
for(i=0;i<12;i++) //9 clock cycles
{
sht_clk=1;
#asm("nop");
sht_clk=0;
}
comstart(); //transmission start
HumiError = FALSE;
}

void sht_soft_reset (void)
{
comreset(); //SHT75 communication reset
comwrite(RESET); //send SHT75 reset command
delay_ms(15); //pause 15ms
}

void sht_init (void)
{
// sht_soft_reset(); //reset SHT75
comreset(); //SHT75 communication reset
delay_ms(20); //delay for power-up
}

void comwait(void)
{
unsigned int sht_delay;

sht_data=1;
sht_clk=0;
for(sht_delay=0;sht_delay<30000;sht_delay++) //30ms
{
if(!sht_data) break;
// if((sht_data_in & 0x02)==0) break;
}
}

/*
unsigned char comwrite(unsigned char data)
{
unsigned char error,i;

error=0;
DDRA|=0x02; //DATA-line in output
for(i=0;i<8;i++)
{
if(data&0x80) sht_data=1;
else sht_data=0;
data<<=1;
sht_clk=1;
#asm("nop");
sht_clk=0;
}

//shift in ACK
sht_data=1; //release DATA-line
DDRA&=~0x02; //DATA-line in input
#asm("nop");
sht_clk=1; //clock 9th for ACK
#asm("nop");
error= (sht_data_in & 0x02); //check ack (DATA will be pulled down by SHT11)
#asm("nop");
sht_clk=0;
return error; //error=1 in case no ACK
}
*/

void comwrite(unsigned char data)
{
unsigned char i;

DDRA|=0x02; //DATA-line in output
for(i=0x80;i>0;i/=2)
{
if(data & i) sht_data=1;
else sht_data=0;
sht_clk=1;
#asm("nop");
sht_clk=0;
}

//shift in ACK
// sht_data=1; //release DATA-line
DDRA&=~0x02; //DATA-line in input
#asm("nop");
sht_clk=1; //clock 9th for ACK
if(sht_data_in & 0x02) HumiError=TRUE; //check ack (DATA will be pulled down by SHT11)
sht_clk=0;
}

unsigned int comread(unsigned char ack)
{
unsigned char i;
unsigned int byte=0;

DDRA&=~0x02; //DATA-line in input
for(i=0x80;i>0;i/=2)
{
sht_clk=1;
if(sht_data_in & 0x02) byte|=i; //read bit
sht_clk=0;
}

DDRA|=0x02; //DATA-line in output
sht_data=!ack; //"ack==1" pull down DATA-Line
sht_clk=1;
#asm("nop");
sht_clk=0;
DDRA&=~0x02; //DATA-line in input
return byte;
}

//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
//----------------------------------------------------------------------------------
/*
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;

comstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP:
error+=comwrite(MEASURE_TEMP);
break;

case HUMI:
error+=comwrite(MEASURE_HUMI);
break;

default:
break;
}

DDRA&=0xFD; //DATA-line in input
PORTA|=0x02; //release DATA-line
comwait(); //wait for the measurement to complete
if(sht_data_in) error+=1; //or timeout (~2 sec.) is reached
*(p_value+1) = comread(ACK); //read the first byte (MSB)
*(p_value) = comread(ACK); //read the second byte (LSB)
*p_checksum = comread(noACK); //read checksum
return error;
}
*/

unsigned int s_measure(unsigned char mode)
{
unsigned char msb=0,lsb=0,crc;

comstart(); //transmission start
switch(mode)
{
case TEMP:
comwrite(MEASURE_TEMP);
break;

case HUMI:
comwrite(MEASURE_HUMI);
break;

default:
break;
}

DDRA&=0xFD; //DATA-line in input
PORTA|=0x02; //release DATA-line
comwait(); //wait for the measurement to complete
msb = comread(ACK); //read the first byte (MSB)
lsb = comread(ACK); //read the second byte (LSB)
crc = comread(noACK); //read checksum
// return (((unsigned int)msb<<8)&0xff00 | lsb);
return (msb*256 + lsb);
}

void HumidityGet(float *p_humidity, float *p_temperature)
{
unsigned int humidity=0,temperature=0;

humidity = s_measure(HUMI); //measure humidity
temperature = s_measure(TEMP); //measure temperature

if(HumiError == TRUE) comreset(); //in case of an error: Reset
else
{
*p_humidity=(float)humidity; //converts integer to float
*p_temperature=(float)temperature; //converts integer to float
calc_sth11(p_humidity,p_temperature); //calculate humidity, temperature
}
}

//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity, float *p_temperature)
{
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [°C]

// TC = -40.00 + 0.01 * SHt;
t_C=*p_temperature*0.01 - 40; //calc. temperature from ticks to [°C]

rh_lin=C3*(*p_humidity)*(*p_humidity) + C2*(*p_humidity) + C1; //calc. humidity from ticks to [%RH]

// rh_true = (t_C - 25) * (0.01 + 0.00128 * SOrh) + RH_linear;
rh_true=(t_C-25)*(T1+T2*(*p_humidity)) + 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]
}



#ifndef _SHT7x_INCLUDED_
#define _SHT7x_INCLUDED_

//SHT7x
#define sht_clk PORTA.0
#define sht_data PORTA.1
#define sht_data_in PINA.1

#define noACK 0
#define ACK 1

#define FALSE 0
#define TRUE 1

unsigned char HumiError = FALSE;

//adr command r/w
#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
#define RESET 0x1E //000 1111 0

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

void comstart(void);
void comreset(void);
void sht_init(void);
void sht_soft_reset(void);
void comwait(void);
//unsigned char comwrite(unsigned char data);
void comwrite(unsigned char data);
unsigned int comread(unsigned char ack);
//char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
unsigned int s_measure(unsigned char mode);
void HumidityGet(float *p_humidity ,float *p_temperature);
void calc_sth11(float *p_humidity ,float *p_temperature);

typedef union {
unsigned int i;
float f;
} weather;

enum {TEMP, HUMI};

#endif



//SLAVE 1

/*****************************************************
Chip type : ATmega8535
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
*****************************************************/

#include "mega8535.h"
#include "global.h"
#include "stdio.h"
#include "SHT71.c"
#include "eeprom.h"
#include "bcd.h"

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include "lcd.h"

char lcd_buffer[33];

//============================================================================================
void main(void)
{
float Humidity,Temperature;
//weather humi_val, temp_val;
//unsigned char error, checksum;



InitIO();

DDRA.0 = 1; //SCK is an output
PORTA.0 = 0; //ClockLow
PORTA.1 = 0; //Always Zero
//Toggle DDRA.1 for Data

sht_init();

delay_ms(100); //delay when power turned on

lcd_clear();
lcd_gotoxy(0, 1);
lcd_putsf("Initialize......");
delay_ms(3000);
lcd_clear();


while (1)
{

/*
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
if(error!=0) comreset();
else
{
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i;
calc_sth11(&humi_val.f,&temp_val.f);
lcd_clear();
sprintf(lcd_buffer,"T: %3.1fC",temp_val.f);
lcd_gotoxy(0,0);
lcd_puts(lcd_buffer);
sprintf(lcd_buffer,"H: %3.1f%%",humi_val.f);
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
delay_ms(300);
}
*/

comreset(); //reset sensor
if(HumiError == TRUE) sht_init();
HumidityGet(&Humidity, &Temperature);
lcd_clear();
sprintf(lcd_buffer,"T: %5.1fC",Temperature);
lcd_gotoxy(0,0);
lcd_puts(lcd_buffer);
sprintf(lcd_buffer,"H: %5.1f%%",Humidity);
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
delay_ms(300);







};//end while
}//end main


//============================================================================================
void InitIO(void)
{

PORTA=0xFF;
DDRA=0xFF;

PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=P State6=P State5=P State4=P State3=0 State2=0 State1=0 State0=0
PORTC=0xF0;
DDRC=0x0F;

// Port D initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=Out Func0=Out
// State7=1 State6=P State5=P State4=P State3=1 State2=P State1=1 State0=1
PORTD=0xFF;
DDRD=0x8B;


// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
}
 

Re: error reading SHT71

No body help me??? :|

Added after 4 hours 19 minutes:

Hi all!

I'm working on the sensor SHT71 and it working but when i get temperature and humidity to display on lcd. I received:

Temperature = 5.5 C
Humidity = 32.1 %

The Humidity (calc at 12-bit) is correct but the Temperature (calc at 14-bit) is not correct.

Please help me explain it.

Thanks so much!
 

Re: error reading SHT71

Hi!

a few days ago, I have successed with SHT11/71/75.

:D

Good luck day to all member!
 

error reading SHT71

Please tell me the error of your program, i need you help, please!
 

Re: error reading SHT71

Hello, did you solve your problem? :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top