htu21d  2.0.0.0
htu21d.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef HTU21D_H
36 #define HTU21D_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_i2c_master.h"
50 
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define HTU21D_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA )
65 
71 #define HTU21D_RETVAL uint8_t
72 
73 #define HTU21D_OK 0x00
74 #define HTU21D_INIT_ERROR 0xFF
75 
82 #define HTU21D_I2C_ADDRESS 0x40
83 
89 #define HTU21D_USER_REG_WRITE 0xE6
90 
96 #define HTU21D_USER_REG_READ 0xE7
97 
103 #define HTU21D_HEATER_REG_WRITE 0x51
104 
110 #define HTU21D_HEATER_REG_READ 0x11
111 
117 #define HTU21D_SOFT_RESET 0xFE
118 
124 #define HTU21D_SERIAL1_READ1 0xFA
125 
131 #define HTU21D_SERIAL1_READ2 0x0F
132 
138 #define HTU21D_SERIAL2_READ1 0xFC
139 
145 #define HTU21D_SERIAL2_READ2 0xC9
146 
152 #define HTU21D_SI7013_CHIPID 0x0D
153 
159 #define HTU21D_SI7020_CHIPID 0x14
160 
166 #define HTU21D_SI7021_CHIPID 0x15
167 
173 #define HTU21D_CHIPID 0x32
174 
180 #define HTU21D_FIRMWARE_READ1 0x84
181 
187 #define HTU21D_FIRMWARE_READ2 0xB8
188 
194 #define HTU21D_FIRMWARE_V1 0xFF
195 
201 #define HTU21D_FIRMWARE_V2 0x20
202 
208 #define HTU21D_FORCE_READ_TEMP 0xFE
209 
215 #define HTU21D_ERROR 0xFF
216 
222 #define HTU21D_RES_RH12_TEMP14 0x00
223 
229 #define HTU21D_RES_RH8_TEMP12 0x01
230 
236 #define HTU21D_RES_RH10_TEMP13 0x80
237 
243 #define HTU21D_RES_RH11_TEMP11 0x81
244 
250 #define HTU21D_TRIG_HUMD_MEASURE_HOLD 0xE5
251 
257 #define HTU21D_TRIG_HUMD_MEASURE_NOHOLD 0xF5
258 
264 #define HTU21D_TRIG_TEMP_MEASURE_HOLD 0xE3
265 
271 #define HTU21D_TRIG_TEMP_MEASURE_NOHOLD 0xF3
272 
278 #define HTU21D_ON 0x04
279 
285 #define HTU21D_OFF 0xFB
286 
292 #define HTU21D_CRC8_POLYNOMINAL 0x1310
293 
299 #define HTU21D_DATA_FILTER 0xFFFC
300 #define HTU21D_DATA_RESOLUTION 65536.0
301 #define HTU21D_TEMP_CALC_COEF 175.72
302 #define HTU21D_TEMP_STB_COEF -46.85
303 #define HTU21D_HUMI_CALC_COEF 125.0
304 #define HTU21D_HUMI_STB_COEF 6.0
305  // End group macro
308 // --------------------------------------------------------------- PUBLIC TYPES
317 typedef struct
318 {
319  // Modules
320 
321  i2c_master_t i2c;
322 
323  // ctx variable
324 
325  uint8_t slave_address;
326 
327 } htu21d_t;
328 
332 typedef struct
333 {
334  // Communication gpio pins
335 
336  pin_name_t scl;
337  pin_name_t sda;
338 
339  // static variable
340 
341  uint32_t i2c_speed;
342  uint8_t i2c_address;
343 
344 } htu21d_cfg_t;
345  // End types group
347 // ------------------------------------------------------------------ CONSTANTS // End constants group
356 // ------------------------------------------------------------------ VARIABLES // End variable group
364 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
365 
371 #ifdef __cplusplus
372 extern "C"{
373 #endif
374 
384 
393 
404 void htu21d_generic_write ( htu21d_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
405 
417 void htu21d_generic_read ( htu21d_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
418 
429 uint32_t htu21d_read_data ( htu21d_t *ctx, uint8_t reg_adr );
430 
441 void htu21d_send_cmd ( htu21d_t *ctx, uint8_t cmd_byte );
442 
453 void htu21d_generic_read ( htu21d_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
454 
466 
478 
490 #ifdef __cplusplus
491 }
492 #endif
493 #endif // _HTU21D_H_
494  // End public_function group
497 
498 // ------------------------------------------------------------------------- END
htu21d_cfg_t::scl
pin_name_t scl
Definition: htu21d.h:336
htu21d_get_temperature
float htu21d_get_temperature(htu21d_t *ctx)
Get temperature value function.
htu21d_t
Click ctx object definition.
Definition: htu21d.h:318
htu21d_cfg_setup
void htu21d_cfg_setup(htu21d_cfg_t *cfg)
Config Object Initialization function.
htu21d_generic_write
void htu21d_generic_write(htu21d_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
htu21d_cfg_t::sda
pin_name_t sda
Definition: htu21d.h:337
HTU21D_RETVAL
#define HTU21D_RETVAL
Definition: htu21d.h:71
htu21d_send_cmd
void htu21d_send_cmd(htu21d_t *ctx, uint8_t cmd_byte)
Send command function.
htu21d_cfg_t::i2c_address
uint8_t i2c_address
Definition: htu21d.h:342
htu21d_init
HTU21D_RETVAL htu21d_init(htu21d_t *ctx, htu21d_cfg_t *cfg)
Initialization function.
htu21d_t::slave_address
uint8_t slave_address
Definition: htu21d.h:325
htu21d_generic_read
void htu21d_generic_read(htu21d_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
htu21d_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: htu21d.h:341
htu21d_read_data
uint32_t htu21d_read_data(htu21d_t *ctx, uint8_t reg_adr)
Generic read 24-bit data function.
htu21d_t::i2c
i2c_master_t i2c
Definition: htu21d.h:321
htu21d_get_humidity
float htu21d_get_humidity(htu21d_t *ctx)
Get humidity value function.
htu21d_cfg_t
Click configuration structure definition.
Definition: htu21d.h:333