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 CCS Technical Support

SHT71(Temperature & Humidity Sensor) Code problem

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



Joined: 07 Jan 2009
Posts: 7

View user's profile Send private message Yahoo Messenger

SHT71(Temperature & Humidity Sensor) Code problem
PostPosted: Wed Jan 07, 2009 10:28 pm     Reply with quote

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.
ECACE



Joined: 24 Jul 2006
Posts: 94

View user's profile Send private message

PostPosted: Wed Jan 07, 2009 10:57 pm     Reply with quote

Search the CCS forum and there is a driver in the code section.


http://www.ccsinfo.com/forum/viewtopic.php?t=28564&highlight=sht71
_________________
A HW Engineer 'trying' to do SW !!! Run!!!
tarmimi



Joined: 07 Jan 2009
Posts: 7

View user's profile Send private message Yahoo Messenger

thanks..
PostPosted: Thu Jan 08, 2009 1:47 am     Reply with quote

the driver working perfectly...now takes time for me to understand the code...anyway thanks for the help...really appreciate that help cause i've been trying since last 2 weeks..only needs to understand a little bit and a bit modification on code and now my my SHT71 working perfectly...the value displayed on hyperteminal
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