Pages: [1]   Go Down
Print
Author Topic: ใครพอจะมีไฟล์ SHT71.h ขอหน่อยคับ  (Read 7742 times)
0 Members and 2 Guests are viewing this topic.
pam
มาใหม่
*
Offline Offline

Posts: 6


Email
« on: September 16, 2010, 12:19:06 PM »

#include "lpc214x.h"
#include "sht71.h"      อยากได้ไฟล์นี้ครับ
#include "delay.h"
#include "math.h"

// DATA = P0.27
// SCK  = P0.26


//MYDEF'S
#define IODIR         IODIR1

#define IOSET         IOSET1
#define IOCLR         IOCLR1

#define SCK                   0x04000000
#define SCK_HIGH      IOSET1 = 0x04000000;
#define SCK_LOW         IOCLR1 = 0x04000000;

#define DATA                0x08000000
#define DATA_HIGH      IOSET1 = 0x08000000;
#define DATA_LOW      IOCLR1 = 0x08000000;
//MYDEF'S END

typedef union
{
   unsigned int i;
   float f;
}value;

enum { TEMPERATURE, HUMIDITY};

#define noACK   0
#define ACK    1

//adr command r/w
#define STATUS_REG_W    0x06    //0000 0110
#define STATUS_REG_R    0x07    //0000 0111
#define MEASURE_TEMP   0x03    //0000 0011
#define MEASURE_HUMI    0x05    //0000 0101
#define RESET          0x1e   //0001 1110



char sht71_write_command(unsigned char command)
{
   unsigned char i, err=0;
   
   for (i=0x80; i>0; i/=2)      //shift bit for masking
   {
      if (i & command)
         DATA_HIGH      //masking value with i , write to SENSI-BUS
      
      else
         DATA_LOW
      
      SCK_HIGH      //clk for SENSI-BUS
      
      
         nop();
      nop();
      nop();;   //pulswith approx. 5 us

      SCK_LOW
   }
   
   DATA_HIGH   //release DATA-line
   

   IODIR &= ~(DATA);   //define DATA line as input

   SCK_HIGH   //clk #9 for ack

   //check ack (DATA will be pulled down by SHT11)
   if(IOSET & DATA)
      err = 1;

   else
      err = 0;

   IODIR |= DATA;   //define DATA line as output
   
   SCK_LOW
   

   return err;
}


char sht71_read_value(unsigned char ack)
{
   unsigned char i,val=0;

   DATA_HIGH //release DATA-line

   IODIR &= ~(DATA);   //define DATA line as input

   for (i=0x80; i>0 ; i/=2) //shift bit for masking
   {
      SCK_HIGH      //clk for SENSI-BUS
      
      if (IOSET & DATA)
         val=(val | i); //read bit
      
      SCK_LOW;
   }

   IODIR = DATA;   //define DATA line as ouput
   
   if(!ack)
      DATA_HIGH

   else
      DATA_LOW
   
   SCK_HIGH      //clk #9 for ack
   
   nop();
   nop();
   nop(); //pulswith approx. 5 us
   
   SCK_LOW
   
   DATA_HIGH //release DATA-line
   
   return val;
}

void sht71_transmission_start(void)
{
   DATA_HIGH
   SCK_LOW //Initial state
   
   nop();

   SCK_HIGH
   nop();

   DATA_LOW
   nop();

   SCK_LOW
   nop();nop();nop();

   SCK_HIGH
   nop();

   DATA_HIGH
   nop();

   SCK_LOW
}

void sht71_connection_reset(void)
{
   unsigned char i;
   
   DATA_HIGH
   SCK_LOW    //Initial state
   
   
   for(i=0; i<9; i++) //9 SCK cycles
   {
      SCK_HIGH

      SCK_LOW
   }

   sht71_transmission_start(); //transmission start
}

char sht71_soft_reset(void)
{
   unsigned char err=0;

   sht71_connection_reset(); //reset communication

   err = sht71_write_command(RESET); //send RESET-command to sensor

   return err; //error=1 in case of no response form the sensor
}

char sht71_read_status_register(unsigned char *p_value, unsigned char *p_checksum)
{
   unsigned char err=0;

   sht71_transmission_start(); //transmission start
   
   err = sht71_write_command(STATUS_REG_R); //send command to sensor
   
   *p_value = sht71_read_value(ACK); //read status register (8-bit)
   *p_checksum = sht71_read_value(noACK); //read checksum (8-bit)
   
   return err; //error=1 in case of no response form the sensor
}

char sht71_write_status_register(unsigned char *p_value)
{
   unsigned char err=0;

   sht71_transmission_start(); //transmission start
   
   err += sht71_write_command(STATUS_REG_W);//send command to sensor
   err += sht71_write_command(*p_value); //send value of status register

   return err; //error>=1 in case of no response form the sensor
}

char sht71_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
   unsigned err=0;
   unsigned int i;

   sht71_transmission_start(); //transmission start
   
   
   switch(mode)
   { //send command to sensor
      case TEMPERATURE : err += sht71_write_command(MEASURE_TEMP); break;
      case HUMIDITY : err += sht71_write_command(MEASURE_HUMI); break;
      default : break;
   }

   
   IODIR &= ~(DATA);   //define DATA line as input

   for (i=0;i<65535;i++)
      if(!(IOSET & DATA))
         break; //wait until sensor has finished the measurement
   
   if(IOSET & DATA)
      err+=1; // or timeout (~2 sec.) is reached

   IODIR |= DATA;      //define DATA line as input
   
   *(p_value) = sht71_read_value(ACK); //read the first byte (MSB)
   *(p_value+1)= sht71_read_value(ACK); //read the second byte (LSB)
   *p_checksum = sht71_read_value(noACK); //read checksum
   
   
   return err;
}

void sht71_calc_humidity(float *p_humidity ,float *p_temperature)
{
   const float C1=-4.0; // for 12 Bit
   const float C2= 0.0405; // for 12 Bit
   const float C3=-0.0000028; // for 12 Bit
   const float T1=0.01; // for 14 Bit @ 5V
   const float T2=0.00008; // for 14 Bit @ 5V
   float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
   float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
   float rh_lin; // rh_lin: Humidity linear
   float rh_true; // rh_true: Temperature compensated humidity
   float t_C; // t_C : Temperature [C]

   t_C = t * 0.01 - 40; //calc. Temperature from ticks to [C]

   rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]

   if(rh_true>100)
      rh_true=100; //cut if the value is outside of
   
   if(rh_true<0.1)
      rh_true=0.1; //the physical possible range
      
   *p_temperature = t_C; //return temperature [C]
   *p_humidity=rh_true; //return humidity[%RH]
}

void sht71_calc_temperature(float *p_temperature)
{
   const float C1=-4.0; // for 12 Bit
   const float C2= 0.0405; // for 12 Bit
   const float C3=-0.0000028; // for 12 Bit
   const float T1=0.01; // for 14 Bit @ 5V
   const float T2=0.00008; // for 14 Bit @ 5V
   float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
   float t_C; // t_C : Temperature [C]

   t_C = t * 0.01 - 40; //calc. Temperature from ticks to [C]

   *p_temperature = t_C; //return temperature [C]
}

void sht71_init(void)
{
   IODIR1 |= SCK;
   IODIR1 |= DATA;
   
   sht71_connection_reset();
}

//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
//      for(i=0;i<40000;i++); //(be sure that the compiler doesn’t eliminate this line!)
//-----------------------------------------------------------------------------------
float sht71_get_humidity()
{
   value humi_val,temp_val;
   unsigned char err,checksum;

   err=0;

    //measure humidity
   err+=sht71_measure((unsigned char*) &humi_val.i,&checksum,HUMIDITY);

    //measure temperature
   err+=sht71_measure((unsigned char*) &temp_val.i,&checksum,TEMPERATURE);

   if(err!=0)
   {
      //in case of an error: connection reset
      sht71_connection_reset();

      return -1;
   }
      
   else
   {
      //converts integer to float
      humi_val.f=(float)humi_val.i;    
      temp_val.f=(float)temp_val.i;    
      
      //calculate humidity, temperature
      sht71_calc_humidity(&humi_val.f, &temp_val.f);

      return humi_val.f;
   }
}

float sht71_get_temperature()
{
   value temp_val;
   unsigned char err, checksum;

   err=0;
   
   //measure temperature
   err+=sht71_measure((unsigned char*) &temp_val.i,&checksum, TEMPERATURE);


   if(err!=0)
   {
      sht71_connection_reset(); //in case of an error: connection reset

      return (err * (-1));
   }
      
   else
   {
      //converts integer to float
      temp_val.f=(float)temp_val.i;
      
      //calculate temperature
      sht71_calc_temperature(&temp_val.f);

      return temp_val.f;
   }
   

   return 0; //DEBUG
}
Logged
gundam
Global Moderator
Hero Member
*****
Offline Offline

Posts: 292



« Reply #1 on: September 16, 2010, 05:54:47 PM »

ลองดูที่
http://softgun.sourcearchive.com/documentation/0.18-1/dir_ed6aef1ced588c5bc5216df60481b061.html
Logged
pam
มาใหม่
*
Offline Offline

Posts: 6


Email
« Reply #2 on: September 16, 2010, 06:19:31 PM »

เปิดดูที่เว็บแล้วครับผม
มันขึ้นแค่นี้ครับ
ผมจะทำยังไงต่อครับงงมากๆเลย

sht71.h
void SHT71_New(const char *name);

Logged
gundam
Global Moderator
Hero Member
*****
Offline Offline

Posts: 292



« Reply #3 on: September 16, 2010, 06:50:57 PM »

- ให้คุณสร้าง text ไฟล์ขึ้นมา(เช่นสร้างจาก notepad) แล้วใส่
void SHT71_New(const char *name);
บันทึกไฟล์เป็นชื่อ sht71.h

- หรือ comment บันทัด
//#include "sht71.h"
แล้วใส่
void SHT71_New(const char *name);
Logged
Pages: [1]   Go Up
Print
 
Jump to: