CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SHT11 & 16F877A - Need Help!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
soundsk



Joined: 10 May 2008
Posts: 6

View user's profile Send private message

SHT11 & 16F877A - Need Help!
PostPosted: Thu May 15, 2008 5:23 am     Reply with quote

Hi,

In the last couple of days i've been trying to get a Sensirion SHT11 sensor to work with a PIC 16F877A, but i'm not getting anywhere.

I've changed everything i could think of and my eyes are going to fall off their sockets due to so much googling...

Can someone give a hand? I'm desperate...

Thank You!

The CLK line is connected to pin D1 and the data line connected to D0 with a 10K pull up...

The code used is the one available at the forum's code library...
http://www.ccsinfo.com/forum/viewtopic.php?t=28564

When i power on the circuit the LCD displays fixed values of -39.9 ÂșC for temp and -4.6% for humidity...

I've had a look at the sample code from Sensrion and some other pieces of code i found, but so far i've had no luck...

The code i am using is the following:


Main:
Code:
#include <16F877A.h>
#fuses HS,NOWDT,PUT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#include<lcdts.c>
#include<sht.c>

void main()
{
 float restemp, truehumid;
 lcd_init();
 sht_init();

 while(1)
 {
  sht_rd (restemp, truehumid);
  lcd_gotoxy(1,1);
  printf(lcd_putc, "Temp : %3.1f %cC   ", restemp, 223);
  printf(lcd_putc, "\nRH   : %3.1f %%   ", truehumid);
  delay_ms(500);        //delay 500 ms between reading to prevent self heating of sensor
 }
}


sht.c:
Code:
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Driver file for SHT75 Temperature & Humidity Sensor                       //
//                                                                           //
// ***** To initialise SHT75 sensor upon power up *****                      //
//                                                                           //
// Function : sht_init()                                                     //
// Return   : none                                                           //
//                                                                           //
//                                                                           //
// ***** To measure and caluculate SHT75 temp & real RH *****                //
//                                                                           //
// Function : sht_rd (temp, truehumid)                                       //
// Return   : temperature & true humidity in float values                    //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#define sht_data_pin   PIN_D0
#define sht_clk_pin    PIN_D1


//***** Function to alert SHT75 *****

void comstart (void)
{
 output_float(sht_data_pin);  //data high
 output_low(sht_clk_pin);  //clk low
 delay_us(2);
 output_high(sht_clk_pin);  //clk high
 delay_us(2);
 output_low(sht_data_pin); //data low
 delay_us(2);
 output_low(sht_clk_pin);  //clk low
 delay_us(5);
 output_high(sht_clk_pin);  //clk high
 delay_us(2);
 output_float(sht_data_pin);  //data high
 delay_us(2);
 output_low(sht_clk_pin);  //clk low
}


//***** Function to write data to SHT75 *****

int1 comwrite (int8 iobyte)
{
 int8 i, mask = 0x80;
 int1 ack;

 //Shift out command
 delay_us(5);
 for(i=0; i<8; i++)
  {
   output_low(sht_clk_pin);                          //clk low
   if((iobyte & mask) > 0) output_float(sht_data_pin);  //data high if MSB high
   else output_low(sht_data_pin);                    //data low if MSB low
   delay_us(2);
   output_high(sht_clk_pin);                          //clk high
   delay_us(2);
   mask = mask >> 1;                                    //shift to next bit
  }

 //Shift in ack
 output_low(sht_clk_pin);  //clk low
 delay_us(2);
 ack = input(sht_data_pin);   //get ack bit
 output_high(sht_clk_pin);  //clk high
 delay_us(2);
 output_low(sht_clk_pin);  //clk low
 return(ack);
}


//***** Function to read data from SHT75 *****

int16 comread (void)
{
 int8 i;
 int16 iobyte = 0;
 const int16 mask0 = 0x0000;
 const int16 mask1 = 0x0001;

 //shift in MSB data
 for(i=0; i<8; i++)
  {
   iobyte = iobyte << 1;
   output_high(sht_clk_pin);                //clk high
   delay_us(2);
   if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
   else iobyte |= mask0;
   output_low(sht_clk_pin);                //clk low
   delay_us(2);
  }

 //send ack 0 bit
 output_low(sht_data_pin); //data low
 delay_us(2);
 output_high(sht_clk_pin);  //clk high
 delay_us(5);
 output_low(sht_clk_pin);  //clk low
 delay_us(2);
 output_float(sht_data_pin);  //data high

 //shift in LSB data
 for(i=0; i<8; i++)
  {
   iobyte = iobyte << 1;
   output_high(sht_clk_pin);                //clk high
   delay_us(2);
   if (input(sht_data_pin)) iobyte |= mask1;  //shift in data bit
   else iobyte |= mask0;
   output_low(sht_clk_pin);                //clk low
   delay_us(2);
  }

 //send ack 1 bit
 output_float(sht_data_pin);  //data high
 delay_us(2);
 output_high(sht_clk_pin);  //clk high
 delay_us(5);
 output_low(sht_clk_pin);  //clk low

 return(iobyte);
}


//***** Function to wait for SHT75 reading *****

void comwait (void)
{
 int16 sht_delay;

 output_float(sht_data_pin);                     //data high
 output_low(sht_clk_pin);                     //clk low
 delay_us(2);
 for(sht_delay=0; sht_delay<30000; sht_delay++)  // wait for max 300ms
  {
   if (!input(sht_data_pin)) break;              //if sht_data_pin low, SHT75 ready
   delay_us(10);
  }
}


//***** Function to reset SHT75 communication *****

void comreset (void)
{
 int8 i;

 output_float(sht_data_pin);    //data high
 output_low(sht_clk_pin);    //clk low
 delay_us(5);
 for(i=0; i<9; i++)
  {
   output_high(sht_clk_pin);  //toggle clk 9 times
   delay_us(5);
   output_low(sht_clk_pin);
   delay_us(5);
 }
 comstart();
}


//***** Function to soft reset SHT75 *****

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


//***** Function to measure SHT75 temperature *****

int16 measuretemp (void)
{
 int1 ack;
 int16 iobyte;

 comstart();             //alert SHT75
 ack = comwrite(0x03);   //send measure temp command and read ack status
 if(ack == 1) return;
 comwait();              //wait for SHT75 measurement to complete
 iobyte = comread();     //read SHT75 temp data
 return(iobyte);
}


//***** Function to measure SHT75 RH *****

int16 measurehumid (void)
{
 int1 ack;
 int16 iobyte;

 comstart();            //alert SHT75
 ack = comwrite(0x05);  //send measure RH command and read ack status
 if(ack == 1) return;
 comwait();             //wait for SHT75 measurement to complete
 iobyte = comread();    //read SHT75 temp data
 return(iobyte);
}


//***** Function to calculate SHT75 temp & RH *****

void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue)
{
 float truehumid1, rh;

 //calculate temperature reading
 tc = ((float) temp * 0.01) - 40.0;

 //calculate Real RH reading
 rh = (float) humid;

 rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;

 //calculate True RH reading
 rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
}


//***** Function to measure & calculate SHT75 temp & RH *****

void sht_rd (float & temp, float & truehumid)
{
 int16 restemp, reshumid;
 float realhumid;
 restemp = 0; truehumid = 0;

 restemp = measuretemp();    //measure temp
 reshumid = measurehumid();  //measure RH
calculate_data (restemp, reshumid, temp, realhumid, truehumid);  //calculate temp & RH
}


//***** Function to initialise SHT75 on power-up *****

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 15, 2008 1:21 pm     Reply with quote

1. Put an oscilloscope problem on the clock and data lines of the SHT11.
Do you see any activity ? Also check the Vdd pin on the SHT11.
Does it show any voltage ? The idle state of the DATA pin should be
at a high level. Is it ?

2. What voltage is the Vdd voltage of the PIC and the SHT11 ?

3. Is there a ground connection between the SHT11 and the PIC ?

4. Make sure you don't have the signal lines swapped.

5. Try adding a 10K pull-down resistor on the CLK line.
This is recommended by Sensiron. Note this is a pull-down,
not a pull-up. The DATA line should have a pull-up on it.

6. Add a 100 nF (0.1 uF) ceramic capacitor between the Vdd and Vss
pins on the SHT11.
soundsk



Joined: 10 May 2008
Posts: 6

View user's profile Send private message

PostPosted: Fri May 16, 2008 9:15 am     Reply with quote

Hi!

First of all thank you for your reply...

Answering your questions:

I don't currently have access to an oscilloscope, but both the PIC and the sensor are being powered with 5V supplied by a 7805 and two caps.

VDD Pin on SHT shows 4.99V,and although not necessary, i added another 100nF cap right by the sensor between VDD and VSS.

Also added the 10K pull down on the clock line.

With the circuit powered on, the sensor is correctly supplied, and the DATA line (with pull up) shows 4.99V and the CLK line (with pull down) shows 4.10V.

Another thing, when compiling the code above, i get two warnings:

That function int16 measuretemp (void) is not void and does not return a value measuretemp.
That function int16 measurehumid (void) is not void and does not return a value measuretemp.

Any thoughts or suggestions on what the problem can be?
Thanks!
Ttelmah
Guest







PostPosted: Fri May 16, 2008 9:47 am     Reply with quote

The error messages from the compiler, are because you have a 'return' without a value, in the middle of both the functions, while the functions are defined to return a value. In ANSI C, this defaults to returning a 'NULL' value, but CCS does not do this, so safer to return your own 'null' value when you prematurely exit like this.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group