sht  2.0.0.0
sht.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 SHT_H
36 #define SHT_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_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define SHT_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
66  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
67 
73 #define SHT_OK 0x00
74 #define SHT_ERROR 0xFF
75 
77 #define SHT_I2C_ADDR0 0x44
78 #define SHT_I2C_ADDR1 0x45
79 
84 #define SHT_MPS_05 0x20 // 0.5 measurements per second
85 #define SHT_MPS_1 0x21 // 1 measurements per second
86 #define SHT_MPS_2 0x22 // 2 measurements per second
87 #define SHT_MPS_4 0x23 // 4 measurements per second
88 #define SHT_MPS_10 0x27 // 10 measurements per second
89 
95 #define SHT_RPT_HIGH 0 // High repeatability
96 #define SHT_RPT_MEDIUM 1 // Medium repeatability
97 #define SHT_RPT_LOW 2 // Low repeatability
98 #define SHT_STR_ENABLE 0x2C // Stretching enabled
99 #define SHT_STR_DISABLE 0x24 // Stretching disabled
100 
106 #define SHT_CRC_POLYNOMIAL 0x31
107 #define SHT_FETCH_DATA 0xE000
108 #define SHT_PERIODIC_ART 0x2B32
109 #define SHT_BREAK 0x3093
110 #define SHT_SOFT_RESET 0x30A2
111 #define SHT_HEATER 0x30
112 #define SHT_READ_STATUS 0xF32D
113 #define SHT_CLEAR_STATUS1 0x3041
114  // End group macro
117 // --------------------------------------------------------------- PUBLIC TYPES
123 typedef struct
124 {
125  uint8_t clk_stretching;
126  uint8_t repeatability;
127  uint8_t mps;
128 
130 
134 typedef struct
135 {
136  // Output pins
137  digital_out_t rst;
138 
139  // Input pins
140  digital_in_t int_pin;
141 
142  // Modules
143  i2c_master_t i2c;
144 
145  // ctx variable
146  uint8_t slave_address;
147 
148  // driver variables
150 
151 } sht_t;
152 
156 typedef struct
157 {
158  // Communication gpio pins
159  pin_name_t scl;
160  pin_name_t sda;
161 
162  // Additional gpio pins
163  pin_name_t rst;
164  pin_name_t int_pin;
165 
166  // static variable
167  uint32_t i2c_speed;
168  uint8_t i2c_address;
169 
170  // driver variables config
172 
173 } sht_cfg_t;
174  // End types group
176 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
177 
183 #ifdef __cplusplus
184 extern "C"{
185 #endif
186 
195 void sht_cfg_setup ( sht_cfg_t *cfg );
196 
205 err_t sht_init ( sht_t *ctx, sht_cfg_t *cfg );
206 
217 void sht_generic_write ( sht_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
218 
229 void sht_generic_read ( sht_t *ctx, uint8_t *reg, uint8_t *data_buf, uint8_t len );
230 
238 void sht_reset ( sht_t *ctx );
239 
247 void sht_hw_reset ( sht_t *ctx );
248 
256 uint8_t sht_int_get ( sht_t *ctx );
257 
266 void sht_rst_set ( sht_t *ctx, uint8_t state );
267 
285 void sht_set_clk_strecth ( sht_t *ctx, uint8_t clk_stretching );
286 
297 void sht_set_repeats ( sht_t *ctx, uint8_t repeatability );
298 
319 void sht_set_mps ( sht_t *ctx, uint8_t measure_per_second );
320 
330 float sht_temp_ss ( sht_t *ctx );
331 
339 float sht_hum_ss ( sht_t *ctx );
340 
348 void sht_start_pm ( sht_t *ctx );
349 
364 float sht_temp_pm ( sht_t *ctx );
365 
380 float sht_hum_pm ( sht_t *ctx );
381 
390 void sht_stop_pm ( sht_t *ctx );
391 
401 void sht_software_rst ( sht_t *ctx );
402 
414 void sht_heater_control ( sht_t *ctx, uint8_t state );
415 
425 void sht_clear_status ( sht_t *ctx );
426 
437 uint8_t sht_alert_status ( sht_t *ctx );
438 
448 uint8_t sht_heater_status ( sht_t *ctx );
449 
459 uint8_t sht_hum_status ( sht_t *ctx );
460 
470 uint8_t sht_temp_status ( sht_t *ctx );
471 
482 uint8_t sht_reset_status ( sht_t *ctx );
483 
493 uint8_t sht_cmd_status ( sht_t *ctx );
494 
504 uint8_t sht_wr_chksum_status ( sht_t *ctx );
505 
506 
507 #ifdef __cplusplus
508 }
509 #endif
510 #endif // _SHT_H_
511  // End public_function group
514 
515 // ------------------------------------------------------------------------- END
sht_t::i2c
i2c_master_t i2c
Definition: sht.h:143
sht_cfg_setup
void sht_cfg_setup(sht_cfg_t *cfg)
Config Object Initialization function.
sht_t
Click ctx object definition.
Definition: sht.h:135
sht_cfg_t::scl
pin_name_t scl
Definition: sht.h:159
sht_t::vars
drv_variables_t vars
Definition: sht.h:149
sht_init
err_t sht_init(sht_t *ctx, sht_cfg_t *cfg)
Initialization function.
sht_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: sht.h:167
drv_variables_t::repeatability
uint8_t repeatability
Definition: sht.h:126
sht_cfg_t::rst
pin_name_t rst
Definition: sht.h:163
sht_start_pm
void sht_start_pm(sht_t *ctx)
Start Periodic Measurement.
sht_t::slave_address
uint8_t slave_address
Definition: sht.h:146
sht_set_mps
void sht_set_mps(sht_t *ctx, uint8_t measure_per_second)
Measurements per Second.
sht_heater_status
uint8_t sht_heater_status(sht_t *ctx)
Heater State.
sht_temp_status
uint8_t sht_temp_status(sht_t *ctx)
Temperature Alert.
sht_reset
void sht_reset(sht_t *ctx)
Resets settings.
sht_hum_status
uint8_t sht_hum_status(sht_t *ctx)
Humidity Alert.
sht_hw_reset
void sht_hw_reset(sht_t *ctx)
Resets device.
sht_cfg_t
Click configuration structure definition.
Definition: sht.h:157
sht_hum_pm
float sht_hum_pm(sht_t *ctx)
Periodic Mode Humidity.
sht_t::rst
digital_out_t rst
Definition: sht.h:137
sht_cfg_t::int_pin
pin_name_t int_pin
Definition: sht.h:164
sht_software_rst
void sht_software_rst(sht_t *ctx)
Software Reset.
sht_generic_read
void sht_generic_read(sht_t *ctx, uint8_t *reg, uint8_t *data_buf, uint8_t len)
Generic read function.
sht_temp_ss
float sht_temp_ss(sht_t *ctx)
Single Shot Temperature Measurement.
sht_temp_pm
float sht_temp_pm(sht_t *ctx)
Periodic Mode Temperature.
sht_heater_control
void sht_heater_control(sht_t *ctx, uint8_t state)
Heater State.
sht_set_clk_strecth
void sht_set_clk_strecth(sht_t *ctx, uint8_t clk_stretching)
Sets the clock stretching state.
sht_alert_status
uint8_t sht_alert_status(sht_t *ctx)
Alert Status.
sht_set_repeats
void sht_set_repeats(sht_t *ctx, uint8_t repeatability)
Sets the repeatability value.
drv_variables_t
Definition: sht.h:124
sht_cfg_t::vars_cfg
drv_variables_t vars_cfg
Definition: sht.h:171
sht_clear_status
void sht_clear_status(sht_t *ctx)
Clears Status Register.
sht_cfg_t::sda
pin_name_t sda
Definition: sht.h:160
sht_rst_set
void sht_rst_set(sht_t *ctx, uint8_t state)
Int status.
sht_int_get
uint8_t sht_int_get(sht_t *ctx)
Int status.
sht_cmd_status
uint8_t sht_cmd_status(sht_t *ctx)
Command Status.
sht_reset_status
uint8_t sht_reset_status(sht_t *ctx)
System Reset.
sht_stop_pm
void sht_stop_pm(sht_t *ctx)
Stop Periodic Measurement.
sht_wr_chksum_status
uint8_t sht_wr_chksum_status(sht_t *ctx)
Checksum Status.
sht_t::int_pin
digital_in_t int_pin
Definition: sht.h:140
sht_cfg_t::i2c_address
uint8_t i2c_address
Definition: sht.h:168
drv_variables_t::mps
uint8_t mps
Definition: sht.h:127
sht_hum_ss
float sht_hum_ss(sht_t *ctx)
Single Shot Humidity Measurement.
sht_generic_write
void sht_generic_write(sht_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
drv_variables_t::clk_stretching
uint8_t clk_stretching
Definition: sht.h:125