/home/daqweb/fgddaq/c8051/Devices/Humidity_sensor.c File Reference

#include "../mscbemb.h"
#include "../Protocols/SHT7x_protocol.h"
#include "Humidity_sensor.h"

Functions

void HumiSensor_Init (int humsen)
unsigned char HumidSensor_Cmd (unsigned int *rhumidity, unsigned int *rtemperature, float *humidity, float *temperature, unsigned char *OrigFCS, unsigned char *DeviceFCS, int humsen)
signed char SHT7x_Measure (unsigned int *DataToSend, unsigned char flag, unsigned char *FCSoriginator, unsigned char *FCSclient, int humsen)
void SHT7x_Correction (float *p_humidity, float *p_temperature)
unsigned char ReverseByte (unsigned char dataBeReversed)

Function Documentation

unsigned char HumidSensor_Cmd ( unsigned int *  rhumidity,
unsigned int *  rtemperature,
float *  humidity,
float *  temperature,
unsigned char *  OrigFCS,
unsigned char *  DeviceFCS,
int  humsen 
)

00032                                                                                                  {
00033         
00034         float         xdata temp_humi, temp_tempe;
00035         unsigned char xdata TempStatus,HumiStatus;
00036         unsigned int  xdata rtemp,rhumi;
00037         unsigned char xdata temp_FCSOrig,temp_FCSDevice;
00038         
00039         TempStatus = SHT7x_Measure(&rtemp, TEMP, &temp_FCSOrig, &temp_FCSDevice, humsen); //measure temperature
00040         HumiStatus = SHT7x_Measure(&rhumi, HUMI, &temp_FCSOrig, &temp_FCSDevice, humsen); //measure humidity
00041         
00042         *DeviceFCS = temp_FCSDevice;
00043         *OrigFCS = temp_FCSOrig;
00044         *rtemperature = rtemp;
00045         *rhumidity = rhumi;
00046 
00047   temp_humi  = (float) rhumi;   
00048   temp_tempe = (float) rtemp;
00049   SHT7x_Correction(&temp_humi,&temp_tempe);                     //calculate humidity, temperature
00050   *humidity = temp_humi;
00051   *temperature = temp_tempe;                                                            
00052 
00053         if((TempStatus == ERROR) || (HumiStatus == ERROR)){ 
00054                 SHT7x_ConnectionReset(humsen); //in case of an error: connection reset
00055                 // delay_ms(800);
00056                 return ERROR;
00057         } 
00058 
00059         //wait approx. 0.8s to avoid heating up SHTxx
00060   //delay_ms(800);
00061         return DONE;
00062 }

void HumiSensor_Init ( int  humsen  ) 

00019                                 {
00020         
00021         SHT7x_Init(humsen);
00022 }

unsigned char ReverseByte ( unsigned char  dataBeReversed  ) 

00207                                                         {
00208 
00209         unsigned char i,j,temp = 0;
00210         
00211         for ( i=0x80,j=7;i>=0x10;i/=2,j-=2 )
00212                         temp |= ((dataBeReversed & i) >> j);            
00213         
00214         for ( i=0x08,j=1;i>=0x01;i/=2,j+=2 )
00215                         temp |= ((dataBeReversed & i) << j);
00216         return temp;
00217 }

void SHT7x_Correction ( float *  p_humidity,
float *  p_temperature 
)

00158 { 
00159         // Assuming VDD to be 3.5V but the actual voltage is 3.3V
00160         *p_temperature = (*p_temperature * D2) + D1 ;   
00161         *p_humidity = (*p_humidity) * (*p_humidity * C3) + *p_humidity * C2 + C1;       
00162         
00163         *p_humidity=(*p_temperature - 25)*(T1+ *p_humidity * T2)+ *p_humidity; //calc. Temperature compensated humidity [%RH]
00164 
00165         if(*p_humidity > 100)
00166                 *p_humidity = 100;                  // Cut if the value is outside of
00167         
00168         if(*p_humidity <0.1)
00169                 *p_humidity = 0.1;        // the physical possible range
00170 
00171 }

signed char SHT7x_Measure ( unsigned int *  DataToSend,
unsigned char  flag,
unsigned char *  FCSoriginator,
unsigned char *  FCSclient,
int  humsen 
)

00072 {
00073         unsigned char xdata check_flag, status = DONE;
00074         unsigned char xdata FCSdevice, FCSorig, MSBdata, LSBdata;
00075         unsigned int  xdata measurements=0;
00076         unsigned long xdata SHT_time =0, temp_check=0;
00077 
00078         
00079         SHT7x_TransStart(humsen);                                                //transmission start
00080         
00081         switch(flag) {                                                           //send command to sensor
00082                 case TEMP: 
00083                         status = SHT7x_WriteByte(MEASURE_TEMP, humsen); 
00084                         check_flag = MEASURE_TEMP;
00085                         break;
00086                 
00087                 case HUMI : 
00088                         status = SHT7x_WriteByte(MEASURE_HUMI, humsen); //if device does not ack, returns error
00089                         check_flag = MEASURE_HUMI;
00090                         break;
00091                 
00092                 default : 
00093                         break;
00094         }
00095         
00096         SHT_time = uptime();
00097 
00098         do {
00099                 if(humsen==1) {
00100                         if(SHT7x_DATA1 == 0) {
00101                                 temp_check = 1; 
00102                                 break; //wait until sensor has finished the measurement
00103                         }
00104                 }
00105 #ifdef MORETHANONEHUM
00106                 else if(humsen==2) {
00107                         if(SHT7x_DATA2 == 0) {
00108                                 temp_check = 1; 
00109                                 break; //wait until sensor has finished the measurement
00110                         }                       
00111                 }
00112 #endif
00113         }while ((uptime() - SHT_time) < SHT_TIME);
00114         
00115 
00116         if(temp_check == 0){ 
00117                 SHT7x_ConnectionReset(humsen);
00118                 return ERROR;                     // timeout is reached
00119         }
00120 
00121         //14bit temperature measurements
00122         //12bit humidity measurements
00123         MSBdata         = SHT7x_ReadByte(ACK, humsen);            //read the first byte (MSB)
00124         LSBdata         = SHT7x_ReadByte(ACK, humsen);      //read the second byte (LSB)
00125         FCSdevice = SHT7x_ReadByte(ACK, humsen);      //read the last byte (checksum)
00126         FCSdevice = ReverseByte (FCSdevice);
00127         
00128         //Calculate originator's side write FCS
00129   FCSorig = SHT7x_FCS_Step(LSBdata
00130           , SHT7x_FCS_Step(MSBdata
00131           , SHT7x_FCS_Step(check_flag
00132           , 0x00)));    
00133 
00134         *FCSclient = FCSdevice;
00135         *FCSoriginator = FCSorig;
00136 
00137         // Checking the checksum
00138         if (FCSdevice != FCSorig) {
00139                 SHT7x_ConnectionReset(humsen);
00140                 return ERROR;
00141   }
00142 
00143         measurements = (MSBdata <<8) | (LSBdata);
00144 
00145         if(flag == TEMP)
00146                 measurements &= 0x3fff;
00147         else
00148                 measurements &= 0x0fff;
00149         
00150         *DataToSend =  measurements;
00151         return status;     
00152 }


Generated on 10 Jun 2013 for FGDC8051 by  doxygen 1.4.7