Beispiel #1
0
//==============================================================================
int main(void)
//==============================================================================
{  // variables
  u8t  error = 0;              //variable for error code. For codes see system.h
 // u8t  userRegister;           //variable for user register
 // bt   endOfBattery;           //variable for end of battery

  nt16 sRH;                    //variable for raw humidity ticks
  ft   humidityRH;             //variable for relative humidity[%RH] as float
  nt16 sT;                     //variable for raw temperature ticks
  ft   temperatureC;           //variable for temperature[°C] as float
  //u8t  SerialNumber_SHT2x[8];  //64bit serial number

  Grace_init();
  //Init_HW();                   //initializes Hardware (osc, watchdog,...)
  I2c_Init();                  //initializes uC-ports for I2C
  DelayMicroSeconds(15000);    //wait for sensor initialization t_powerUp (15ms)

  //note: The following code segments show how to use the different functions
  //      of SHT2x. The loop does not show a typical sequence in an application

  while(1)
  { error = 0;                                       // reset error status
   /* // --- Reset sensor by command ---
    error |= SHT2x_SoftReset();

    // --- Read the sensors serial number (64bit) ---
    error |= SHT2x_GetSerialNumber(SerialNumber_SHT2x);

    // --- Set Resolution e.g. RH 10bit, Temp 13bit ---
    error |= SHT2x_ReadUserRegister(&userRegister);  //get actual user reg
    userRegister = (userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
    error |= SHT2x_WriteUserRegister(&userRegister); //write changed user reg
*/
    // --- measure humidity with "Polling Mode" (no hold master) ---
    error |= SHT2x_MeasurePoll(HUMIDITY, &sRH);
    // --- measure temperature with "Polling Mode" (no hold master) ---
    error |= SHT2x_MeasurePoll(TEMP, &sT);

    //-- calculate humidity and temperature --
    temperatureC = SHT2x_CalcTemperatureC(sT.u16);
    humidityRH   = SHT2x_CalcRH(sRH.u16);

 /*   // --- check end of battery status (eob)---
    // note: a RH / Temp. measurement must be executed to update the status of eob
    error |= SHT2x_ReadUserRegister(&userRegister);  //get actual user reg
    if( (userRegister & SHT2x_EOB_MASK) == SHT2x_EOB_ON ) endOfBattery = true;
    else endOfBattery = false;
*/

  }
}
uint8_t getTH(uint8_t *humidity, float *temperature) {
    uint8_t error;
    uint16_t raw;
    
    error = SHT2x_MeasurePoll(HUMIDITY, &raw);
    if (error) {
        *humidity = 255;
    } else {
        *humidity = (uint8_t) (SHT2x_CalcRH(raw) + 0.5);
    }
    
    error |= SHT2x_MeasurePoll(TEMP, &raw);
    if (error) {
        *temperature = 255;
    } else {
        *temperature = SHT2x_CalcTemperatureC(raw);
    }
    return error;
}