Need help! SHT11 humidity & Temp sensor

General discussion on mikroC.
Author
Message
M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

Need help! SHT11 humidity & Temp sensor

#1 Post by M1ck3y » 01 Mar 2008 13:38

Hi guys,

I am facing a little problem with the Sensiron SHT11 humidity & temperature sensor. Hoping for some advices and suggestions.

I've search through the forum in mikroElektronika whether mikroC or mikroBasic, etc and of course on google also. And I've found that there are something in common in their codes that is similar to the original sensiron application notes which written by Keil.

I now trying to use Keil's codes by making a little modification to suit my hardware and conditions. However, the values I get from the sensor seem to be wrong.

I am using PIC 16F877A~4MHz, data pin =PortB.5, sck pin =PortC.3.

The values from my sensor:

Temperature~ 613.0699
Humidity ~1e-1

Any approaches or suggestions are welcome.

The following are my codes:

Code: Select all

//Filename   : SHT11_testing.c
//Compiled by: Mikro C v8.0.0.0
//PIC 16F877A~4MHz
/*********************************************/

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


//------------------------------------------------------------------------------
// modul-var
//------------------------------------------------------------------------------
//enum {TEMP,HUMI};

#include "initlcd.c"

#define TEMP 0
#define HUMI 1

#define PORT_COND TRISB.F5    //data port condition: input/output
#define DATA PORTB.F5         //SHT11 DATA pin
#define SCK PORTC.F3          //STH11 SCK pin
#define noACK 0
#define ACK 1
//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

#define MEMORY_ADDRESS  0xA0

//------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
   unsigned char i,error=0;

   PORT_COND = 0;        //SHT11 WRITE

   for (i=0x80;i>0;i/=2) //shift bit for masking
   { if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
     else DATA=0;
     SCK=1; //clk for SENSI-BUS
     Delay_us(5);   //pulswith approx. 5 us
     SCK=0;
   }

   DATA=1; //release DATA-line

   PORT_COND = 1;    //SHT11 READ

   Delay_us(1);

   SCK=1; //clk #9 for ack
   error=DATA; //check ack (DATA will be pulled down by SHT11)
   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;

   PORT_COND = 1;        //SHT11 READ

   DATA=1; //release DATA-line

   for (i=0x80;i>0;i/=2) //shift bit for masking
   { SCK=1; //clk for SENSI-BUS

     if (DATA) val=(val | i); //read bit
     SCK=0;
   }

   DATA=!ack; //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack
   Delay_us(5); //pulswith approx. 5 us
   SCK=0;
   DATA=1; //release DATA-line
   return val;
}


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

   DATA=1; SCK=0;   //Initial state
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=0;
   Delay_us(2);
   SCK=0;
   Delay_us(5);
   SCK=1;
   Delay_us(2);
   DATA=1;
   Delay_us(2);
   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;

   PORT_COND = 0;  //SHT11 WRITE

   DATA=1; SCK=0;  //Initial state

   for(i=0;i<9;i++) //9 SCK cycles
   { SCK=1;
     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(RESET); //send 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(STATUS_REG_R); //send command to sensor

   *p_value=s_read_byte(ACK); //read status register (8-bit)
   *p_checksum=s_read_byte(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(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 error=0;
   unsigned int i;

   s_transstart(); //transmission start

   switch(mode)
   { //send command to sensor
   case TEMP : error+=s_write_byte(MEASURE_TEMP); break;   //temp
   case HUMI : error+=s_write_byte(MEASURE_HUMI); break;   //humidity
   default : break;
   }

   PORT_COND = 1;  //SHT11 READ

   for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement

   if(DATA) error+=1; // or timeout (~2 sec.) is reached

   *(p_value) =s_read_byte(ACK); //read the first byte (MSB)
   *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
   *p_checksum =s_read_byte(noACK); //read checksum

   return error;
}

/*
//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
// Initializes the UART so the final data can be sent away, e.g. to a PC
//9600 bps @ 11.059 MHz
{     SCON = 0x52;
      TMOD = 0x20;
      TCON = 0x69;
      TH1 = 0xfd;
}
*/

//----------------------------------------------------------------------------------------
void calc_sth11(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=*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 [C]

   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]
}


//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
{  float k,dew_point ;

   k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);

   dew_point = 243.12*k/(17.62-k);

   return dew_point;
}

/************************************************************/
//LCD display

  char* Out_temp()
 {
     char str[]="Temp:" absolute MEMORY_ADDRESS;
     return str;
 }
 
  char* Out_humi()
 {
     char str[]="Humi:" absolute MEMORY_ADDRESS;
     return str;
 }

//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 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


{  value humi_val,temp_val;

   float dew_point;
   unsigned char error,checksum;
   unsigned int i;

   char temp[13];
   char humi[13];


   Initlcd();    //Initialize LCD

   s_connectionreset();  //reset

   while(1)
   {

     error=0;

     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
     //measure humidity

     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
     //measure temperature

     if(error!=0) 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

       dew_point=calc_dewpoint(humi_val.f,temp_val.f); 
       //calculate dew point

       //send final data to LCD

       Lcd8_Out(1,1,Out_temp());
       Lcd8_Out(2,1,Out_humi());

       FloatToStr(temp_val.f, temp);
       Lcd8_Out(1,6,temp);

       FloatToStr(humi_val.f, humi);
       Lcd8_Out(2,6,humi);


     }
//----------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!)
//-----------------------------------------------------------------------------------
       Delay_ms(500);

   }

}


/************************************************************/
//Filename   : initlcd.c
//Compiled by: Mikro C v8.0.0.0
//Description: Initializations of LCD & Port.
/************************************************************/

void Initlcd(void)
 {
  PORTA = 0x00;          //initialize/clear PORTA
  PORTB = 0x00;          //initialize PORTB
  PORTC = 0x00;          //initialize PORTC
  PORTD = 0x00;          //initialize PORTD
    
  TRISA = 0;             //PORTA = output.
  TRISB = 0x0D;       //0x0D=0b00001101->pin(0,2,3)=input.
  TRISC = 0x03;       //0x03=0b00000011->pin(0,1)=input.
  TRISD = 0;            //PORTD = output.
  TRISE = 0;             //PORTE = output.
  
  Lcd8_Config(&PORTC, &PORTD,7,5,6,7,6,5,4,3,2,1,0);
  Lcd8_Cmd(LCD_CLEAR);       // Clear display
  Lcd8_Cmd(LCD_CURSOR_OFF);  // Turn cursor off


 }


Is there any thing wrong?

Your feedbacks are welcome.

Thank you.

Here is the original Application Notes:

Click Here to Download
Last edited by M1ck3y on 02 Mar 2008 06:42, edited 1 time in total.

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

#2 Post by piort » 01 Mar 2008 15:00

hi,
did you chk http://www.mikroe.com/forum/viewtopic.p ... ight=sht11 ? Yo2lio had make nice lib for it ;-) not in MC but still worth a look )))

happy coding ;-)

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#3 Post by M1ck3y » 01 Mar 2008 15:13

Hi,

Thx for reply. I've read it.It's a quite nice lib however is not for PIC16 . I'll give it a try.

thx anyway.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 01 Mar 2008 20:59

M1ck3y wrote:Hi,

Thx for reply. I've read it.It's a quite nice lib however is not for PIC16 . I'll give it a try.

thx anyway.
MikroBasic http://www.microelemente.ro/MikroBasic/ ... on_lib.zip

MikroPascal http://www.microelemente.ro/MikroPascal/Sensirion.zip
MikroPascal http://www.microelemente.ro/MikroPascal ... on_P16.zip

Both library (MP and MB) have module for P16 !

It's easy to translate this in MC.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#5 Post by M1ck3y » 02 Mar 2008 06:00

Hi,
Thanks for reply.

I'll give it a shot and try to translate it to MC.

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#6 Post by M1ck3y » 02 Mar 2008 06:33

Hi all,

Thanks to all that reply to my posts.

I've figured out what's wrong with my original codes. The major modification from the original Sensirion's Keil C example is on :

s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)

The original one is
*(p_value) = s_read_byte(ACK);
*(p_value+1)=s_read_byte(ACK);

the change is:
*(p_value+1)=s_read_byte(ACK);
*(p_value) = s_read_byte(ACK);

After swaping the two statements above the value of the sensor reported correctly.

The following is my working code for Sensirion SHT11 modified from Keil's codes, hope that I would help someone that might need it in MikroC.

Image

Code: Select all


/*******************************************************************************
      Filename   : SHT11_testing.c

      Author      : LIAU WEI CHUN
      Email        : wcl14u@gmail.com
      Revision    : Demo Revised 1.0b
      Date         : 29 Feb 2008
      Compiled by: Mikro C v8.0.0.0

      Description: main program of SHT11 humidity & temperature sensor 
                        modified from Keil's codes.

      PIC 16F877A~4MHz.

      History    : Changes made to the original Keil's lib.
                 - Reconfigure data pin, sck pin & data port condition.
                 - Added data port condition: input/output
                 - _nop_() function -> Delay_us()

                 ***Critical***
                 - Swap the read first byte (MSB)line with the read second byte
                   (LSB) in function char s_measure().                   

*******************************************************************************/


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


//------------------------------------------------------------------------------
// modul-var
//------------------------------------------------------------------------------
//enum {TEMP,HUMI};

#include "initlcd.h"

#define TEMP 0
#define HUMI 1

#define PORT_COND TRISB.F5    //data port condition: input/output
#define DATA PORTB.F5         //SHT11 DATA pin
#define SCK PORTC.F3          //STH11 SCK pin
#define noACK 0
#define ACK 1
//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

#define MEMORY_ADDRESS  0xA0


  char* Out_temp()
 {
     char str[]="Temp:" absolute MEMORY_ADDRESS;
     return str;
 }
 
  char* Out_humi()
 {
     char str[]="Humi:" absolute MEMORY_ADDRESS;
     return str;
 }


//------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
   unsigned char i,error=0;

   PORT_COND = 0;        //SHT11 WRITE


   for (i=0x80;i>0;i/=2) //shift bit for masking
   { if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
     else DATA=0;
     SCK=1; //clk for SENSI-BUS
     Delay_us(5);   //pulswith approx. 5 us
     SCK=0;
   }

   DATA=1; //release DATA-line

   PORT_COND = 1;    //SHT11 READ


   Delay_us(2);

   SCK=1; //clk #9 for ack
   error=DATA; //check ack (DATA will be pulled down by SHT11)
   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;

   PORT_COND = 1;        //SHT11 READ


   DATA=1; //release DATA-line

   for (i=0x80;i>0;i/=2) //shift bit for masking
   { SCK=1; //clk for SENSI-BUS

     if (DATA) val=(val | i); //read bit
     SCK=0;
   }

   DATA=!ack; //in case of "ack==1" pull down DATA-Line
   SCK=1; //clk #9 for ack
   Delay_us(5); //pulswith approx. 5 us
   SCK=0;
   DATA=1; //release DATA-line
   return val;
}


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

   DATA=1; SCK=0;   //Initial state
   Delay_us(2);
   SCK=1;
   Delay_us(2);
   DATA=0;
   Delay_us(2);
   SCK=0;
   Delay_us(5);
   SCK=1;
   Delay_us(2);
   DATA=1;
   Delay_us(2);
   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;

   PORT_COND = 0;  //SHT11 WRITE


   DATA=1; SCK=0;  //Initial state

   for(i=0;i<9;i++) //9 SCK cycles
   { SCK=1;
     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(RESET); //send 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(STATUS_REG_R); //send command to sensor

   *p_value=s_read_byte(ACK); //read status register (8-bit)
   *p_checksum=s_read_byte(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(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 error=0;
   unsigned int i;

   s_transstart(); //transmission start

   switch(mode)
   { //send command to sensor
   case TEMP : error+=s_write_byte(MEASURE_TEMP); break;   //temp
   case HUMI : error+=s_write_byte(MEASURE_HUMI); break;   //humidity
   default : break;
   }

   PORT_COND = 1;  //SHT11 READ


   for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement

   if(DATA) error+=1; // or timeout (~2 sec.) is reached

   *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
   *(p_value) =s_read_byte(ACK); //read the first byte (MSB)


   
   *p_checksum =s_read_byte(noACK); //read checksum

   return error;
}

/*
//----------------------------------------------------------------------------------
void init_uart()
//----------------------------------------------------------------------------------
// Initializes the UART so the final data can be sent away, e.g. to a PC
//9600 bps @ 11.059 MHz
{     SCON = 0x52;
      TMOD = 0x20;
      TCON = 0x69;
      TH1 = 0xfd;
}
*/

//----------------------------------------------------------------------------------------
void calc_sth11(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=*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 [C]

   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]
}


//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
{  float k,dew_point ;

   k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);

   dew_point = 243.12*k/(17.62-k);

   return dew_point;
}

//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// 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


{  value humi_val,temp_val;

   float dew_point;
   unsigned char error,checksum;
   //unsigned int i;

   char temp[13];
   char humi[13];


   Initlcd();    //Initialize LCD

   s_connectionreset();
   Delay_ms(20);

   while(1)
   {

     error=0;

     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
     //measure humidity

     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
     //measure temperature

     if(error!=0) 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

       dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point

       //send final data to serial interface (UART)

       Lcd8_Out(1,1,Out_temp());
       Lcd8_Out(2,1,Out_humi());

       FloatToStr(temp_val.f, temp);
       Lcd8_Out(1,6,temp);

       FloatToStr(humi_val.f, humi);
       Lcd8_Out(2,6,humi);


     }
//----------wait approx. 0.8s to avoid heating up SHTxx----------------------
//-----------------------------------------------------------------------------------
       Delay_ms(1000);

   }

}

/************************************************************   
       Filename   : initlcd.c
       Description: Initializations of LCD & Sound.
************************************************************/

void Initlcd(void)
 {
  PORTA = 0x00;          //initialize/clear PORTA
  PORTB = 0x00;          //initialize PORTB
  PORTC = 0x00;          //initialize PORTC
  PORTD = 0x00;          //initialize PORTD
    
  TRISA = 0;             //PORTA = output.
  TRISB = 0;
  TRISC = 0;
  TRISD = 0;             //PORTD = output.
  TRISE = 0;             //PORTE = output.
  

  Lcd8_Config(&PORTC, &PORTD,7,5,6,7,6,5,4,3,2,1,0);
  Lcd8_Cmd(LCD_CLEAR);       // Clear display
  Lcd8_Cmd(LCD_CURSOR_OFF);  // Turn cursor off


 }


Puggs
Posts: 179
Joined: 17 Nov 2007 23:05
Location: Melbourne Australia
Contact:

#7 Post by Puggs » 02 Mar 2008 22:51

Hi

Great Work, can you tell me where you got your sensor from, i would like to buy a few for my project?

Puggs

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#8 Post by M1ck3y » 03 Mar 2008 17:51

Hi Puggs,

Actually I got the sensor from the Sensirion company. Its a free sample but now the free sample promotion need to wait till June to be available.

More details you can get from:

http://www.sensirion.com/en/06_contact- ... arters.htm

http://www.sensirion.com/en/01_humidity ... mpling.htm

Besides I think you also can get from distributor like farnell:

http://my.farnell.com/jsp/Industrial+Co ... tid=359983


Hope it helps.

barbarossa
Posts: 37
Joined: 25 Jan 2007 14:18
Location: Sweden, Tågarp

#9 Post by barbarossa » 04 Mar 2008 18:26

Are you shore your acknowledgment code in s_readbyte is working properly?
You need to clear the trisbit to send a low signal to the sensor.

Check if your low byte is 255 or 0.

ku4rt
Posts: 3
Joined: 17 Feb 2008 11:33
Location: Tennessee
Contact:

Where to buy sht11 sensor

#10 Post by ku4rt » 07 Mar 2008 10:04


M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#11 Post by M1ck3y » 07 Mar 2008 20:41

Hi all,
Are you shore your acknowledgment code in s_readbyte is working properly?
I don't quite understand your question. Are you meaning the function of s_readbyte itself or the use of s_readbyte in other function?

ya, I doubted is it actually working?since the code is not actually written by me i just modified it. May be I might miss something can you please further clarify your statement, thanks.

But for now the sensor seem to report correctly.



Thank you.

barbarossa
Posts: 37
Joined: 25 Jan 2007 14:18
Location: Sweden, Tågarp

#12 Post by barbarossa » 07 Mar 2008 22:31

I'm referring to this line, which will NOT pull DATA low:

Code: Select all

DATA=!ack; //in case of "ack==1" pull down DATA-Line

You need to clear the trisbit in order to pull DATA low, otherwise your low byte will be either 0xff or 0x00 ( the last bit in high byte).

You wrote in your first post that you got a temperature of 613.0699 which if you calculate backwards from the floating number gives you

1111 1111 0001 1011.

As the bytes was in incorrect order you have a low byte filled with ones which is just not very likely to be repeatable.

What you have now is a 6 bit temperature and a 4 bit humidity sensor.With a correct code your temperature resolution should be 0.01°C.


I have just finished programming a PIC16F690 for this sensor and I use pull up resistors, and just switch the trisbits.

//extract of my code where it differentiate from yours

Code: Select all

#define DATA_P PORTB.F4
#define DATA_T TRISB.F4
#define SCK_T TRISB.F6

Code: Select all

 

PORTB = 0x00;
TRISB = 0x00;

Code: Select all

char s_read_byte(char ack)
{    unsigned char i, val = 0;
     for (i=0x80;i>0;i/=2)
     {   SCK_T = 1;

         if (DATA_P) val = (val | i);
         SCK_T = 0;
     }

     DATA_T = !ack;
     SCK_T = 1;
     _nop_();_nop_();_nop_();
     SCK_T = 0;
     DATA_T = 1;


     return val;
}
This code works as if a trisbit is set a PIC reads from the pin and if clear the PIC reads from the register.

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#13 Post by M1ck3y » 08 Mar 2008 08:37

Hi,

Thanks for your reply. It does helps a lot. I will give it a try.



Thank you.

Puggs
Posts: 179
Joined: 17 Nov 2007 23:05
Location: Melbourne Australia
Contact:

#14 Post by Puggs » 25 Mar 2008 10:04

Hi

Finaly got my SHT11 from Farnell Australia, it would be nice if you can tell me what pins on the PIC you used (Yes i'm being Lazy) it would save what limited time i do have for doing Dev work.

Puggs

M1ck3y
Posts: 24
Joined: 07 Sep 2007 16:53

#15 Post by M1ck3y » 26 Mar 2008 03:32

Hi,

Actually u can configure your pin according to your will and your pic MCU model.

For me:
#define DATA PORTB.F5 //SHT11 DATA pin
#define SCK PORTC.F3 //STH11 SCK pin
For barbarossa:
#define DATA_P PORTB.F4
#define DATA_T TRISB.F4
#define SCK_T TRISB.F6

Usually preferably port B for data pin. You can have a look on the above code that we have posted.

Have a nice try.


Regards,

M1ck3y

Post Reply

Return to “mikroC General”