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.

PIC 16f877 and SHT humidity sensor

Status
Not open for further replies.

wolfei

Junior Member level 1
Joined
Mar 15, 2010
Messages
19
Helped
11
Reputation
22
Reaction score
9
Trophy points
1,283
Activity points
1,413
Hello everybody!
I am building for my graduation project a wireless weather station.
I am using pic 16f877 and I am building the project using Mplab.
So far I managed to display the temperature using LM35 and pressure using MPX4115 on a 3310 nokia display
**broken link removed**
For humidity I want to use an SHTxx sensor from sensirion...so I tried building a project for SHT75 and I managed to build it but when it comes to put the temp and humidity on the 3310 display...it doesn't work...I don't know why:|
I also build a project for SHT11 following the instructions on the sensirion website...
But although I managed to build this project too...I still couldn't display the results on the display...
I have attached the two projects and the proteus file
It would mean a lot to me if someone could tell me what am I doing wrong...because in 2 weeks I am supposed to show what I have done.
Thank you very much in advance!
 

I don't have a magic answer for you, but here are some things to think about...

Have you dumped the original raw values that you have read from the sensor?

When ever I work with a new device, I dump the original raw values to RS232, then do some calculations by hand using a calculator.

If my hand calculations seem to be incorrect, then:
A) I'm either doing the calculation wrong, or
B) The data from the sensor is wrong, or
C) I'm not reading the data from the device correctly, or
D) The sensor is not configured correctly, or
E) The device is not wired up correctly at the hardware level, or
F) sensor is broke (so swap with another one).
 
I got this code from someone...it turns out I had a few mistakes in my code...so I modified it...but I still get no results.This is the code I got:

Code:
/*******************************************************************************
*******************************************************************************/

#include "delay.h"
#include "lcd3310.c"
#include <htc.h>
__CONFIG(WDTDIS & PWRTEN & UNPROTECT & BORDIS & LVPDIS & DEBUGEN & WP2 & DUNPROT);
#include <math.h>
#include <stdio.h>

 
enum {TEMP,HUMI};

#define TRIS_DATA TRISB1    //data port condition: input/output
#define TRIS_SCK  TRISB0

#define DATA RB1         //SHT11 DATA pin
#define SCK RB0          //STH11 SCK pin

#define noACK 0
#define ACK 1

//adrress commands from Sensirion   //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

//Constants used for SHT Humidity Measurment
#define C1 -4.0
#define C2 0.0405
#define C3  -0.0000028

//Constants used for SHT Temeperature Measurment (14 Bit)
#define D1 -40.0
#define D2 0.01

// constant use for SHT True Humidity Measurement (12 Bit)
#define T1  0.01
#define T2  0.00008



/************************************************************
// writes a byte on the Sensibus and checks the acknowledge
***************************************************************/
//------------------------------------------------------------------------------
unsigned char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------	
		
{	
   unsigned char i,error=0;
   TRIS_DATA = 0;   //SHT11 WRITE...make DATA-line output
   SCK=0; //initial state
   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     DelayUs(5);
     if (i & value)
         DATA=1; //masking value with i , write to SENSI-BUS
     else
         DATA=0;
     SCK=1; //clk for SENSI-BUS
     
     DelayUs(5);   //pulswith approx. 5 us
     
     SCK=0;
   }

  
   TRIS_DATA = 1;    //SHT11 READ..make DATA-line input
   DelayUs(5);
   SCK=1; //clk #9 for ack
   DelayUs(5);
   error = DATA; //check ack (DATA will be pulled down by SHT11)
   SCK=0;
   return error; //error=1 in case of no acknowledge
}


/****************************************************************************/
// reads a byte from the Sensibus and gives an acknowledge in case of "ack=1"
/****************************************************************************/

unsigned char s_read_byte(unsigned char ack)
{
   unsigned char i,val=0;
   TRIS_DATA = 1;        //SHT11 READ
   //DATA=1; //release DATA-line
   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     DelayUs(5);
     SCK=1; //clk for SENSI-BUS
     if (DATA)
     val=(val | i); //read bit		
     DelayUs(5);
     SCK=0;
   }
   TRIS_DATA = 0;    //make DATA-Line output
   DATA=!ack;         //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack 
   DelayUs(5); //pulswith approx. 5 us
   SCK=0;
   TRIS_DATA=1;
   //DATA=1; //release DATA-line
   return val;
}


//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
{
   TRIS_DATA = 0;   //SHT11 WRITE - DATA_Line output

   DATA=1;
   SCK=0;   //Initial state
   DelayUs(5);
   SCK=1;
   DelayUs(5);
   DATA=0;
   DelayUs(5);
   SCK=0;
   DelayUs(5);
   SCK=1;
   DelayUs(5);
   DATA=1;
   DelayUs(5);
   SCK=0;
}


//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________         ________
// DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
{
   unsigned char i;
   TRIS_DATA = 0;  //SHT11 WRITE
   DATA=1; SCK=0;  //Initial state
   for(i=0;i<12;i++) //12 SCK cycles....more than 9 for safe being
   {
	 SCK=1;
     DelayUs(5);
     SCK=0;
   }
   s_transstart(); //transmission start
}

void initialize(void)
{
	lcd_init();		//initialize 3310LCD
	lcd_clear();	//clear screen	
}

void afisare (int numar, unsigned char linia, unsigned char coloana)
{
	if(numar>99)
	{
		display_digit(linia,coloana,(numar/100)%10);                //sute
		display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 
	}
	else
	{
		lcd_gotoxy(coloana-1,linia-1); lcd_puts("  ");
		lcd_gotoxy(coloana-1,linia); lcd_puts("  "); 
	    display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 	
	}
}

//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 1. Define Ports
// 2. Initalize LCD and reset connection to sensor
// 3. While Loop: calculate humidity [%RH] and temperature []C in 12BIT mod
// 4. print temperature, humidity, dew point

{ 	
	unsigned char error,checksum, byte_1, byte_2;
    int lValue_Temp, lValue_Hum, i;
    int fTemp_true,Rh_lin,fRh_True;
   
 	   //CONFIG1 = 0b1110000011000101;
// 	   OSCCON = 0b01100101;
 //	   OSCTUNE = 0b00001111;
	   TRISD = 0x00;
       TRISA = 0x00;
       TRISB = 0x00;
       TRISC = 0x00;
       INTCON = 0x00; //disable interrupts
  //     ANSEL=0x00; //Digital AN[0:7]
  //     ANSELH = 0x00; //Digital AN[13:8]
       ADCON0=0; //turn off A/D
       
       
 	initialize();
   s_connectionreset();
   DelayUs(20);
  // s_write_statusreg(0x03); //low resolution data
   while(1)
   {
	   
    
    /********************************************************
      Measure Temperature
    ********************************************************/
    
    s_connectionreset();
    s_transstart(); //transmission start
	s_write_byte(MEASURE_TEMP); //measure Temperature
	while (DATA); //wait until SHT11 ends measurements
	//Save highest byte read, in RAM
	byte_1 =  s_read_byte(ACK);
	//Save lowest byte read, in RAM
	byte_2 =  s_read_byte(noACK);
    lValue_Temp = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
    
    
    /********************************************************
      temperature calculation
    ********************************************************/
    
    fTemp_true = (D1+(D2*lValue_Temp)*100);
    
    /********************************************************
     Meausure Humidity
    ********************************************************/
    
    s_connectionreset(); //Reset Sensor BUS
	s_transstart(); //transmission start
	s_write_byte(MEASURE_HUMI); //measure Humidity
	while (DATA); //wait until SHT11 ends measurements
	//Save highest byte read, in RAM
	byte_1 =(unsigned char)  s_read_byte(ACK);
	//Save lowest byte read, in RAM
	byte_2 = (unsigned char) s_read_byte(noACK);
    lValue_Hum = ((unsigned char)byte_1*256) + (unsigned char)byte_2; //combines two bytes (8 bit) into a 16 bit (word)
    
    /********************************************************
      Humidity calculation
    ********************************************************/
    
    Rh_lin = (C1+(C2*lValue_Hum)+(C3*lValue_Hum*lValue_Hum));
    fRh_True = (((fTemp_true-25)*(T1+(T2*lValue_Hum)))+Rh_lin)*100;
    if((fRh_True/100)>100) fRh_True=10000;
    //if((fRh_True/100)<0.1)fRh_True=0.001;
    
    
    
    /********************************************************
      Print to LCD
    ********************************************************/
/*    
    char buffer[20];
    sprintf(buffer, "T=%d.%d C H=%d.%d %%", fTemp_true/100, fTemp_true%100, fRh_True/100, fRh_True%100); 
	lcd_goto(0);	// select first line
	lcd_puts(buffer);
*/
	afisare( fTemp_true, 1, 30);
	afisare(fRh_True, 3, 30);
	


     
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
     for(i=0;i<2000;i++)
        DelayUs(8000);

   }
}

This is my code now:

Code:
//------------------------------------------------------------------------------
// Module hygrometre-thermometre sensirion sht11
//
// adaptations JYP 2003
//
//------------------------------------------------------------------------------

#include "math.h"
#include <pic.h>
#include "delay.h"
typedef union
	{ 	unsigned int i;
	float f;
	} 
	value;

enum {TEMP,HUMI};
#define SHT11_DATA RB1
#define SHT11_SCK RB0

#define SHT11_TRIS_DATA TRISB1
#define SHT11_TRIS_SCK TRISB0

#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
	{
	DelayUs(5);
	if (i & value)
		SHT11_DATA=1; //masking value with i , write to SENSI-BUS
	else
		SHT11_DATA=0;
		SHT11_SCK=1; //clk for SENSI-BUS
		DelayUs( 5); //pulswith approx. 5 us
		SHT11_SCK=0;
	}
SHT11_TRIS_DATA=1; //release DATA-line
DelayUs(5);
SHT11_SCK=1; //clk #9 for ack
error=SHT11_DATA; //check ack (DATA will be pulled down by SHT11)
SHT11_SCK=0;
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;
SHT11_TRIS_DATA=1; //release DATA-line
	for (i=0x80;i>0;i/=2) //shift bit for masking
	{
	DelayUs(5);
	SHT11_SCK=1; //clk for SENSI-BUS
	if (SHT11_DATA)
		val=(val | i); //read bit
	DelayUs(5);
	SHT11_SCK=0;
	}
SHT11_TRIS_DATA=0;   //make DATA-Line output
SHT11_DATA=!sht_ACK; //in case of "ack==1" pull down DATA-Line
SHT11_SCK=1; //clk #9 for ack
DelayUs( 5); //pulswith approx. 5 us
SHT11_SCK=0;
SHT11_TRIS_DATA=1; //release DATA-line
return val;
}

//------------------------------------------------------------------------------
void sht11_transstart(void)
//------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
SHT11_TRIS_DATA = 0;   //SHT11 WRITE - DATA_Line output
SHT11_DATA=1;
SHT11_SCK=0; //Initial state
DelayUs( 5);
SHT11_SCK=1;
DelayUs( 5);
SHT11_DATA=0;
DelayUs( 5);
SHT11_SCK=0;
DelayUs( 5);
SHT11_SCK=1;
DelayUs( 1);
SHT11_DATA=1;
DelayUs( 1);
SHT11_SCK=0;
}

//------------------------------------------------------------------------------
void sht11_connectionreset(void)
//------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles
// followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
SHT11_TRIS_DATA=1;  //SHT11 WRITE
SHT11_DATA=1;
SHT11_SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SHT11_SCK=1;
DelayUs(5);
SHT11_SCK=0;
}
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(SHT11_DATA==0)
			break; //wait until sensor has finished the measurement
	if(SHT11_DATA==1)
		error+=1; // or timeout (~2 sec.) is reached

*(p_value) =sht11_read_byte(sht_ACK); //read the first byte (MSB)
*(p_value+1)=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 sht11_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( int w_humidity)
//--------------------------------------------------------------------
{
// calcul de l'humidite en entier (sans calcul float)

int h1,h2;

h1 = ((int) w_humidity) * ((int) 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;
}

I don't understand what am I doing wrong...
Thanks for the advice with the serial communication, but I have to learn how to do that...I never used that before...I am a newbie..

I get results with the project I got...but they are very strange:
**broken link removed**
 
I did it! This works!
Code:
/*******************************************************************************
*******************************************************************************/




#include "delay.h"
#include "lcd3310.c"
#include <htc.h>
#include "math.h"
#include "stdio.h"
//#include "serial.h"


enum {TEMP,HUMI};


#define TRIS_DATA TRISB1    //data port condition: input/output
#define TRIS_SCK  TRISB0

#define DATA RB1         //SHT11 DATA pin
#define SCK RB0          //STH11 SCK pin

#define noACK 0
#define ACK 1

//adrress commands from Sensirion   //adr ||command || r/w
#define MEASURE_TEMP 0x03           //000    0001       1
#define MEASURE_HUMI 0x05           //000    0010       1


//Constants used for SHT Humidity Measurment
#define C1 -4
#define C2 0.0405
#define C3  -0.0000028

//Constants used for SHT Temeperature Measurment (14 Bit)
#define D1 -40.0
#define D2 0.01

// constant use for SHT True Humidity Measurement (12 Bit)
#define T1  0.01
#define T2  0.00008

//PWM Heat Tape
#define HEAT_ON  RB5=1
#define HEAT_OFF RB5=0


/************************************************************
// writes a byte on the Sensibus and checks the acknowledge
***************************************************************/
//------------------------------------------------------------------------------
unsigned char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------	
		
{
	
	
   unsigned char i,error=0;
   TRIS_DATA = 0;   //SHT11 WRITE...make DATA-line output
   SCK=0; //initial state
   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     DelayUs(20);
     if (i & value)
         DATA=1; //masking value with i , write to SENSI-BUS
     else
         DATA=0;
     SCK=1; //clk for SENSI-BUS
     
     DelayUs(20);   //pulswith approx. 5 us
     
     SCK=0;
   }

  
   TRIS_DATA = 1;    //SHT11 READ..make DATA-line input
   DelayUs(20);
   SCK=1; //clk #9 for ack
   DelayUs(20);
   SCK=0;
   error = DATA; //check ack (DATA will be pulled down by SHT11)
   return error; //error=1 in case of no acknowledge
}


/****************************************************************************/
// reads a byte from the Sensibus and gives an acknowledge in case of "ack=1"
/****************************************************************************/

unsigned char s_read_byte(unsigned char ack)
{
   unsigned char i; int val=0;
   TRIS_DATA = 1;        //SHT11 READ
   for (i=0x80;i>0;i/=2) //shift bit for masking
   {
     DelayUs(20);
     SCK=1; //clk for SENSI-BUS
     if (DATA)
     val=(val | i); //read bit		
     DelayUs(20);
     SCK=0;
   }
   TRIS_DATA = 0;    //make DATA-Line output
   DATA=!ack;         //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack 
   DelayUs(20); //pulswith approx. 5 us
   SCK=0;
   TRIS_DATA=1;
   return val;
}


//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
{
   TRIS_DATA = 0;   //SHT11 WRITE - DATA_Line output

   DATA=1;
   SCK=0;   //Initial state
   DelayUs(20);
   SCK=1;
   DelayUs(20);
   DATA=0;
   DelayUs(20);
   SCK=0;
   DelayUs(20);
   SCK=1;
   DelayUs(20);
   DATA=1;
   DelayUs(20);
   SCK=0;
}


//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________         ________
// DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
{
   unsigned char i;
   TRIS_DATA = 0;  //SHT11 WRITE
   DATA=1; SCK=0;  //Initial state
   for(i=0;i<12;i++) //12 SCK cycles....more than 9 for safe being
   {
	 SCK=1;
     DelayUs(20);
     SCK=0;
   }
   s_transstart(); //transmission start
}


    
    float prev_Temp;
    float prev_Hum;
    float temp_true=23, Rh_lin, RH_true=50,dew_point=10,p;
    float k;
   	
void initialize(void)
{
	lcd_init();		//initialize 3310LCD
	lcd_clear();	//clear screen	
}

void afisare (int numar, unsigned char linia, unsigned char coloana)
{
	if(numar>99)
	{
		display_digit(linia,coloana,(numar/100)%10);                //sute
		display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 
	}
	else
	{
		lcd_gotoxy(coloana-1,linia-1); lcd_puts("  ");
		lcd_gotoxy(coloana-1,linia); lcd_puts("  "); 
	    display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 	
	}
}
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 1. Define Ports
// 2. Initalize LCD and reset connection to sensor
// 3. While Loop: calculate humidity [%RH] and temperature []C in 12BIT mod
// 4. print temperature, humidity, dew point

{ 	
    
    int lValue_Temp, lValue_Hum,i;
    float k;
   	char buffer[20];
    long int time=0;
 	unsigned char error,checksum, byte_1, byte_2,byte_0; 
 	
 	  
 	   
       TRISA = 0x00;
       TRISB = 0x00;
       TRISC = 0x00;
       ADCON0=0; //turn off A/D
   
  	initialize();
    s_connectionreset();
   DelayUs(20);
   while(1)
   {   
	  
   /********************************************************
      DewPoint Calculation
   ********************************************************/
   
	if(temp_true!=NULL){
	k = (log10(RH_true)-2)/0.4343 + (17.62*temp_true)/(243.12+temp_true);
   	//k+= (17.62*temp_true)/(243.12+temp_true);
	dew_point = 243.12*k/(17.62-k);
	}
	  prev_Temp = temp_true;
	   prev_Hum = RH_true;
	
    /********************************************************
      Measure Temperature
    ********************************************************/
   
    s_connectionreset();
	s_write_byte(MEASURE_TEMP); //measure Temperature
	while(DATA);
	//while (DATA); //wait until SHT11 ends measurements
	//Save highest byte read, in RAM
	byte_1 =  s_read_byte(ACK);
	//Save lowest byte read, in RAM
	byte_2 =  s_read_byte(noACK);
    lValue_Temp = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
    
    
    /********************************************************
      temperature calculation
    ********************************************************/
    temp_true = (D1+(D2*lValue_Temp));
    
    /********************************************************
     Meausure Humidity
    ********************************************************/
    
    s_connectionreset(); //Reset Sensor BUS
	s_transstart(); //transmission start
	s_write_byte(MEASURE_HUMI); //measure Humidity
	for(i=0;i<9999;i++)
		{if (DATA==0) break;}
	if (DATA=1) s_connectionreset();
	//while (DATA); //wait until SHT11 ends measurements
	//Save highest byte read, in RAM
	byte_1 =(unsigned char)  s_read_byte(ACK);
	//Save lowest byte read, in RAM
	byte_2 = (unsigned char) s_read_byte(noACK);
    lValue_Hum = (byte_1*256) + byte_2; //combines two bytes (8 bit) into a 16 bit (word)
    
    /********************************************************
      Humidity calculation
    ********************************************************/
    
    Rh_lin = (C1+(C2*lValue_Hum)+(C3*lValue_Hum*lValue_Hum));
    RH_true = (((temp_true-25)*(T1+(T2*lValue_Hum)))+Rh_lin);
    if((RH_true)>100) RH_true=100; //Ceiling for RH measurment
    if((RH_true)<0.1) RH_true=0.1; //Floor for RH measurment
    
    
    /********************************************************
      Print to LCD
    ********************************************************/
/*    
    sprintf(buffer, "T=%.2f C RH=%.2f %%", temp_true, RH_true);
	lcd_goto(0x00);	// select first line
	lcd_puts(buffer);
	sprintf(buffer, "Dew Point= %.2f C", dew_point);
	lcd_goto(0x40);	
	lcd_puts(buffer);
	lcd_goto(0x14);
	sprintf(buffer,"PREV T=%.2f C",prev_Temp);
	lcd_puts(buffer);
	lcd_goto(0x54);
	sprintf(buffer,"PREV RH=%.2f %%",prev_Hum);
	lcd_puts(buffer);
	
    

 /********************************************************
      Print to SERIAL
  ********************************************************
  time+=10;
  sprintf(buffer,"\rT= %.2f   RH= %.2f   Time= %d   ",temp_true,RH_true,time);
  puts(buffer);
    
*/ 

	afisare( temp_true, 1, 30);
	afisare(RH_true, 3, 30);
  
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
        DelayMs(220);
        DelayMs(220);
  
   }
}
 
Here you can submit your work (using LM35 temperature and pressure using the MPX4115 on display nokia 3310)??

It's important for me!

Ricardo Carvalho
 

This is the main part of my program..hope it helps!

Code:
// PIC16F877 + Nokia 3310 LCD
// Hi-Tech C 

#include <pic.h>
__CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGEN & UNPROTECT);
#include "delay.h"
#include "stdlib.h"
#include "lcd3310.c"
#include "SHT11.c"
void initialize(void)
	{
	GIE=0;			// disable interrupts
	ADCS1 = 0;		//select Fosc/8
	ADCS0 = 1;
	ADCON1=0;		// A/D port configuration 0
	ADFM = 1;		//right justified result
	ADON=1;			// turn on the AD conversion module	
	lcd_init();		//initialize 3310LCD
	lcd_clear();	//clear screen	
	}
/* return a 10-bit result */
unsigned int read_adc(unsigned char channel)
{
	channel&=0x07;			// truncate channel to 3 bits
	ADCON0&=0xC5;			// clear current channel select
	ADCON0|=(channel<<3);	// apply the new channel select
	DelayMs(10);
	ADGO=1;					// initiate conversion on the selected channel
	while(ADGO)continue;
	return(((ADRESH&0x03)<<8)+ADRESL);	// return the 10-bit result
}


float mpx4115a_read(void)                // for pressure sensor MPX4115A
{
	int p_an,adc_sample;
	float pres_mmHg;			

		p_an=0;											// reset value
		DelayMs(5);					
		adc_sample = read_adc(1);
		DelayMs(10);

// 10 bits ADC is 1024 steps --> -Vref is 0 V and +Vref is 5 V  
// 760 mmHg = 101.325 kPa  
// Vout = VDD*(0.009*P - 0.095) it's a linear ecuation --> y = mX + b
// m = Prange / ADrange = 100 / 920 and b = Pmax - (m * ADmax) = 115 - (0.1087 * 975)
// P = 0.1087 * ADval + 9.0175

		if (adc_sample>1023)return;				// error detection
		p_an=adc_sample; 						
		pres_mmHg=0.1087*p_an+9.0175;
		return (pres_mmHg);
}
void afisare (int numar, unsigned char linia, unsigned char coloana)
{
	if(numar>99)
	{
		display_digit(linia,coloana,(numar/100)%10);                //sute
		display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 
	}
	else
	{
		lcd_gotoxy(coloana-1,linia-1); lcd_puts("  ");
		lcd_gotoxy(coloana-1,linia); lcd_puts("  "); 
	    display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 	
	}
}

void afisare_float (float numar_float, unsigned char linia, unsigned char coloana)
{
	int numar;
	numar=((int)numar_float);
	
	if(numar>99)
	{
		display_digit(linia,coloana,(numar/100)%10);                //sute
		display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 
	}
	else
	{
		lcd_gotoxy(coloana-1,linia-1); lcd_puts("  ");
		lcd_gotoxy(coloana-1,linia); lcd_puts("  "); 
	    display_digit(linia,coloana+10,(numar/10)%10);                 //zeci
		display_digit(linia,coloana+20,numar%10);                      //unitati 	
	}
}  

 

bit   bAfisare;

void main(void)
{
   int temp,pres;
   float temp_sht, umid_sht, p_roua;
   
   initialize();
   DelayUs(20);
   
   TRISB = 0x01;
   TRISC = 0x00;

   while(1)
   {
      s_connectionreset();
          
       if (RB0)
           { 
                DelayMs(100);    /* soft debouncing*/
              if (RB0) 
                {
                      RC1^=1;       /* inverseaza RD1*/
                      bAfisare^=1; /*inverseaza bAfisare */
                 }
       }
        switch (bAfisare)
        {
             case 0 : lcd_init();
					lcd_clear();
                    lcd_gotoxy(0,0); lcd_puts("T in: ");   
                    lcd_gotoxy(70,0); lcd_puts("o");   
                    lcd_gotoxy(0,3); lcd_puts("T out: ");   
                    lcd_gotoxy(70,2); lcd_puts("o");   
                    temp =read_adc(0)*((5.0*100.0)/1023.0);
                    afisare(temp, 1, 40);
                    DelayMs(10);
                    temp_sht=temperatura_sht();   
                    afisare_float(temp_sht, 3, 40);
                    DelayMs(10);
                    break;

             case 1:
         default : 
					 lcd_init();
					 lcd_clear();
                     lcd_gotoxy(0,0); lcd_puts("Pres: ");   
                     lcd_gotoxy(60,0); lcd_puts("mmHg");
                     pres =mpx4115a_read();
                     DelayUs(250);
                     afisare(pres,1, 30);
                     lcd_gotoxy(0,3); lcd_puts("H: ");   
                     lcd_gotoxy(70,3); lcd_puts("%");
                     umid_sht=umiditate_sht();
                     DelayUs(250);
                     afisare_float(umid_sht, 3, 30);
                     lcd_gotoxy(0,5); lcd_puts("DP: ");   
                     lcd_gotoxy(70,5); lcd_puts("C");
                     p_roua=punct_roua();
                     DelayUs(250);
                     afisare_float(p_roua, 5, 30);
                     DelayMs(10);
                     break;
        } //switch

       DelayUs(250);

   } //while(1)
   
}
 

Hey I have almost same situation I use LM35 to display the temperature on LCD (16*2) for my school project
I have downloaded some source and modified it as my circuit but the problem is I could not understand the logic behind of some function in this program and the temperature value is not real value I think.
I simulate it using Proteus
is anybody can help me ...
I also wanted to use #include<math.h> for using some formula such as
t=round(val*0.48876);//Convert to Degree Celcius
but it gives me error that round is not defined :-|

here is the sample source which I modified it but it does not show anything on LCD :

/==================inckude=================================
#include<pic.h>

//===============configuration==============================
__CONFIG (0x3F32);

//===============define IO port=============================
#define lcd PORTD
#define RS RB1
#define E RB3
#define CHANNEL0 0b10000001 // AN0
#define CHANNEL1 0b10001001 // AN1
//#define buzzer RB5
#define fanA RC1 // this one I added for turn on the fan //


//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data); // this function I can't understand I think for send the value of sensor to display in LCD
void increment(unsigned long data); // this Function I don't understand
void read_adc(void); // this function is for reading value of sensor ***adc reading***
unsigned short read_temp(void); // this function I don't understand the operation

//====================MAIN================================
unsigned short result;
unsigned short temp,tempA,tempB; //introduce 3 variable for sensors

void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result

//setting ADCON1 Register
ADCON1=0b11000000; // A/D result right justified, I modified to ALL AN channels as analoug input
// configure RA2 and RA5 as digital I/O// have changed

TRISA=0b11111111; //configure PORTA I/O direction // I modified portA as inputs for all pins
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output

PORTA=0;
PORTB=0;

while(1)
{
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set

lcd_goto(0); //cursor start from beginning

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('A');
send_char('=');

lcd_goto(20); //cursor go to 2nd line of the LCD

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('B');
send_char('=');

while(1) //infinity loop
{
//sensor A
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
lcd_goto(8);

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempA=temp;

//sensor B
ADCON0=CHANNEL1; //CHANNEL0=0b10000001

lcd_goto(28);

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempB=temp;



delay(2000);



}

}



//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}

void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}

void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}


void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}


void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}


void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;

hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;

if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}

else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}

}

//==================subroutine ADC=========================

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;

}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}
 
here is my circuit
 

Attachments

  • my circuit.PNG
    my circuit.PNG
    39.8 KB · Views: 166
I'm also using the SHT10 sensor. What I got from it is obvious incorrect result.

I've also adapted the code provided by the sensirion website. I'm using it with a PIC18F4550. The other part of my circuitry works just well.

My crystal is 20MHz.

Here is my code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***********************************************************************************
Project:          SHT1x/7x demo program (V2.4)
Filename:         SHT1x_sample_code.c    
 
Prozessor:        80C51 family
Compiler:         Keil Version 6.23a
 
Autor:            MST
Copyrigth:        (c) Sensirion AG      
***********************************************************************************/
// Revisions:
// V2.4  calc_sht11()       Coefficients for humidity and temperature conversion 
//                          changed (for V4 sensors)
//       calc_dewpoint()    New formula for dew point calculation 
 
 
 
#include <htc.h>
#include <math.h>    //Keil library  
#include <stdio.h>   //Keil library
#include "Def.h"
 
typedef union 
{ 
    unsigned int i;
    float f;
} value;
 
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum { SHT_TEMP, SHT_HUMI };
 
#define SHT_DATA        RC6         // I/O
#define SHT_SCK         RC7         // O
#define SHT_DATA_DIR    TRISC6
 
#define SHT_DATA_RELEASE() SHT_DATA_DIR = INPUT_PIN
#define SHT_DATA_OUTPUT() SHT_DATA_DIR = OUTPUT_PIN
 
#define _nop_() asm("nop")
 
 
#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 s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge 
{ 
    unsigned char i,error=0;  
    SHT_DATA_OUTPUT();
    for (i=0x80;i>0;i/=2)             //shift bit for masking
    { 
        if (i & value) SHT_DATA=1;          //masking value with i , write to SENSI-BUS
        else SHT_DATA=0;                        
        _nop_();                        //observe setup time
        SHT_SCK=1;                          //clk for SENSI-BUS
        _nop_();_nop_();_nop_();        //pulswith approx. 5 us     
        SHT_SCK=0;
        _nop_();                         //observe hold time
    }
    SHT_DATA_RELEASE();
    //SHT_DATA=1;                           //release SHT_DATA-line
    _nop_();                          //observe setup time
    SHT_SCK=1;                            //clk #9 for ack 
    error=SHT_DATA;                       //check ack (SHT_DATA will be pulled down by SHT11)
    SHT_SCK=0;        
    return error;                     //error=1 in case of no acknowledge
}
 
//----------------------------------------------------------------------------------
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;
    SHT_DATA_RELEASE();
    //SHT_DATA=1;                           //release SHT_DATA-line
    for (i=0x80;i>0;i/=2)             //shift bit for masking
    {
        SHT_SCK=1;                          //clk for SENSI-BUS
        if (SHT_DATA) val=(val | i);        //read bit  
        SHT_SCK=0;                       
    }
    SHT_DATA_OUTPUT();
    SHT_DATA=!ack;                        //in case of "ack==1" pull down SHT_DATA-Line
    _nop_();                          //observe setup time
    SHT_SCK=1;                            //clk #9 for ack
    _nop_();_nop_();_nop_();          //pulswith approx. 5 us 
    SHT_SCK=0;
    _nop_();                          //observe hold time                           
    SHT_DATA_RELEASE();
    return val;
}
 
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start 
//          _____         ________
// SHT_DATA:     |_______|
//               ___     ___
// SHT_SCK : ___|   |___|   |______
{  
    SHT_DATA_OUTPUT();
    SHT_DATA=1; SHT_SCK=0;                   //Initial state
    _nop_();
    SHT_SCK=1;
    _nop_();
    SHT_DATA=0;
    _nop_();
    SHT_SCK=0;  
    _nop_();_nop_();_nop_();
    SHT_SCK=1;
    _nop_();
    SHT_DATA=1;        
    _nop_();
    SHT_SCK=0;         
}
 
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: SHT_DATA-line=1 and at least 9 SHT_SCK cycles followed by transstart
//       _____________________________________________________         ________
// SHT_DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SHT_SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
{  
    unsigned char i; 
    SHT_DATA_OUTPUT();
    SHT_DATA=1; SHT_SCK=0;                    //Initial state
    for(i=0;i<9;i++)                  //9 SHT_SCK cycles
    { 
        SHT_SCK=1;
        SHT_SCK=0;
    }
    s_transstart();                   //transmission start
}
 
//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset 
{ 
    unsigned char error=0;  
    s_connectionreset();              //reset communication
    error+=s_write_byte(SHT_RESET);       //send SHT_RESET-command to sensor
    return error;                     //error=1 in case of no response form the sensor
}
 
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{ 
    unsigned char error=0;
    s_transstart();                   //transmission start
    error=s_write_byte(SHT_STATUS_REG_R); //send command to sensor
    *p_value=s_read_byte(SHT_ACK);        //read status register (8-bit)
    *p_checksum=s_read_byte(SHT_noACK);   //read checksum (8-bit)  
    return error;                     //error=1 in case of no response form the sensor
}
 
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{ 
    unsigned char error=0;
    s_transstart();                   //transmission start
    error+=s_write_byte(SHT_STATUS_REG_W);//send command to sensor
    error+=s_write_byte(*p_value);    //send value of status register
    return error;                     //error>=1 in case of no response form the sensor
}
 
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{ 
    unsigned char error=0;
    unsigned int i;
 
    s_transstart();                   //transmission start
    switch(mode)
    {                     //send command to sensor
    case SHT_TEMP   : error+=s_write_byte(SHT_MEASURE_TEMP); break;
    case SHT_HUMI   : error+=s_write_byte(SHT_MEASURE_HUMI); break;
    default     : break;     
    }
    SHT_DATA_RELEASE();
    for (i=0;i<65535;i++)
    {
        if(SHT_DATA==0) break; //wait until sensor has finished the measurement
    }
    if(SHT_DATA) error+=1;                // or timeout (~2 sec.) is reached
    *(p_value)  =s_read_byte(SHT_ACK);    //read the first byte (MSB)
    *(p_value+1)=s_read_byte(SHT_ACK);    //read the second byte (LSB)
    *p_checksum =s_read_byte(SHT_noACK);  //read checksum
    return error;
}
 
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH] 
// input :  humi [Ticks] (12 bit) 
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [癈]
{ 
    const float C1=-2.0468;           // for 12 Bit RH
    const float C2=+0.0367;           // for 12 Bit RH
    const float C3=-0.0000015955;     // for 12 Bit RH
    const float T1=+0.01;             // for 12 Bit RH
    const float T2=+0.00008;          // for 12 Bit RH  
 
    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.1;                //calc. temperature [癈] from 14 bit temp. ticks @ 5V
    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 [癈]
    *p_humidity=rh_true;              //return humidity[%RH]
}
 
UCHAR str[20];
 
//----------------------------------------------------------------------------------
void test_humi()
//----------------------------------------------------------------------------------
// 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 [癈]
// 4. calculate dew point [癈]
// 5. print temperature, humidity, dew point  
 
{ 
    value humi_val,temp_val;
    float dew_point;
    unsigned char error,checksum;
    unsigned int i;
 
    //init_uart();
    s_connectionreset();
    while(1)
    { 
        error=0;
        error+=s_measure((unsigned char*) &humi_val.i,&checksum,SHT_HUMI);  //measure humidity
        error+=s_measure((unsigned char*) &temp_val.i,&checksum,SHT_TEMP);  //measure temperature
        
        if(error!=0)
        {
            LCD_Gotoxy(0, 0);
            LCD_DispText("DHT error!!  ");
            s_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
            calc_sth11(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
            sprintf(str, "T:%4.1f HR:%4.1f%%",temp_val.f,humi_val.f);
            LCD_Gotoxy(0, 0);
            LCD_DispText(str);
        }
 
        //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------      
        for (i=0;i<40000;i++);     //(be sure that the compiler doesn't eliminate this line!)
        //-----------------------------------------------------------------------------------                       
    }
}



The result I get is like: T:475.1 HR:100.0

Am I doing something wrong?
 
Last edited:
any one tell me dht and sht not inbuld in protius7.1 ,, how simulate in protius?
 
  • Like
Reactions: noom33

    noom33

    Points: 2
    Helpful Answer Positive Rating
hey bro. can you send me the file for delay and lcd?

i also want to test this code i have the same humidity measuring project

- - - Updated - - -

hey bro. can you send me the file for delay and lcd?

i also want to test this code i have the same humidity measuring project
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top