Port of the SHT2x example code from Sensirion

Dependents:   sht21_test ENVLogger

Committer:
ssozonoff
Date:
Sat Apr 23 18:10:56 2011 +0000
Revision:
1:d0f691423bf1
Parent:
0:74df6ab91c79
Child:
2:2464fed17980
Got Master Hold mode working, Thanks Andy!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ssozonoff 0:74df6ab91c79 1 //
ssozonoff 0:74df6ab91c79 2 // S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland
ssozonoff 0:74df6ab91c79 3 //==============================================================================//
ssozonoff 0:74df6ab91c79 4 // Project : SHT2x Sample Code (V1.2)
ssozonoff 0:74df6ab91c79 5 // File : SHT2x.c
ssozonoff 0:74df6ab91c79 6 // Author : MST
ssozonoff 0:74df6ab91c79 7 // Controller: NEC V850/SG3 (uPD70F3740)
ssozonoff 0:74df6ab91c79 8 // Compiler : IAR compiler for V850 (3.50A)
ssozonoff 0:74df6ab91c79 9 // Brief : Sensor layer. Functions for sensor access
ssozonoff 0:74df6ab91c79 10 //==============================================================================//
ssozonoff 0:74df6ab91c79 11 //---------- Includes ----------------------------------------------------------
ssozonoff 0:74df6ab91c79 12 #include "SHT2x.h"
ssozonoff 1:d0f691423bf1 13 #include "mbed.h"
ssozonoff 0:74df6ab91c79 14
ssozonoff 1:d0f691423bf1 15 SHT2x::SHT2x (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl), out(USBTX, USBRX) {
ssozonoff 0:74df6ab91c79 16 }
ssozonoff 0:74df6ab91c79 17
ssozonoff 0:74df6ab91c79 18 //==============================================================================//
ssozonoff 0:74df6ab91c79 19 int SHT2x::SHT2x_CheckCrc(int data[], int nbrOfBytes, int checksum)
ssozonoff 0:74df6ab91c79 20 //==============================================================================//
ssozonoff 0:74df6ab91c79 21 {
ssozonoff 0:74df6ab91c79 22 int crc = 0;
ssozonoff 0:74df6ab91c79 23 int byteCtr;
ssozonoff 0:74df6ab91c79 24 //calculates 8-Bit checksum with given polynomial
ssozonoff 0:74df6ab91c79 25 for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) {
ssozonoff 0:74df6ab91c79 26 crc ^= (data[byteCtr]);
ssozonoff 0:74df6ab91c79 27 for (int bit = 8; bit > 0; --bit) {
ssozonoff 0:74df6ab91c79 28 if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
ssozonoff 0:74df6ab91c79 29 else crc = (crc << 1);
ssozonoff 0:74df6ab91c79 30 }
ssozonoff 0:74df6ab91c79 31 }
ssozonoff 0:74df6ab91c79 32 if (crc != checksum) return CHECKSUM_ERROR;
ssozonoff 0:74df6ab91c79 33 else return 0;
ssozonoff 0:74df6ab91c79 34 }
ssozonoff 0:74df6ab91c79 35 //===========================================================================//
ssozonoff 0:74df6ab91c79 36 int SHT2x::SHT2x_ReadUserRegister(int *pRegisterValue)
ssozonoff 0:74df6ab91c79 37 //===========================================================================//
ssozonoff 0:74df6ab91c79 38 {
ssozonoff 0:74df6ab91c79 39 int checksum; //variable for checksum byte
ssozonoff 0:74df6ab91c79 40 int error=0; //variable for error code
ssozonoff 1:d0f691423bf1 41
ssozonoff 0:74df6ab91c79 42 i2c.start();
ssozonoff 0:74df6ab91c79 43 i2c.write(I2C_ADR_W);
ssozonoff 0:74df6ab91c79 44 i2c.write(USER_REG_R);
ssozonoff 1:d0f691423bf1 45
ssozonoff 0:74df6ab91c79 46 i2c.start();
ssozonoff 0:74df6ab91c79 47 i2c.write(I2C_ADR_R);
ssozonoff 1:d0f691423bf1 48
ssozonoff 1:d0f691423bf1 49
ssozonoff 1:d0f691423bf1 50 *pRegisterValue = i2c.read(ACK);
ssozonoff 1:d0f691423bf1 51
ssozonoff 1:d0f691423bf1 52 checksum=i2c.read(NoACK);
ssozonoff 1:d0f691423bf1 53
ssozonoff 0:74df6ab91c79 54 error |= SHT2x_CheckCrc (pRegisterValue,1,checksum);
ssozonoff 0:74df6ab91c79 55
ssozonoff 0:74df6ab91c79 56 i2c.stop();
ssozonoff 0:74df6ab91c79 57 return error;
ssozonoff 0:74df6ab91c79 58 }
ssozonoff 0:74df6ab91c79 59 //===========================================================================//
ssozonoff 0:74df6ab91c79 60 int SHT2x::SHT2x_WriteUserRegister(int *pRegisterValue)
ssozonoff 0:74df6ab91c79 61 //===========================================================================//
ssozonoff 0:74df6ab91c79 62 {
ssozonoff 0:74df6ab91c79 63 int error=0; //variable for error code
ssozonoff 0:74df6ab91c79 64 i2c.start();
ssozonoff 1:d0f691423bf1 65
ssozonoff 0:74df6ab91c79 66 i2c.write(I2C_ADR_W);
ssozonoff 0:74df6ab91c79 67 i2c.write(USER_REG_W);
ssozonoff 0:74df6ab91c79 68 i2c.write(*pRegisterValue);
ssozonoff 0:74df6ab91c79 69 i2c.stop();
ssozonoff 0:74df6ab91c79 70
ssozonoff 0:74df6ab91c79 71 return error;
ssozonoff 0:74df6ab91c79 72 }
ssozonoff 0:74df6ab91c79 73 //===========================================================================//
ssozonoff 0:74df6ab91c79 74 int SHT2x::SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, int *pMeasurand)
ssozonoff 0:74df6ab91c79 75 //===========================================================================//
ssozonoff 0:74df6ab91c79 76 {
ssozonoff 0:74df6ab91c79 77 int checksum; //checksum
ssozonoff 0:74df6ab91c79 78 int data[2]; //data array for checksum verification
ssozonoff 0:74df6ab91c79 79 int error=0; //error variable
ssozonoff 1:d0f691423bf1 80 int i;
ssozonoff 1:d0f691423bf1 81
ssozonoff 1:d0f691423bf1 82 //-- write I2C sensor address and command --
ssozonoff 0:74df6ab91c79 83 i2c.start();
ssozonoff 1:d0f691423bf1 84
ssozonoff 0:74df6ab91c79 85 i2c.write(I2C_ADR_W); // I2C Adr
ssozonoff 0:74df6ab91c79 86 switch (eSHT2xMeasureType) {
ssozonoff 0:74df6ab91c79 87 case HUMIDITY:
ssozonoff 0:74df6ab91c79 88 i2c.write(TRIG_RH_MEASUREMENT_HM);
ssozonoff 0:74df6ab91c79 89 break;
ssozonoff 1:d0f691423bf1 90 case TEMP:
ssozonoff 0:74df6ab91c79 91 i2c.write(TRIG_T_MEASUREMENT_HM);
ssozonoff 0:74df6ab91c79 92 break;
ssozonoff 0:74df6ab91c79 93 default:
ssozonoff 0:74df6ab91c79 94 break;
ssozonoff 0:74df6ab91c79 95 }
ssozonoff 1:d0f691423bf1 96
ssozonoff 0:74df6ab91c79 97 //-- wait until hold master is released --
ssozonoff 1:d0f691423bf1 98 i2c.start();
ssozonoff 0:74df6ab91c79 99 i2c.write(I2C_ADR_R);
ssozonoff 1:d0f691423bf1 100
ssozonoff 0:74df6ab91c79 101 //SCL=HIGH; // set SCL I/O port as input
ssozonoff 1:d0f691423bf1 102 i2c.sclAsInput();
ssozonoff 1:d0f691423bf1 103
ssozonoff 1:d0f691423bf1 104 for (i=0; i<1000; i++) { // wait until master hold is released or
ssozonoff 1:d0f691423bf1 105 wait_ms(1); // a timeout (~1s) is reached
ssozonoff 1:d0f691423bf1 106 if (i2c.sclRead() == 1) break;
ssozonoff 1:d0f691423bf1 107 }
ssozonoff 1:d0f691423bf1 108
ssozonoff 0:74df6ab91c79 109 //-- check for timeout --
ssozonoff 1:d0f691423bf1 110 if (i2c.sclRead() == 0) error |= TIME_OUT_ERROR;
ssozonoff 1:d0f691423bf1 111 //-- read two data bytes and one checksum byte --
ssozonoff 0:74df6ab91c79 112
ssozonoff 1:d0f691423bf1 113 i2c.sclNormal();
ssozonoff 1:d0f691423bf1 114
ssozonoff 1:d0f691423bf1 115 data[0] = i2c.read(ACK);
ssozonoff 1:d0f691423bf1 116 data[1] = i2c.read(ACK);
ssozonoff 1:d0f691423bf1 117
ssozonoff 0:74df6ab91c79 118 *pMeasurand = data[0] << 8;
ssozonoff 0:74df6ab91c79 119 *pMeasurand |= data[1];
ssozonoff 1:d0f691423bf1 120
ssozonoff 1:d0f691423bf1 121 checksum = i2c.read(NoACK);
ssozonoff 0:74df6ab91c79 122 //-- verify checksum --
ssozonoff 0:74df6ab91c79 123 error |= SHT2x_CheckCrc (data, 2, checksum);
ssozonoff 0:74df6ab91c79 124
ssozonoff 0:74df6ab91c79 125 i2c.stop();
ssozonoff 0:74df6ab91c79 126 return error;
ssozonoff 0:74df6ab91c79 127 }
ssozonoff 0:74df6ab91c79 128 //===========================================================================//
ssozonoff 0:74df6ab91c79 129 int SHT2x::SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, int *pMeasurand)
ssozonoff 0:74df6ab91c79 130 //===========================================================================//
ssozonoff 0:74df6ab91c79 131 {
ssozonoff 0:74df6ab91c79 132 int checksum; //checksum
ssozonoff 0:74df6ab91c79 133 int data[2]; //data array for checksum verification
ssozonoff 0:74df6ab91c79 134 int error = 0; //error variable
ssozonoff 0:74df6ab91c79 135 int i = 0; //counting variableSample Code SHT21
ssozonoff 0:74df6ab91c79 136
ssozonoff 0:74df6ab91c79 137 //-- write I2C sensor address and command --
ssozonoff 0:74df6ab91c79 138 i2c.start();
ssozonoff 1:d0f691423bf1 139
ssozonoff 0:74df6ab91c79 140 i2c.write(I2C_ADR_W);
ssozonoff 0:74df6ab91c79 141 switch (eSHT2xMeasureType) {
ssozonoff 0:74df6ab91c79 142 case HUMIDITY:
ssozonoff 0:74df6ab91c79 143 i2c.write(TRIG_RH_MEASUREMENT_POLL);
ssozonoff 0:74df6ab91c79 144 break;
ssozonoff 0:74df6ab91c79 145 case TEMP:
ssozonoff 0:74df6ab91c79 146 i2c.write(TRIG_T_MEASUREMENT_POLL);
ssozonoff 0:74df6ab91c79 147 break;
ssozonoff 0:74df6ab91c79 148 default:
ssozonoff 0:74df6ab91c79 149 break;
ssozonoff 1:d0f691423bf1 150
ssozonoff 0:74df6ab91c79 151 }
ssozonoff 0:74df6ab91c79 152 //-- poll every 10ms for measurement ready. Timeout after 20 retries (200ms)--
ssozonoff 0:74df6ab91c79 153 do {
ssozonoff 0:74df6ab91c79 154 i2c.start();
ssozonoff 1:d0f691423bf1 155 wait_ms(10);
ssozonoff 0:74df6ab91c79 156 if (i++ >= 20) break;
ssozonoff 0:74df6ab91c79 157 } while (i2c.write(I2C_ADR_R) == 0);
ssozonoff 1:d0f691423bf1 158
ssozonoff 0:74df6ab91c79 159 if (i >= 20) error |= TIME_OUT_ERROR;
ssozonoff 1:d0f691423bf1 160
ssozonoff 0:74df6ab91c79 161 //-- read two data bytes and one checksum byte --
ssozonoff 1:d0f691423bf1 162 data[0] = i2c.read(ACK);
ssozonoff 1:d0f691423bf1 163 data[1] = i2c.read(ACK);
ssozonoff 1:d0f691423bf1 164
ssozonoff 0:74df6ab91c79 165 *pMeasurand = data[0] << 8;
ssozonoff 1:d0f691423bf1 166 *pMeasurand |= data[1];
ssozonoff 1:d0f691423bf1 167
ssozonoff 1:d0f691423bf1 168 checksum = i2c.read(NoACK);
ssozonoff 0:74df6ab91c79 169 //-- verify checksum --
ssozonoff 0:74df6ab91c79 170 error |= SHT2x_CheckCrc (data,2,checksum);
ssozonoff 0:74df6ab91c79 171 i2c.stop();
ssozonoff 0:74df6ab91c79 172 return error;
ssozonoff 0:74df6ab91c79 173 }
ssozonoff 0:74df6ab91c79 174 //===========================================================================//
ssozonoff 0:74df6ab91c79 175 int SHT2x::SHT2x_SoftReset()
ssozonoff 0:74df6ab91c79 176 //===========================================================================//
ssozonoff 0:74df6ab91c79 177 {
ssozonoff 0:74df6ab91c79 178 int error=0; //error variable
ssozonoff 0:74df6ab91c79 179 i2c.start();
ssozonoff 0:74df6ab91c79 180 error |= i2c.write(I2C_ADR_W); // I2C Adr
ssozonoff 0:74df6ab91c79 181 error |= i2c.write(SOFT_RESET); // Command
ssozonoff 0:74df6ab91c79 182 i2c.stop();
ssozonoff 0:74df6ab91c79 183 wait_ms(15);
ssozonoff 0:74df6ab91c79 184 return error;
ssozonoff 0:74df6ab91c79 185 }
ssozonoff 0:74df6ab91c79 186 //==============================================================================//
ssozonoff 0:74df6ab91c79 187 float SHT2x::SHT2x_CalcRH(int u16sRH)
ssozonoff 0:74df6ab91c79 188 //==============================================================================//
ssozonoff 0:74df6ab91c79 189 {
ssozonoff 0:74df6ab91c79 190 float humidityRH; // variable for result
ssozonoff 0:74df6ab91c79 191 u16sRH &= ~0x0003; // clear bits [1..0] (status bits)
ssozonoff 0:74df6ab91c79 192 //-- calculate relative humidity [%RH] --
ssozonoff 0:74df6ab91c79 193 humidityRH = -6.0 + 125.0/65536 * (float)u16sRH; // RH= -6 + 125 * SRH/2^16
ssozonoff 0:74df6ab91c79 194 return humidityRH;
ssozonoff 0:74df6ab91c79 195 }
ssozonoff 0:74df6ab91c79 196 //==============================================================================//
ssozonoff 0:74df6ab91c79 197 float SHT2x::SHT2x_CalcTemperatureC(int u16sT)
ssozonoff 0:74df6ab91c79 198 //==============================================================================//
ssozonoff 0:74df6ab91c79 199 {
ssozonoff 0:74df6ab91c79 200 float temperatureC; // variable for result
ssozonoff 0:74df6ab91c79 201 u16sT &= ~0x0003; // clear bits [1..0] (status bits)
ssozonoff 1:d0f691423bf1 202 //-- calculate temperature [�C] --
ssozonoff 0:74df6ab91c79 203 temperatureC= -46.85 + 175.72/65536 *(float)u16sT; //T= -46.85 + 175.72 * ST/2^16
ssozonoff 0:74df6ab91c79 204 return temperatureC;
ssozonoff 0:74df6ab91c79 205 }
ssozonoff 0:74df6ab91c79 206 //==============================================================================//
ssozonoff 0:74df6ab91c79 207 int SHT2x::SHT2x_GetSerialNumber(int u8SerialNumber[])
ssozonoff 0:74df6ab91c79 208 //==============================================================================//
ssozonoff 0:74df6ab91c79 209 {
ssozonoff 0:74df6ab91c79 210 int error=0; //error variable
ssozonoff 1:d0f691423bf1 211 //Read from memory location 1
ssozonoff 0:74df6ab91c79 212 i2c.start();
ssozonoff 1:d0f691423bf1 213 error |= i2c.write(I2C_ADR_W);
ssozonoff 0:74df6ab91c79 214 error |= i2c.write(0xFA); //Command for readout on-chip memory
ssozonoff 0:74df6ab91c79 215 error |= i2c.write(0x0F); //on-chip memory address
ssozonoff 0:74df6ab91c79 216 i2c.start();
ssozonoff 0:74df6ab91c79 217 error |= i2c.write(I2C_ADR_R); //I2C address
ssozonoff 1:d0f691423bf1 218 u8SerialNumber[5] = i2c.read(ACK); //Read SNB_3
ssozonoff 1:d0f691423bf1 219 i2c.read(ACK); //Read CRC SNB_3 (CRC is not analyzed)
ssozonoff 1:d0f691423bf1 220 u8SerialNumber[4] = i2c.read(ACK); //Read SNB_2
ssozonoff 1:d0f691423bf1 221 i2c.read(ACK); //Read CRC SNB_2 (CRC is not analyzed)
ssozonoff 1:d0f691423bf1 222 u8SerialNumber[3] = i2c.read(ACK); //Read SNB_1Sample Code SHT21
ssozonoff 1:d0f691423bf1 223 i2c.read(ACK); //Read CRC SNB_1 (CRC is not analyzed)
ssozonoff 1:d0f691423bf1 224 u8SerialNumber[2] = i2c.read(ACK); //Read SNB_0
ssozonoff 1:d0f691423bf1 225 i2c.read(NoACK); //Read CRC SNB_0 (CRC is not analyzed)
ssozonoff 0:74df6ab91c79 226 i2c.stop();
ssozonoff 0:74df6ab91c79 227 //Read from memory location 2
ssozonoff 0:74df6ab91c79 228 i2c.start();
ssozonoff 0:74df6ab91c79 229 error |= i2c.write(I2C_ADR_W); //I2C address
ssozonoff 0:74df6ab91c79 230 error |= i2c.write(0xFC); //Command for readout on-chip memory
ssozonoff 0:74df6ab91c79 231 error |= i2c.write(0xC9); //on-chip memory address
ssozonoff 0:74df6ab91c79 232 i2c.start();
ssozonoff 0:74df6ab91c79 233 error |= i2c.write(I2C_ADR_R); //I2C address
ssozonoff 1:d0f691423bf1 234 u8SerialNumber[1] = i2c.read(ACK); //Read SNC_1
ssozonoff 1:d0f691423bf1 235 u8SerialNumber[0] = i2c.read(ACK); //Read SNC_0
ssozonoff 1:d0f691423bf1 236 i2c.read(ACK);
ssozonoff 1:d0f691423bf1 237 u8SerialNumber[7] = i2c.read(ACK); //Read SNA_1
ssozonoff 1:d0f691423bf1 238 u8SerialNumber[6] = i2c.read(ACK); //Read SNA_0
ssozonoff 1:d0f691423bf1 239 i2c.read(NoACK); //Read CRC SNA0/1 (CRC is not analyzed)
ssozonoff 0:74df6ab91c79 240 i2c.stop();
ssozonoff 0:74df6ab91c79 241 return error;
ssozonoff 0:74df6ab91c79 242 }