ph2  2.1.0.0
ph2.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef PH2_H
29 #define PH2_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_analog_in.h"
48 #include "drv_i2c_master.h"
49 #include "drv_one_wire.h"
50 
71 #define PH2_CMD_CONVERT_TEMPERATURE 0x44
72 #define PH2_CMD_WRITE_SCRATCHPAD 0x4E
73 #define PH2_CMD_READ_SCRATCHPAD 0xBE
74 #define PH2_CMD_COPY_SCRATCHPAD 0x48
75 #define PH2_CMD_RECALL_SCRATCHPAD 0xB8
76 #define PH2_CMD_READ_POWER_SUPPLY 0xB4
77  // ph2_cmd
79 
94 #define PH2_PH_CALCULATION_COEF 0.18
95 
96 #define PH2_CONFIG_ADDRESS 0x10
97 
101 #define PH2_ADC_RESOLUTION 0x0FFF
102 #define PH2_VREF_3V3 3.3
103 #define PH2_VREF_5V 5.0
104 
110 #define PH2_SET_DEV_ADDR 0x4D
111 
116 #define PH2_FAMILY_CODE 0x28
117 
123 #define PH2_CONFIG_RESOLUTION_9BIT 0x00
124 #define PH2_CONFIG_RESOLUTION_10BIT 0x20
125 #define PH2_CONFIG_RESOLUTION_11BIT 0x40
126 #define PH2_CONFIG_RESOLUTION_12BIT 0x60
127 #define PH2_CONFIG_RESOLUTION_BIT_MASK 0x60
128 #define PH2_CONFIG_RESERVED_BITS 0x10
129 
134 #define PH2_DATA_RESOLUTION 0.0625
135 
140 #define PH2_DEFAULT_ADDRESS PH2_CONFIG_ADDRESS
141 #define PH2_DEFAULT_RESOLUTION PH2_CONFIG_RESOLUTION_12BIT
142 #define PH2_DEFAULT_TEMP_HIGH_ALARM 0xFF
143 #define PH2_DEFAULT_TEMP_LOW_ALARM 0x00
144  // ph2_set
146 
161 #define PH2_MAP_MIKROBUS( cfg, mikrobus ) \
162  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
163  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
164  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
165  cfg.dq = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
166  cfg.st1 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
167  cfg.st2 = MIKROBUS( mikrobus, MIKROBUS_INT )
168  // ph2_map // ph2
171 
176 typedef enum
177 {
182 
187 typedef struct
188 {
189  digital_out_t st1;
190  digital_out_t st2;
192  analog_in_t adc;
193  i2c_master_t i2c;
194  one_wire_t ow;
196  float pH_val;
197  float pH_vol;
198 
199  one_wire_rom_address_t rom_addr;
200  uint8_t config;
201  uint8_t slave_address;
202  float vref;
205 } ph2_t;
206 
211 typedef struct
212 {
213  pin_name_t an;
214  pin_name_t scl;
215  pin_name_t sda;
216  pin_name_t dq;
217  pin_name_t st1;
218  pin_name_t st2;
220  analog_in_resolution_t resolution;
221  float vref;
223  uint32_t i2c_speed;
224  uint8_t i2c_address;
228 } ph2_cfg_t;
229 
234 typedef enum
235 {
236  PH2_OK = 0,
237  PH2_ERROR = -1
238 
240 
256 void ph2_cfg_setup ( ph2_cfg_t *cfg );
257 
273 
287 err_t ph2_init ( ph2_t *ctx, ph2_cfg_t *cfg );
288 
300 err_t ph2_read_raw_adc ( ph2_t *ctx, uint16_t *raw_adc );
301 
313 err_t ph2_read_voltage ( ph2_t *ctx, float *voltage );
314 
326 err_t ph2_set_vref ( ph2_t *ctx, float vref );
327 
340 err_t ph2_calibrate ( ph2_t *ctx, float pH_value );
341 
354 err_t ph2_calculate_ph ( ph2_t *ctx, float *pH_value );
355 
369 
378 void ph2_enable_st1 ( ph2_t *ctx );
379 
388 void ph2_disable_st1 ( ph2_t *ctx );
389 
398 void ph2_enable_st2 ( ph2_t *ctx );
399 
408 void ph2_disable_st2 ( ph2_t *ctx );
409 
423 err_t ph2_ds18b20_init ( ph2_t *ctx, ph2_cfg_t *cfg );
424 
437 
450 
462 err_t ph2_ds18b20_read_scratchpad ( ph2_t *ctx, uint8_t *scratchpad );
463 
477 err_t ph2_ds18b20_write_scratchpad ( ph2_t *ctx, uint8_t temp_high, uint8_t temp_low, uint8_t config );
478 
491 
503 
515 err_t ph2_ds18b20_read_temperature ( ph2_t *ctx, float *temperature );
516 
517 #ifdef __cplusplus
518 }
519 #endif
520 #endif // PH2_H
521  // ph2
523 
524 // ------------------------------------------------------------------------ END
ph2_t
pH 2 Click context object.
Definition: ph2.h:188
ph2_cfg_setup
void ph2_cfg_setup(ph2_cfg_t *cfg)
pH 2 configuration object setup function.
ph2_drv_interface_selection
void ph2_drv_interface_selection(ph2_cfg_t *cfg, ph2_drv_t drv_sel)
pH 2 driver interface setup function.
ph2_ds18b20_check_communication
err_t ph2_ds18b20_check_communication(ph2_t *ctx)
pH 2 DS18B20 check communication function.
ph2_t::st1
digital_out_t st1
Definition: ph2.h:189
ph2_calibrate
err_t ph2_calibrate(ph2_t *ctx, float pH_value)
Ph 2 calibrate function.
ph2_disable_st2
void ph2_disable_st2(ph2_t *ctx)
Ph 2 disable LED STAT2 function.
ph2_t::ow
one_wire_t ow
Definition: ph2.h:194
ph2_cfg_t::scl
pin_name_t scl
Definition: ph2.h:214
ph2_enable_st2
void ph2_enable_st2(ph2_t *ctx)
Ph 2 enable LED STAT2 function.
ph2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ph2.h:223
ph2_return_value_t
ph2_return_value_t
pH 2 Click return value data.
Definition: ph2.h:235
PH2_ERROR
@ PH2_ERROR
Definition: ph2.h:237
ph2_cfg_t::drv_sel
ph2_drv_t drv_sel
Definition: ph2.h:226
ph2_ds18b20_read_scratchpad
err_t ph2_ds18b20_read_scratchpad(ph2_t *ctx, uint8_t *scratchpad)
pH 2 DS18B20 read scratchpad function.
ph2_t::adc
analog_in_t adc
Definition: ph2.h:192
ph2_t::drv_sel
ph2_drv_t drv_sel
Definition: ph2.h:203
ph2_ds18b20_start_measurement
err_t ph2_ds18b20_start_measurement(ph2_t *ctx)
pH 2 DS18B20 start measurement function.
ph2_cfg_t::st2
pin_name_t st2
Definition: ph2.h:218
ph2_ds18b20_read_temperature
err_t ph2_ds18b20_read_temperature(ph2_t *ctx, float *temperature)
pH 2 DS18B20 read temperature function.
ph2_cfg_t::st1
pin_name_t st1
Definition: ph2.h:217
ph2_calculate_ph
err_t ph2_calculate_ph(ph2_t *ctx, float *pH_value)
Ph 2 calculate pH value function.
ph2_t::vref
float vref
Definition: ph2.h:202
ph2_t::pH_vol
float pH_vol
Definition: ph2.h:197
ph2_read_raw_adc
err_t ph2_read_raw_adc(ph2_t *ctx, uint16_t *raw_adc)
pH 2 read raw ADC value function.
ph2_enable_st1
void ph2_enable_st1(ph2_t *ctx)
Ph 2 enable LED STAT1 function.
ph2_drv_t
ph2_drv_t
pH 2 Click driver selector.
Definition: ph2.h:177
ph2_cfg_t
pH 2 Click configuration object.
Definition: ph2.h:212
ph2_t::rom_addr
one_wire_rom_address_t rom_addr
Definition: ph2.h:199
ph2_read_voltage
err_t ph2_read_voltage(ph2_t *ctx, float *voltage)
pH 2 read voltage level function.
ph2_cfg_t::sda
pin_name_t sda
Definition: ph2.h:215
ph2_t::i2c
i2c_master_t i2c
Definition: ph2.h:193
ph2_ds18b20_select_device
err_t ph2_ds18b20_select_device(ph2_t *ctx)
pH 2 DS18B20 select device function.
ph2_t::pH_val
float pH_val
Definition: ph2.h:196
ph2_set_vref
err_t ph2_set_vref(ph2_t *ctx, float vref)
pH 2 set vref function.
ph2_cfg_t::vref
float vref
Definition: ph2.h:221
ph2_init
err_t ph2_init(ph2_t *ctx, ph2_cfg_t *cfg)
pH 2 initialization function.
ph2_ds18b20_init
err_t ph2_ds18b20_init(ph2_t *ctx, ph2_cfg_t *cfg)
pH 2 DS18B20 initialization function.
ph2_t::config
uint8_t config
Definition: ph2.h:200
ph2_calibrate_offset
void ph2_calibrate_offset(ph2_t *ctx)
Ph 2 calibrate offset function.
ph2_cfg_t::an
pin_name_t an
Definition: ph2.h:213
PH2_DRV_SEL_I2C
@ PH2_DRV_SEL_I2C
Definition: ph2.h:179
ph2_ds18b20_write_scratchpad
err_t ph2_ds18b20_write_scratchpad(ph2_t *ctx, uint8_t temp_high, uint8_t temp_low, uint8_t config)
pH 2 DS18B20 write scratchpad function.
ph2_t::st2
digital_out_t st2
Definition: ph2.h:190
PH2_DRV_SEL_ADC
@ PH2_DRV_SEL_ADC
Definition: ph2.h:178
ph2_cfg_t::resolution
analog_in_resolution_t resolution
Definition: ph2.h:220
ph2_disable_st1
void ph2_disable_st1(ph2_t *ctx)
Ph 2 disable LED STAT1 function.
ph2_cfg_t::dq
pin_name_t dq
Definition: ph2.h:216
ph2_cfg_t::i2c_address
uint8_t i2c_address
Definition: ph2.h:224
ph2_ds18b20_default_cfg
err_t ph2_ds18b20_default_cfg(ph2_t *ctx)
pH 2 DS18B20 default configuration function.
ph2_t::slave_address
uint8_t slave_address
Definition: ph2.h:201
PH2_OK
@ PH2_OK
Definition: ph2.h:236