tarmimi
Joined: 07 Jan 2009 Posts: 7
|
SHT71(Temperature & Humidity Sensor) Code problem |
Posted: Wed Jan 07, 2009 10:28 pm |
|
|
Hello everyone. I would like to ask about communication between the SHT71 sensor and PIC18F6722.
I found a sample code from the forum but not CCS and I have edited the code for CCS application. The problem is I cant get the value of temperature and humidity to display on hyperterminal. When I reset the PIC, the value of temperature and humidity appear but not correct.
Code: |
// Example of user space C program to read a humidity and temperature
// sensor Sensirion SHT71 (http://www.acmesystems.it/?id=89)
#include <18f6722.h>
//#include<intrins.h.>
#include<math.h>
#include<stdio.h.>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=PC)
// -----------------------------------------------------------------------
// SHT71 sensor define
// -----------------------------------------------------------------------
#define CLOCK_BIT PIN_B1
#define DATA_BIT PIN_B0
// Macro to set the data line direction
//#define DATA_LINE_IN gpiosetdir(PORTG,DIRIN,DATA_BIT)
//#define DATA_LINE_OUT gpiosetdir(PORTG,DIROUT,DATA_BIT)
//#define DATA_LINE_IN set_tris_c(0x01)
//#define DATA_LINE_OUT set_tris_c(0x00)
//#define DATA_LINE_LOW 0
//#define DATA_LINE_HIGH 1
//#define DATA_LINE_READ gpiogetbits(PORTG,DATA_BIT)?(1):(0)
//#define DATA_LINE_READ input_state(PIN_C0)
//#define CLOCK_LINE_LOW 0
//#define CLOCK_LINE_HIGH 1
#define CMD_READ_TEMP 0x03
#define CMD_READ_HUM 0x05
// -----------------------------------------------------------------------
// Send the start sequence
// -----------------------------------------------------------------------
void SendStart(void) {
//DATA_LINE_OUT;
set_tris_b(0x00);
output_high(DATA_BIT);
output_low(CLOCK_BIT);
delay_cycles(1);
output_high(CLOCK_BIT);
delay_cycles(1);
output_low(DATA_BIT);
delay_cycles(1);
output_low(CLOCK_BIT);
delay_cycles(1);
output_high(CLOCK_BIT);
delay_cycles(1);
output_high(DATA_BIT);
delay_cycles(1);
output_low(CLOCK_BIT);
//DATA_LINE_IN;
set_tris_b(0x01);
}
// -----------------------------------------------------------------------
// Sensor reset
// -----------------------------------------------------------------------
void SendReset(void) {
int i;
output_high(DATA_BIT);
output_low(CLOCK_BIT);
for (i=0;i<12;i++) {
output_high(CLOCK_BIT);
delay_cycles(1);
output_low(CLOCK_BIT);
}
SendStart();
}
// -----------------------------------------------------------------------
// Send a byte to the sensor
// -----------------------------------------------------------------------
int SendByte(unsigned char value) {
int i;
//DATA_LINE_OUT;
set_tris_b(0x00);
for (i=0x80;i>0;i/=2) {
if (i & value)
output_high(DATA_BIT);
else
output_low(DATA_BIT);
output_high(CLOCK_BIT);
delay_cycles(1);
output_low(CLOCK_BIT);
}
//DATA_LINE_IN;
set_tris_b(0x01);
output_high(DATA_BIT);
output_high(CLOCK_BIT);
output_low(CLOCK_BIT);
return 1;
}
// -----------------------------------------------------------------------
// Read a byte from the sensor
// withack
// 1 = send the ACK
// 0 = don't send the ACK
// -----------------------------------------------------------------------
char ReadByte(int withack) {
char val=0;
int i;
int x;
//DATA_LINE_IN;
set_tris_b(0x01);
x=input_state(DATA_BIT);
for (i=0x80;i>0;i/=2) {
output_high(CLOCK_BIT);
if (x) val = (val| i);
output_low(CLOCK_BIT);
}
if (withack) {
// Acknowledge del byte
//DATA_LINE_OUT;
set_tris_b(0x00);
output_low(DATA_BIT);
output_high(CLOCK_BIT);
output_low(CLOCK_BIT);
//DATA_LINE_IN;
//set_tris_b(0x01);
} else {
// Senza acknowledge
//DATA_LINE_OUT;
set_tris_b(0x00);
output_high(DATA_BIT);
output_high(CLOCK_BIT);
output_low(CLOCK_BIT);
//DATA_LINE_IN;
set_tris_b(0x01);
}
return val;
}
// ----------------------
// Read the temperature
// ----------------------
int ReadTemperature(void){
char Lsb,Msb,Chk;
int z;
SendStart();
z=input_state(DATA_BIT);
if (!SendByte(CMD_READ_TEMP)) return 0;
while(z);
Msb=ReadByte(1);
Lsb=ReadByte(1);
Chk=ReadByte(0);
return ((Msb<<8)+Lsb);
}
// ------------------
// Read the humidity
// ------------------
int ReadHumidity(void) {
char Lsb,Msb,Chk;
int y;
SendStart();
y=input_state(DATA_BIT);
if (!SendByte(CMD_READ_HUM)) return 0;
while(y);
Msb=ReadByte(1);
Lsb=ReadByte(1);
Chk=ReadByte(0);
return ((Msb<<8)+Lsb);
}
// ----------
// main code
// ----------
void main(void) {
float real_humidity;
float real_temperature;
int soh;
int sot;
SendReset();
soh=ReadHumidity();
sot=ReadTemperature();
real_humidity= -4 + 0.0405*soh + -2.8E-6;
real_temperature = -39.66 + 0.01*sot;
printf ("Humidity : %.2f %%\n",real_humidity);
printf ("Temperature: %.2f C\n",real_temperature);
}
|
If someone does have working sample code for this SHT71 sensor hopefully will help me. Thanks. |
|