|
|
View previous topic :: View next topic |
Author |
Message |
Mr.P Guest
|
PIC16F877 readout SHT15 data show increase from real value |
Posted: Fri Jan 23, 2009 3:07 am |
|
|
SHT15 Code 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_A1
#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 rh;
//float truehumid1;
//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
}
|
Main code
Code: |
#include <16F877.h>
#define TxD PIN_C6 // Define Transmitted Data
#define RxD PIN_C7 // Define Received Data
#define CLOCK_SP 10000000 // Clock Speed(Hz)
#fuses HS // Oscillator mode HS
#fuses NOLVP, NOWDT // No Low Voltage Program, No Watchdog timer
#fuses NOPROTECT // Code no protection
#use delay (clock=CLOCK_SP) // Use built-in function: delay_ms() & delay_us()
#use rs232(baud=9600,parity=N,xmit=TxD,rcv=RxD,bits=8,stream=PC_COM) // Use serial I/O port (RS232)
#include<lcd.c>
#include<sht15.c>
void main(void)
{
float restemp, truehumid;
//lcd_init();
sht_init();
while(TRUE)
{
sht_rd (restemp, truehumid);
//lcd_gotoxy(1,1);
//printf(lcd_putc, "Temp : %3.1f %cC ", restemp, 223);
//printf(lcd_putc, "\nRH : %3.1f %% ", truehumid);
printf("\r\nTemp : %3.1f %cC ", restemp, 223); // print to hyper terminal
printf("\r\nRH : %3.1f %% ", truehumid); // print to hyper terminal
printf("\r\n- - - - - - - - - - - - - - - -");
delay_ms(500); //delay 500 ms between reading to prevent self heating of sensor
}
}
|
and I debugging on hyperterminal
"
- - - - - - - - - - - - - - - -
Temp : 27.5 ßC
RH : 37.6 %
- - - - - - - - - - - - - - - -
"
while room temperature show 25.3 C
I don't know why.
someone help me please. Thanks |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jan 23, 2009 3:23 am |
|
|
This is most likely to do with your hardware.
How are you reading the room temp 25.3 ?
If the difference is consitant e.g.
Code: |
Room Reading Diff
25.3 27.5 2.2
10.0 12.2 2.2
50.5 52.7 2.2
|
Then you can just add a cal value, this may need to be set differently on each hardware build as you have to adjust for tollerences of components.
You would do this by storing the value in eeprom or flash and have a serial or other routine to allow you to set it.
If the difference changes you need to work out if it is exponential, if the difference in change is the same then you have the same problem and can adjust for it.
If it is random then you have a different problem.
Besides all this, are you sure the device you are using to read the room temp is correct ?
Have you tried different ones ?
Are you reading the temp from exactly the same place ?
Is the vref correct ?
Does the vref change on each build of hardware ?
how many builds of hardware do you have ? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Fri Jan 23, 2009 9:44 am |
|
|
The SHT15 is a precision sensor (an SMD variant of the SHT75). I have use lots of these sensors in precision environment monitoring systems and found them to be reliable and accurate when check against calibrated precision instruments.
Check the following:
1. Ensure you have not turned on the sensor's self heater - this will give an error corresponding to what you are seeing
2. Do you have heat sources or contaminated airflow near the sensor?
3. Is the PCB holding the sensor obstructing the airflow?
4. Are you comparing the temperature reading against a precision, calibrated thermometer?
5. Is the thermometer and sensor close proximity - less than 2 cm?
5. What is the impact when you introduce a constant airflow across the sensor and thermometer? _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Mr.P Guest
|
|
Posted: Sat Jan 24, 2009 7:13 am |
|
|
Thank you , asmallri and Wayne_ for your suggest. Let me try check again, after that I will update again |
|
|
atai21
Joined: 30 Dec 2010 Posts: 31
|
|
Posted: Thu Dec 30, 2010 2:01 am |
|
|
may i know why when i type the driver code into ccs,the error come out say a device is required before this line (the one that i highlight)
//***** 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 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Dec 30, 2010 3:07 am |
|
|
Because you need a device statement.....
Remember CCS, deals with an enormous range of different hardware features for the PIC's. Different instruction sets between each of the 'families', and lot of variants inside these.
The compiler _must_ be told a series of things, _before_ it can compile anything:
1) Processor type
2) Fuses used
3) Clock rate (affects delays etc...).
These must be present, before anything else.
If using serial, or I2C, the definitions for these, should also exist.
Look at the CCS examples. Note how they always have right near the top of the code, these things defined (sometimes by loading another 'include' file).
Best Wishes |
|
|
atai21
Joined: 30 Dec 2010 Posts: 31
|
|
Posted: Thu Dec 30, 2010 3:11 am |
|
|
tqvm ttelmah...i will try it first |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Dec 30, 2010 3:21 am |
|
|
Actually, I suspect the problem is not visible here - are you using MPLAB?.
If so, make sure that the _include files_ LCD.c, and sht15.c, _are not_ in the project.
If MPLAB 'sees' a .c file, in the project, it will try to compile this file.
So what happens, if that it will attempt to compile the SHT15 file "on it's own", rather than 'included' in the main file (where the definitions are present).
Just leave it out of the project. It'll be included, by the include statement, and the error will vanish!....
Best Wishes |
|
|
atai21
Joined: 30 Dec 2010 Posts: 31
|
|
Posted: Thu Dec 30, 2010 3:25 am |
|
|
i'm using ccs compiler not MPLAB
tq |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Dec 30, 2010 4:03 am |
|
|
A lot of people use MPLAB, in place of the IDE for CCS, with the CCS compiler 'attached'. Hence my comments.
As posted, your code should work, unless you are trying to compile SHT15, on it's own...
Best Wishes |
|
|
|
|
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
|