|
|
View previous topic :: View next topic |
Author |
Message |
hansolo
Joined: 06 Oct 2006 Posts: 20
|
Driver For SHT75 Temperature & Humidity Sensor |
Posted: Tue Oct 17, 2006 5:41 pm |
|
|
This driver should also work for SHT11, SHT71 and SHT15 as these sensors are using the same communication protocol.
I had tested these codes with PIC16F877A but should work with other PICs as well because I used delay in the driver codes.
If you find any bugs or inaccuracy of reading, please PM me.
SHT75.C Driver
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_D3
#define sht_clk_pin PIN_C4
//***** Function to alert SHT75 *****
void comstart (void)
{
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
delay_us(2);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
}
//***** Function to write data to SHT75 *****
int1 comwrite (int8 iobyte)
{
int8 i, mask = 0x80;
int1 ack;
//Shift out command
delay_us(4);
for(i=0; i<8; i++)
{
output_bit(sht_clk_pin, 0); //clk low
if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high
else output_bit(sht_data_pin, 0); //data low if MSB low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
mask = mask >> 1; //shift to next bit
}
//Shift in ack
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
ack = input(sht_data_pin); //get ack bit
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_clk_pin, 0); //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_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 0 bit
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_float(sht_data_pin); //data high
//shift in LSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 1 bit
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
return(iobyte);
}
//***** Function to wait for SHT75 reading *****
void comwait (void)
{
int16 sht_delay;
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
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_bit(sht_clk_pin, 0); //clk low
delay_us(2);
for(i=0; i<9; i++)
{
output_bit(sht_clk_pin, 1); //toggle clk 9 times
delay_us(2);
output_bit(sht_clk_pin, 0);
delay_us(2);
}
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
} |
The Main Program
Code: | #include <16F877A.h>
#fuses HS,NOWDT,PUT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include<lcd.c>
#include<sht75.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
}
} |
|
|
|
huichoman
Joined: 20 Nov 2006 Posts: 2
|
Thank you very much |
Posted: Mon Nov 20, 2006 1:42 am |
|
|
I proved the routine with a sht71. At the moment it works perfectly. If I find some error I send a PM to you.
Greetings from Mexico |
|
|
hansolo
Joined: 06 Oct 2006 Posts: 20
|
Re: Thank you very much |
Posted: Wed Nov 22, 2006 5:17 pm |
|
|
huichoman wrote: | I proved the routine with a sht71. At the moment it works perfectly. If I find some error I send a PM to you.
Greetings from Mexico |
Thanks for your feedback. |
|
|
w_arm
Joined: 10 Apr 2007 Posts: 4 Location: Desenvolvedor
|
The obtained reading is larger than to real. |
Posted: Thu May 10, 2007 11:05 am |
|
|
I am using the sht75. The obtained reading is larger than to real (it differentiates of 4 degrees). |
|
|
hansolo
Joined: 06 Oct 2006 Posts: 20
|
|
Posted: Wed May 16, 2007 5:51 pm |
|
|
How do you confirm this? Did you compare the reading with a calibrated thermo-hygrometer?
Hansolo |
|
|
sonicdeejay
Joined: 20 Dec 2005 Posts: 112
|
|
Posted: Sun May 27, 2007 12:50 am |
|
|
It is working.....I use PIC18F2525 and SHT71(look out for page 2 and 8 out this datasheet for connection)
Main
Code: | #include <18F2525.h>
#fuses HS,NOWDT,PUT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include<lcdmod.c>
#include<sht75.c>
#byte PORTB = 0xf81
#byte PORTC = 0xf82
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
}
}
|
SHT75.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_A1 (I use A1 as data input)
#define sht_clk_pin PIN_C4
//***** Function to alert SHT75 *****
void comstart (void)
{
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
delay_us(2);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
}
//***** Function to write data to SHT75 *****
int1 comwrite (int8 iobyte)
{
int8 i, mask = 0x80;
int1 ack;
//Shift out command
delay_us(4);
for(i=0; i<8> 0) output_float(sht_data_pin); //data high if MSB high
else output_bit(sht_data_pin, 0); //data low if MSB low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
mask = mask >> 1; //shift to next bit
}
//Shift in ack
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
ack = input(sht_data_pin); //get ack bit
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_clk_pin, 0); //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_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 0 bit
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_float(sht_data_pin); //data high
//shift in LSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 1 bit
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
return(iobyte);
}
//***** Function to wait for SHT75 reading *****
void comwait (void)
{
int16 sht_delay;
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
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_bit(sht_clk_pin, 0); //clk low
delay_us(2);
for(i=0; i<9; i++)
{
output_bit(sht_clk_pin, 1); //toggle clk 9 times
delay_us(2);
output_bit(sht_clk_pin, 0);
delay_us(2);
}
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
}
|
LCDmod.C
Code: | ///////////////////////////////////////////////////////////////////////////
//// LCDD.C ////
//// Driver for common LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////
// As defined in the following structure the pin connection is as follows:
// D0 enable
// D1 rs
// D2 rw
// D4 D4
// D5 D5
// D6 D6
// D7 D7
//
// LCD pins D0-D3 are not used and PIC D3 is not used.
// Un-comment the following define to use port B
// #define use_portb_lcd TRUE
struct lcd_pin_map { // This structure is overlayed
BOOLEAN enable; // on to an I/O port to gain
BOOLEAN rs; // access to the LCD pins.
BOOLEAN rw; // The bits are allocated from
BOOLEAN unused; // low order up. ENABLE will
int data : 4; // be pin B0.
} lcd;
#if defined(__PCH__)
#if defined use_portb_lcd
#byte lcd = 0xF81 // This puts the entire structure
#else
#byte lcd = 0xF83 // This puts the entire structure
#endif
#else
#if defined use_portb_lcd
#byte lcd = 6 // on to port B (at address 6)
#else
#byte lcd = 8 // on to port D (at address 8)
#endif
#endif
#if defined use_portb_lcd
#define set_tris_lcd(x) set_tris_b(x)
#else
#define set_tris_lcd(x) set_tris_d(x)
#endif
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
// These bytes need to be sent to the LCD
// to start it up.
// The following are used for setting
// the I/O port direction register.
struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
BYTE lcd_read_byte() {
BYTE low,high;
set_tris_lcd(LCD_READ);
lcd.rw = 1;
delay_cycles(1);
lcd.enable = 1;
delay_cycles(1);
high = lcd.data;
lcd.enable = 0;
delay_cycles(1);
lcd.enable = 1;
delay_us(1);
low = lcd.data;
lcd.enable = 0;
set_tris_lcd(LCD_WRITE);
return( (high<<4>> 4);
lcd_send_nibble(n & 0xf);
}
//Modified code to fit S6A0069 chip based LCD
void lcd_init() {
set_tris_lcd(LCD_WRITE);
lcd.rs = 0;
lcd.rw = 0;
lcd.enable = 0;
delay_ms(30);
lcd_send_nibble(0x02);
delay_us(1);
lcd_send_nibble(0x02);
delay_us(1);
lcd_send_nibble(0x0C);
delay_us(45);
lcd_send_nibble(0x00);
delay_us(1);
lcd_send_nibble(0x0F);
delay_us(45);
lcd_send_nibble(0x00);
delay_us(1);
lcd_send_nibble(0x01);
delay_ms(2);
lcd_send_nibble(0x00);
delay_us(1);
lcd_send_nibble(0x06);
delay_ms(2);
}
//Orignal LCD format
/*
void lcd_init() {
BYTE i;
set_tris_lcd(LCD_WRITE);
lcd.rs = 0;
lcd.rw = 0;
lcd.enable = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0,LCD_INIT_STRING[i]);
}
*/
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
char lcd_getc( BYTE x, BYTE y) {
char value;
lcd_gotoxy(x,y);
while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
lcd.rs=1;
value = lcd_read_byte();
lcd.rs=0;
return(value);
}
|
|
|
|
STIX
Joined: 17 Jun 2006 Posts: 12
|
|
Posted: Wed Jun 13, 2007 2:38 pm |
|
|
I used the same code on a SHT11, however I'm not getting any meaningful readout.
The temperature is showing -39.9C all the time.
The only thing that I've changed are the:
Code: | #define sht_data_pin PIN_D0
#define sht_clk_pin PIN_D1 |
and I'm just outputing to the hyperterminal. Any chance I have a faulty SHT11? or is this something else?
Thanks |
|
|
hansolo
Joined: 06 Oct 2006 Posts: 20
|
|
Posted: Thu Jun 14, 2007 5:44 pm |
|
|
Make sure you connect the +5V and Gnd pins correctly.
If you connect the power pins wrongly and you power up there goes your SHT11.
Hansolo |
|
|
JULIEN LAFFITTE
Joined: 28 Mar 2007 Posts: 20
|
|
Posted: Fri Jul 13, 2007 2:29 am |
|
|
Quote: | I used the same code on a SHT11, however I'm not getting any meaningful readout.
The temperature is showing -39.9C all the time.
The only thing that I've changed are the:
Code:
#define sht_data_pin PIN_D0
#define sht_clk_pin PIN_D1
and I'm just outputing to the hyperterminal. Any chance I have a faulty SHT11? or is this something else?
Thanks |
it's the same for me and i have just change:
-output_bit(x,0) by a output_low(x)
-output_bit(x,1) by a output_high(x)
and now working good
i think it's just a problem of I/O config |
|
|
erik006
Joined: 05 Jan 2008 Posts: 10
|
If you're not getting correct data |
Posted: Sat Mar 08, 2008 1:06 pm |
|
|
For future reference:
If you're not getting "proper" output, check that you have i2c resistors on the sht clock and data lines. |
|
|
soundsk
Joined: 10 May 2008 Posts: 6
|
|
Posted: Sat May 10, 2008 10:43 am |
|
|
Hi,
I've read this thread over and over, and still am getting:
TEMP: -39.9
RH: -4.6%
I can't figure out what's wrong... I'm using 16F877A, and SHT11 (with 10K pull up on both CLK and DATA pins).
The code i'm using is this (pretty much the same, only changed output_bit to output_high or output_low like JULIEN LAFFITTE suggested):
MAIN:
Code: |
#include "remote.h"
//#include "picram.h"
#include <lcdts.C>
#include<SHT.c>
void main()
{
float restemp, truehumid;
lcd_init();
delay_ms(100);
sht_init();
delay_ms(100);
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_D3 //(I use A1 as data input)
#define sht_clk_pin PIN_C4
//***** Function to alert SHT75 *****
void comstart (void)
{
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
delay_us(2);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
}
//***** Function to write data to SHT75 *****
int1 comwrite (int8 iobyte)
{
int8 i, mask = 0x80;
int1 ack;
//Shift out command
delay_us(4);
for(i=0; i<8> 0) output_float(sht_data_pin); //data high if MSB high
else output_bit(sht_data_pin, 0); //data low if MSB low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
mask = mask >> 1; //shift to next bit
}
//Shift in ack
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
ack = input(sht_data_pin); //get ack bit
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_clk_pin, 0); //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_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 0 bit
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_float(sht_data_pin); //data high
//shift in LSB data
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
}
//send ack 1 bit
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
return(iobyte);
}
//***** Function to wait for SHT75 reading *****
void comwait (void)
{
int16 sht_delay;
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
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_bit(sht_clk_pin, 0); //clk low
delay_us(2);
for(i=0; i<9; i++)
{
output_bit(sht_clk_pin, 1); //toggle clk 9 times
delay_us(2);
output_bit(sht_clk_pin, 0);
delay_us(2);
}
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
} |
One other thing, when i compile the code i get two warnings saying that functions measuretemp and measurehumid are not void and do not return a value measuretemp or measurehumid.
Can someone please help me? i need to get this working...
Thanks in advance!... |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Fri Jun 06, 2008 3:13 pm |
|
|
soundsk wrote: | Hi,
I've read this thread over and over, and still am getting:
TEMP: -39.9
RH: -4.6%
I can't figure out what's wrong... I'm using 16F877A, and SHT11 (with 10K pull up on both CLK and DATA pins).
The code i'm using is this (pretty much the same, only changed output_bit to output_high or output_low like JULIEN LAFFITTE suggested):
|
Hi,
The code posted above works fine. I tested it with PIC18F2525 with the version 4 comiler and it gives very accurate results without any intermittent corrupt data. Many thanks to HANSOLO for posting it.
Hardware-wise I use a 10k pullup to +5V on the data line and no resistor on the clock line. The power on the sensirion side is bypassed with a 1.0uF ceramic capacitor.
You can ignore the function not void warning. If you want to get rid of it put the word "void" before the function declaration. |
|
|
monkeyman
Joined: 05 Jun 2008 Posts: 18
|
|
Posted: Thu Jun 12, 2008 6:15 am |
|
|
hello
my sht75 working good in 3v with the same code but i use a pull down of 4.7k on clk pin and pull up on data pin |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Wed Jul 09, 2008 8:42 am |
|
|
i've just tried your code with a 16F876A and a SHT11.
it's working fine, i've just got 28°C instead of 25°C i guess it's normal (tolerency).
i'm using a 10K pull-up on the dataline and no resistor on the clock line. _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
adrian
Joined: 08 Sep 2003 Posts: 92 Location: Glasgow, UK
|
|
Posted: Thu Jul 10, 2008 5:32 am |
|
|
sliders_alpha wrote: | i've just tried your code with a 16F876A and a SHT11.
it's working fine, i've just got 28°C instead of 25°C i guess it's normal (tolerency). |
I have been bitten by this one myself recently. I assume from your readings that you are powering the device from 5V? Have another look at the data sheet - especially footnote 3 in Table 1! The current issue of the data sheet is v3.01. |
|
|
|
|
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
|