123

Dependencies:   mbed HTS221 LPS25HB

Committer:
Simon_mbed
Date:
Mon Apr 06 08:54:17 2020 +0000
Revision:
0:2365a00ff7b6
13

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon_mbed 0:2365a00ff7b6 1 #include "mbed.h"
Simon_mbed 0:2365a00ff7b6 2 #include "HTS221_driver.h"
Simon_mbed 0:2365a00ff7b6 3
Simon_mbed 0:2365a00ff7b6 4 /*------------------------------------------------------------------------------
Simon_mbed 0:2365a00ff7b6 5 Before to use this example, ensure that you an hyperterminal installed on your
Simon_mbed 0:2365a00ff7b6 6 computer. More info here: https://developer.mbed.org/handbook/Terminals
Simon_mbed 0:2365a00ff7b6 7
Simon_mbed 0:2365a00ff7b6 8 The default serial comm port uses the SERIAL_TX and SERIAL_RX pins (see their
Simon_mbed 0:2365a00ff7b6 9 definition in the PinNames.h file).
Simon_mbed 0:2365a00ff7b6 10
Simon_mbed 0:2365a00ff7b6 11 The default serial configuration in this case is 9600 bauds, 8-bit data, no parity
Simon_mbed 0:2365a00ff7b6 12
Simon_mbed 0:2365a00ff7b6 13 If you want to change the baudrate for example, you have to redeclare the
Simon_mbed 0:2365a00ff7b6 14 serial object in your code:
Simon_mbed 0:2365a00ff7b6 15
Simon_mbed 0:2365a00ff7b6 16 Serial pc(SERIAL_TX, SERIAL_RX);
Simon_mbed 0:2365a00ff7b6 17
Simon_mbed 0:2365a00ff7b6 18 Then, you can modify the baudrate and print like this:
Simon_mbed 0:2365a00ff7b6 19
Simon_mbed 0:2365a00ff7b6 20 pc.baud(115200);
Simon_mbed 0:2365a00ff7b6 21 pc.printf("Hello World !\n");
Simon_mbed 0:2365a00ff7b6 22 ------------------------------------------------------------------------------*/
Simon_mbed 0:2365a00ff7b6 23
Simon_mbed 0:2365a00ff7b6 24 //https://www.cnblogs.com/zlbg/p/4216251.html
Simon_mbed 0:2365a00ff7b6 25
Simon_mbed 0:2365a00ff7b6 26 DigitalOut myled(LED1);
Simon_mbed 0:2365a00ff7b6 27 DigitalOut led(LED1);
Simon_mbed 0:2365a00ff7b6 28
Simon_mbed 0:2365a00ff7b6 29 I2C i2c(I2C_SDA, I2C_SCL);
Simon_mbed 0:2365a00ff7b6 30 HTS221Sensor hts221;
Simon_mbed 0:2365a00ff7b6 31 char data_write[2];
Simon_mbed 0:2365a00ff7b6 32 char data_read[4];
Simon_mbed 0:2365a00ff7b6 33
Simon_mbed 0:2365a00ff7b6 34
Simon_mbed 0:2365a00ff7b6 35 float t0, t1, h0, h1;
Simon_mbed 0:2365a00ff7b6 36 int t0out, t1out, h0out, h1out;
Simon_mbed 0:2365a00ff7b6 37
Simon_mbed 0:2365a00ff7b6 38 int16_t outHumi = 0;
Simon_mbed 0:2365a00ff7b6 39 int16_t outTemp = 0;
Simon_mbed 0:2365a00ff7b6 40
Simon_mbed 0:2365a00ff7b6 41 float valueHumi = 0;
Simon_mbed 0:2365a00ff7b6 42 float valueTemp = 0;
Simon_mbed 0:2365a00ff7b6 43
Simon_mbed 0:2365a00ff7b6 44
Simon_mbed 0:2365a00ff7b6 45
Simon_mbed 0:2365a00ff7b6 46 float mapFloat(int x, int in_min, int in_max, float out_min, float out_max)
Simon_mbed 0:2365a00ff7b6 47 {
Simon_mbed 0:2365a00ff7b6 48 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
Simon_mbed 0:2365a00ff7b6 49 }
Simon_mbed 0:2365a00ff7b6 50
Simon_mbed 0:2365a00ff7b6 51
Simon_mbed 0:2365a00ff7b6 52 void read_calidata0()
Simon_mbed 0:2365a00ff7b6 53 {
Simon_mbed 0:2365a00ff7b6 54
Simon_mbed 0:2365a00ff7b6 55 //Read Cali Data
Simon_mbed 0:2365a00ff7b6 56
Simon_mbed 0:2365a00ff7b6 57 //read out t0degCx8, t1degCx8
Simon_mbed 0:2365a00ff7b6 58 data_write[0] = HTS221_T0_DEGC_X8;
Simon_mbed 0:2365a00ff7b6 59 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 60 i2c.read(0xBF,data_read, 4, 0);
Simon_mbed 0:2365a00ff7b6 61
Simon_mbed 0:2365a00ff7b6 62
Simon_mbed 0:2365a00ff7b6 63
Simon_mbed 0:2365a00ff7b6 64
Simon_mbed 0:2365a00ff7b6 65 int t0degCx8 = ((data_read[3] & 0b00000011) << 8) | data_read[0];
Simon_mbed 0:2365a00ff7b6 66 int t1degCx8 = ((data_read[3] & 0b00001100) << 6) | data_read[1];
Simon_mbed 0:2365a00ff7b6 67 t0 = t0degCx8/8.0;
Simon_mbed 0:2365a00ff7b6 68 t1 = t1degCx8/8.0;
Simon_mbed 0:2365a00ff7b6 69
Simon_mbed 0:2365a00ff7b6 70 //read out t0out, t1out
Simon_mbed 0:2365a00ff7b6 71
Simon_mbed 0:2365a00ff7b6 72 data_write[0] = HTS221_T0_OUT_L |0x80;
Simon_mbed 0:2365a00ff7b6 73 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 74 i2c.read(0xBF,data_read, 4, 0);
Simon_mbed 0:2365a00ff7b6 75
Simon_mbed 0:2365a00ff7b6 76 t0out = (data_read[1] << 8) | data_read[0];
Simon_mbed 0:2365a00ff7b6 77 t1out = (data_read[3] << 8) | data_read[2];
Simon_mbed 0:2365a00ff7b6 78
Simon_mbed 0:2365a00ff7b6 79
Simon_mbed 0:2365a00ff7b6 80 //read out h0RHx2, h1RHx2
Simon_mbed 0:2365a00ff7b6 81 data_write[0] = HTS221_H0_RH_X2 |0x80;
Simon_mbed 0:2365a00ff7b6 82 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 83 i2c.read(0xBF,data_read, 2, 0);
Simon_mbed 0:2365a00ff7b6 84
Simon_mbed 0:2365a00ff7b6 85 unsigned char h0RHx2 = data_read[0];
Simon_mbed 0:2365a00ff7b6 86 unsigned char h1RHx2 = data_read[1];
Simon_mbed 0:2365a00ff7b6 87
Simon_mbed 0:2365a00ff7b6 88 h0 = h0RHx2/2.0;
Simon_mbed 0:2365a00ff7b6 89 h1 = h1RHx2/2.0;
Simon_mbed 0:2365a00ff7b6 90
Simon_mbed 0:2365a00ff7b6 91 //read out h0t0Out
Simon_mbed 0:2365a00ff7b6 92
Simon_mbed 0:2365a00ff7b6 93 data_write[0] = HTS221_H0_T0_OUT_L |0x80;
Simon_mbed 0:2365a00ff7b6 94 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 95 i2c.read(0xBF,data_read, 2, 0);
Simon_mbed 0:2365a00ff7b6 96
Simon_mbed 0:2365a00ff7b6 97 h0out = (data_read[1] << 8) | data_read[0];
Simon_mbed 0:2365a00ff7b6 98
Simon_mbed 0:2365a00ff7b6 99
Simon_mbed 0:2365a00ff7b6 100 //read out h1t0Out
Simon_mbed 0:2365a00ff7b6 101 data_write[0] = HTS221_H1_T0_OUT_L |0x80;
Simon_mbed 0:2365a00ff7b6 102 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 103 i2c.read(0xBF,data_read, 2, 0);
Simon_mbed 0:2365a00ff7b6 104
Simon_mbed 0:2365a00ff7b6 105 h1out = (data_read[1] << 8) | data_read[0];
Simon_mbed 0:2365a00ff7b6 106
Simon_mbed 0:2365a00ff7b6 107 //print the calibration coefficients
Simon_mbed 0:2365a00ff7b6 108 printf("Calibration coefficients: \r\n");
Simon_mbed 0:2365a00ff7b6 109 printf("t0 =%f `C t0out = %d \r\n",t0,t0out);
Simon_mbed 0:2365a00ff7b6 110 printf("t1 =%f `C t1out = %d \r\n",t1,t1out);
Simon_mbed 0:2365a00ff7b6 111 printf("h0 =%f RH h0out = %d \r\n",h0,h0out);
Simon_mbed 0:2365a00ff7b6 112 printf("h1 =%f RH h1out = %d \r\n",h1,h1out);
Simon_mbed 0:2365a00ff7b6 113 printf("------------------------\r\n");
Simon_mbed 0:2365a00ff7b6 114
Simon_mbed 0:2365a00ff7b6 115 }
Simon_mbed 0:2365a00ff7b6 116
Simon_mbed 0:2365a00ff7b6 117 HTS221_Error_et HTS221_Get_Temperature1(int16_t *value)
Simon_mbed 0:2365a00ff7b6 118 {
Simon_mbed 0:2365a00ff7b6 119 int16_t T0_out,T1_out,T_out,T0_degC_x8_u16,T1_degC_x8_u16;
Simon_mbed 0:2365a00ff7b6 120 int16_t T0_degC, T1_degC;
Simon_mbed 0:2365a00ff7b6 121 uint8_t buffer[4], tmp;
Simon_mbed 0:2365a00ff7b6 122 uint32_t tmp32;
Simon_mbed 0:2365a00ff7b6 123
Simon_mbed 0:2365a00ff7b6 124 /*1. Read from 0x32 & 0x33 registers the value of coefficients T0_d egC_x8 and T1_de gC_x8*/
Simon_mbed 0:2365a00ff7b6 125 data_write[0] = HTS221_T0_DEGC_X8;
Simon_mbed 0:2365a00ff7b6 126 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 127 i2c.read(0xBF,(char*)buffer, 2, 0);
Simon_mbed 0:2365a00ff7b6 128
Simon_mbed 0:2365a00ff7b6 129 /*2. Read from 0x35 register the value of the MSB bits of T1_deg C and T0_deg C */
Simon_mbed 0:2365a00ff7b6 130 data_write[0] = HTS221_T0_T1_DEGC_H2;
Simon_mbed 0:2365a00ff7b6 131 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 132 i2c.read(0xBF,(char*)&tmp, 1, 0);
Simon_mbed 0:2365a00ff7b6 133
Simon_mbed 0:2365a00ff7b6 134 /*Calculate the T0_deg C and T1_deg C values*/
Simon_mbed 0:2365a00ff7b6 135
Simon_mbed 0:2365a00ff7b6 136 T0_degC_x8_u16 = (((uint16_t)(tmp & 0x03)) << 8) | ((uint16_t)buffer[0]);
Simon_mbed 0:2365a00ff7b6 137 T1_degC_x8_u16 = (((uint16_t)(tmp & 0x0C)) << 6) | ((uint16_t)buffer[1]);
Simon_mbed 0:2365a00ff7b6 138 T0_degC = T0_degC_x8_u16>>3;
Simon_mbed 0:2365a00ff7b6 139 T1_degC = T1_degC_x8_u16>>3;
Simon_mbed 0:2365a00ff7b6 140
Simon_mbed 0:2365a00ff7b6 141 /*3. Read from 0x3C & 0x3D registers the value of T0_OUT*/
Simon_mbed 0:2365a00ff7b6 142 /*4. Read from 0x3E & 0x3F registers the value of T1_OUT*/
Simon_mbed 0:2365a00ff7b6 143
Simon_mbed 0:2365a00ff7b6 144 data_write[0] = HTS221_T0_OUT_L |0x80;
Simon_mbed 0:2365a00ff7b6 145 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 146 i2c.read(0xBF,(char*)buffer, 4, 0);
Simon_mbed 0:2365a00ff7b6 147
Simon_mbed 0:2365a00ff7b6 148 T0_out = (((uint16_t)buffer[1])<<8) | (uint16_t)buffer[0];
Simon_mbed 0:2365a00ff7b6 149 T1_out = (((uint16_t)buffer[3])<<8) | (uint16_t)buffer[2];
Simon_mbed 0:2365a00ff7b6 150
Simon_mbed 0:2365a00ff7b6 151 /* 5.Read from 0x2A & 0x2B registers the value T_OUT (ADC _OUT).*/
Simon_mbed 0:2365a00ff7b6 152
Simon_mbed 0:2365a00ff7b6 153 data_write[0] = HTS221_TEMP_OUT_L_REG;
Simon_mbed 0:2365a00ff7b6 154 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 155 i2c.read(0xBF,(char*)buffer, 2, 0);
Simon_mbed 0:2365a00ff7b6 156
Simon_mbed 0:2365a00ff7b6 157 T_out = (((uint16_t)buffer[1])<<8) | (uint16_t)buffer[0];
Simon_mbed 0:2365a00ff7b6 158
Simon_mbed 0:2365a00ff7b6 159 /* 6. Compute the Temperature value by linea r interpolation*/
Simon_mbed 0:2365a00ff7b6 160 tmp32 = ((uint32_t)(T_out - T0_out)) * ((uint32_t)(T1_degC - T0_degC)*10);
Simon_mbed 0:2365a00ff7b6 161 *value = tmp32 /(T1_out - T0_out) + T0_degC*10;
Simon_mbed 0:2365a00ff7b6 162
Simon_mbed 0:2365a00ff7b6 163 return HTS221_OK;
Simon_mbed 0:2365a00ff7b6 164
Simon_mbed 0:2365a00ff7b6 165
Simon_mbed 0:2365a00ff7b6 166 }
Simon_mbed 0:2365a00ff7b6 167
Simon_mbed 0:2365a00ff7b6 168 int main()
Simon_mbed 0:2365a00ff7b6 169 {
Simon_mbed 0:2365a00ff7b6 170 HTS221_Error_et ret;
Simon_mbed 0:2365a00ff7b6 171 uint8_t deviceid;
Simon_mbed 0:2365a00ff7b6 172
Simon_mbed 0:2365a00ff7b6 173 HTS221_Get_DeviceID(&deviceid);
Simon_mbed 0:2365a00ff7b6 174
Simon_mbed 0:2365a00ff7b6 175 HTS221_Init_st init_st;
Simon_mbed 0:2365a00ff7b6 176 init_st.avg_h = HTS221_AVGH_4;
Simon_mbed 0:2365a00ff7b6 177 init_st.avg_t = HTS221_AVGT_4;
Simon_mbed 0:2365a00ff7b6 178 init_st.odr = HTS221_ODR_1HZ;
Simon_mbed 0:2365a00ff7b6 179 init_st.bdu_status = HTS221_ENABLE;
Simon_mbed 0:2365a00ff7b6 180 init_st.heater_status = HTS221_ENABLE;
Simon_mbed 0:2365a00ff7b6 181 init_st.irq_level = HTS221_HIGH_LVL;
Simon_mbed 0:2365a00ff7b6 182 init_st.irq_output_type = HTS221_PUSHPULL;
Simon_mbed 0:2365a00ff7b6 183 init_st.irq_enable = HTS221_DISABLE;
Simon_mbed 0:2365a00ff7b6 184
Simon_mbed 0:2365a00ff7b6 185 HTS221_Set_InitConfig(&init_st);
Simon_mbed 0:2365a00ff7b6 186
Simon_mbed 0:2365a00ff7b6 187
Simon_mbed 0:2365a00ff7b6 188
Simon_mbed 0:2365a00ff7b6 189 if(deviceid != HTS221_WHO_AM_I_VAL) {
Simon_mbed 0:2365a00ff7b6 190 printf("HTS221 Not Found!\r\n");
Simon_mbed 0:2365a00ff7b6 191 while(1) {
Simon_mbed 0:2365a00ff7b6 192 myled=!myled;
Simon_mbed 0:2365a00ff7b6 193 wait(0.2);
Simon_mbed 0:2365a00ff7b6 194 }
Simon_mbed 0:2365a00ff7b6 195 } else {
Simon_mbed 0:2365a00ff7b6 196 printf("HTS221 Has Found!\r\n");
Simon_mbed 0:2365a00ff7b6 197 }
Simon_mbed 0:2365a00ff7b6 198
Simon_mbed 0:2365a00ff7b6 199 HTS221_Activate();
Simon_mbed 0:2365a00ff7b6 200
Simon_mbed 0:2365a00ff7b6 201
Simon_mbed 0:2365a00ff7b6 202
Simon_mbed 0:2365a00ff7b6 203 HTS221_BitStatus_et mode;
Simon_mbed 0:2365a00ff7b6 204 HTS221_Get_PowerDownMode(&mode);
Simon_mbed 0:2365a00ff7b6 205 if(mode == HTS221_SET) {
Simon_mbed 0:2365a00ff7b6 206 printf("HTS221 in power down mode\r\n");
Simon_mbed 0:2365a00ff7b6 207
Simon_mbed 0:2365a00ff7b6 208 ret = HTS221_Set_PowerDownMode(HTS221_RESET);
Simon_mbed 0:2365a00ff7b6 209 if(ret != HTS221_OK) {
Simon_mbed 0:2365a00ff7b6 210 printf("HTS221_Set_PowerDownMode failed");
Simon_mbed 0:2365a00ff7b6 211 }
Simon_mbed 0:2365a00ff7b6 212 }
Simon_mbed 0:2365a00ff7b6 213 HTS221_Get_PowerDownMode(&mode);
Simon_mbed 0:2365a00ff7b6 214 if(mode == HTS221_SET) {
Simon_mbed 0:2365a00ff7b6 215 printf("HTS221 in power down mode\r\n");
Simon_mbed 0:2365a00ff7b6 216
Simon_mbed 0:2365a00ff7b6 217 ret = HTS221_Set_PowerDownMode(HTS221_RESET);
Simon_mbed 0:2365a00ff7b6 218 if(ret != HTS221_OK) {
Simon_mbed 0:2365a00ff7b6 219 printf("HTS221_Set_PowerDownMode failed");
Simon_mbed 0:2365a00ff7b6 220 }
Simon_mbed 0:2365a00ff7b6 221 }
Simon_mbed 0:2365a00ff7b6 222
Simon_mbed 0:2365a00ff7b6 223 HTS221_Set_BduMode(HTS221_DISABLE);
Simon_mbed 0:2365a00ff7b6 224
Simon_mbed 0:2365a00ff7b6 225 uint16_t humidity;
Simon_mbed 0:2365a00ff7b6 226 int16_t temperature;
Simon_mbed 0:2365a00ff7b6 227 uint16_t humi;
Simon_mbed 0:2365a00ff7b6 228 int16_t temp;
Simon_mbed 0:2365a00ff7b6 229 int16_t temp2;
Simon_mbed 0:2365a00ff7b6 230
Simon_mbed 0:2365a00ff7b6 231
Simon_mbed 0:2365a00ff7b6 232
Simon_mbed 0:2365a00ff7b6 233 HTS221_DrdyLevel_et level = HTS221_LOW_LVL ;
Simon_mbed 0:2365a00ff7b6 234
Simon_mbed 0:2365a00ff7b6 235 printf("pre level=%02x\r\n",level);
Simon_mbed 0:2365a00ff7b6 236
Simon_mbed 0:2365a00ff7b6 237 HTS221_Set_IrqActiveLevel(HTS221_HIGH_LVL);
Simon_mbed 0:2365a00ff7b6 238
Simon_mbed 0:2365a00ff7b6 239
Simon_mbed 0:2365a00ff7b6 240 HTS221_Get_IrqActiveLevel(&level);
Simon_mbed 0:2365a00ff7b6 241
Simon_mbed 0:2365a00ff7b6 242 printf("aft level=%d\r\n",level);
Simon_mbed 0:2365a00ff7b6 243
Simon_mbed 0:2365a00ff7b6 244
Simon_mbed 0:2365a00ff7b6 245 // while(1) {
Simon_mbed 0:2365a00ff7b6 246 // HTS221_StartOneShotMeasurement();
Simon_mbed 0:2365a00ff7b6 247 //
Simon_mbed 0:2365a00ff7b6 248 // ret = HTS221_Get_Measurement(&humidity,&temperature);
Simon_mbed 0:2365a00ff7b6 249 // if(ret==HTS221_OK){
Simon_mbed 0:2365a00ff7b6 250 // printf("humidity:%d%%,temperature:%dC\r\n",humidity/10,temperature/10);
Simon_mbed 0:2365a00ff7b6 251 // }else
Simon_mbed 0:2365a00ff7b6 252 // {
Simon_mbed 0:2365a00ff7b6 253 // printf("GET Measurement failed\r\n");
Simon_mbed 0:2365a00ff7b6 254 // }
Simon_mbed 0:2365a00ff7b6 255 // ret = HTS221_Get_Humidity(&humi);
Simon_mbed 0:2365a00ff7b6 256 // if(ret==HTS221_OK){
Simon_mbed 0:2365a00ff7b6 257 // printf("humi:%d\r\n",humi);
Simon_mbed 0:2365a00ff7b6 258 // }else
Simon_mbed 0:2365a00ff7b6 259 // {
Simon_mbed 0:2365a00ff7b6 260 // printf("GET humi failed\r\n");
Simon_mbed 0:2365a00ff7b6 261 // }
Simon_mbed 0:2365a00ff7b6 262 //
Simon_mbed 0:2365a00ff7b6 263 // ret = HTS221_Get_Temperature(&temp);
Simon_mbed 0:2365a00ff7b6 264 // if(ret==HTS221_OK){
Simon_mbed 0:2365a00ff7b6 265 // printf("temp:%d\r\n",temp);
Simon_mbed 0:2365a00ff7b6 266 // }else
Simon_mbed 0:2365a00ff7b6 267 // {
Simon_mbed 0:2365a00ff7b6 268 // printf("GET temp failed\r\n");
Simon_mbed 0:2365a00ff7b6 269 // }
Simon_mbed 0:2365a00ff7b6 270 //
Simon_mbed 0:2365a00ff7b6 271 //
Simon_mbed 0:2365a00ff7b6 272 //
Simon_mbed 0:2365a00ff7b6 273 // ret = HTS221_Get_Temperature1(&temp2);
Simon_mbed 0:2365a00ff7b6 274 //
Simon_mbed 0:2365a00ff7b6 275 // if(ret==HTS221_OK){
Simon_mbed 0:2365a00ff7b6 276 // printf("temp2:%d\r\n",temp2);
Simon_mbed 0:2365a00ff7b6 277 // }else
Simon_mbed 0:2365a00ff7b6 278 // {
Simon_mbed 0:2365a00ff7b6 279 // printf("GET temp2 failed\r\n");
Simon_mbed 0:2365a00ff7b6 280 // }
Simon_mbed 0:2365a00ff7b6 281 //
Simon_mbed 0:2365a00ff7b6 282 //
Simon_mbed 0:2365a00ff7b6 283 // wait(1);
Simon_mbed 0:2365a00ff7b6 284 // }
Simon_mbed 0:2365a00ff7b6 285
Simon_mbed 0:2365a00ff7b6 286
Simon_mbed 0:2365a00ff7b6 287
Simon_mbed 0:2365a00ff7b6 288 //Find the HTS221
Simon_mbed 0:2365a00ff7b6 289 data_write[0] = HTS221_WHO_AM_I_REG;
Simon_mbed 0:2365a00ff7b6 290 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 291 i2c.read(0xBF,data_read, 1, 0);
Simon_mbed 0:2365a00ff7b6 292
Simon_mbed 0:2365a00ff7b6 293
Simon_mbed 0:2365a00ff7b6 294 //turn on the HTS221, set the update mode to one shot
Simon_mbed 0:2365a00ff7b6 295 data_write[0] = HTS221_CTRL_REG1;
Simon_mbed 0:2365a00ff7b6 296 data_write[1] = 0x84;
Simon_mbed 0:2365a00ff7b6 297 int status = i2c.write(0xBE, data_write, 2, 0);
Simon_mbed 0:2365a00ff7b6 298 if(status!=0) {
Simon_mbed 0:2365a00ff7b6 299 while(1) {
Simon_mbed 0:2365a00ff7b6 300 myled=!myled;
Simon_mbed 0:2365a00ff7b6 301 wait(0.2);
Simon_mbed 0:2365a00ff7b6 302 }
Simon_mbed 0:2365a00ff7b6 303 }
Simon_mbed 0:2365a00ff7b6 304
Simon_mbed 0:2365a00ff7b6 305 read_calidata0();
Simon_mbed 0:2365a00ff7b6 306
Simon_mbed 0:2365a00ff7b6 307
Simon_mbed 0:2365a00ff7b6 308 while(1) {
Simon_mbed 0:2365a00ff7b6 309 //perform a measurement
Simon_mbed 0:2365a00ff7b6 310 data_write[0] = HTS221_CTRL_REG2;
Simon_mbed 0:2365a00ff7b6 311 data_write[1] = 0x01;
Simon_mbed 0:2365a00ff7b6 312 i2c.write(0xBE, data_write, 2, 0);
Simon_mbed 0:2365a00ff7b6 313 //check the status
Simon_mbed 0:2365a00ff7b6 314 status = 0;
Simon_mbed 0:2365a00ff7b6 315 while (status != 0x03) { //typical conversition time: 3ms
Simon_mbed 0:2365a00ff7b6 316
Simon_mbed 0:2365a00ff7b6 317
Simon_mbed 0:2365a00ff7b6 318 wait(0.3);
Simon_mbed 0:2365a00ff7b6 319 data_write[0] = HTS221_STATUS_REG;
Simon_mbed 0:2365a00ff7b6 320 i2c.write(0xBE, data_write, 1, 0);
Simon_mbed 0:2365a00ff7b6 321 i2c.read(0xBF,data_read, 1, 0);
Simon_mbed 0:2365a00ff7b6 322 status = data_read[0];
Simon_mbed 0:2365a00ff7b6 323
Simon_mbed 0:2365a00ff7b6 324 wait(0.1);
Simon_mbed 0:2365a00ff7b6 325 // printf("status:%d\r\n",status);
Simon_mbed 0:2365a00ff7b6 326 }
Simon_mbed 0:2365a00ff7b6 327
Simon_mbed 0:2365a00ff7b6 328 //read multiple bytes incrementing the register address
Simon_mbed 0:2365a00ff7b6 329
Simon_mbed 0:2365a00ff7b6 330 data_write[0] = HTS221_HR_OUT_L_REG |0x80;
Simon_mbed 0:2365a00ff7b6 331 i2c.write(0xBE, data_write, 1, 1);
Simon_mbed 0:2365a00ff7b6 332 i2c.read(0xBF,data_read, 4, 0);
Simon_mbed 0:2365a00ff7b6 333
Simon_mbed 0:2365a00ff7b6 334 outHumi = (data_read[1] << 8) | data_read[0];
Simon_mbed 0:2365a00ff7b6 335 outTemp = (data_read[3] << 8) | data_read[2];
Simon_mbed 0:2365a00ff7b6 336
Simon_mbed 0:2365a00ff7b6 337 valueTemp = mapFloat(outTemp, t0out, t1out, t0, t1);
Simon_mbed 0:2365a00ff7b6 338 valueHumi = mapFloat(outHumi, h0out, h1out, h0, h1);
Simon_mbed 0:2365a00ff7b6 339
Simon_mbed 0:2365a00ff7b6 340 printf("Temp:%f `C Humi:%f RH \r\n",valueTemp,valueHumi);
Simon_mbed 0:2365a00ff7b6 341
Simon_mbed 0:2365a00ff7b6 342
Simon_mbed 0:2365a00ff7b6 343
Simon_mbed 0:2365a00ff7b6 344 led = !led; // Toggle LED
Simon_mbed 0:2365a00ff7b6 345 wait(3); // 1 second
Simon_mbed 0:2365a00ff7b6 346 }
Simon_mbed 0:2365a00ff7b6 347 }