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 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_analog_in.h"
52 #include "drv_i2c_master.h"
53 #include "drv_one_wire.h"
54 
75 #define PH2_CMD_CONVERT_TEMPERATURE 0x44
76 #define PH2_CMD_WRITE_SCRATCHPAD 0x4E
77 #define PH2_CMD_READ_SCRATCHPAD 0xBE
78 #define PH2_CMD_COPY_SCRATCHPAD 0x48
79 #define PH2_CMD_RECALL_SCRATCHPAD 0xB8
80 #define PH2_CMD_READ_POWER_SUPPLY 0xB4
81  // ph2_cmd
83 
98 #define PH2_PH_CALCULATION_COEF 0.18
99 
100 #define PH2_CONFIG_ADDRESS 0x10
101 
105 #define PH2_ADC_RESOLUTION 0x0FFF
106 #define PH2_VREF_3V3 3.3
107 #define PH2_VREF_5V 5.0
108 
114 #define PH2_SET_DEV_ADDR 0x4D
115 
120 #define PH2_FAMILY_CODE 0x28
121 
127 #define PH2_CONFIG_RESOLUTION_9BIT 0x00
128 #define PH2_CONFIG_RESOLUTION_10BIT 0x20
129 #define PH2_CONFIG_RESOLUTION_11BIT 0x40
130 #define PH2_CONFIG_RESOLUTION_12BIT 0x60
131 #define PH2_CONFIG_RESOLUTION_BIT_MASK 0x60
132 #define PH2_CONFIG_RESERVED_BITS 0x10
133 
138 #define PH2_DATA_RESOLUTION 0.0625
139 
144 #define PH2_DEFAULT_ADDRESS PH2_CONFIG_ADDRESS
145 #define PH2_DEFAULT_RESOLUTION PH2_CONFIG_RESOLUTION_12BIT
146 #define PH2_DEFAULT_TEMP_HIGH_ALARM 0xFF
147 #define PH2_DEFAULT_TEMP_LOW_ALARM 0x00
148  // ph2_set
150 
165 #define PH2_MAP_MIKROBUS( cfg, mikrobus ) \
166  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
167  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
168  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
169  cfg.dq = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
170  cfg.st1 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
171  cfg.st2 = MIKROBUS( mikrobus, MIKROBUS_INT )
172  // ph2_map // ph2
175 
180 typedef enum
181 {
186 
191 typedef struct
192 {
193  digital_out_t st1;
194  digital_out_t st2;
196  analog_in_t adc;
197  i2c_master_t i2c;
198  one_wire_t ow;
200  float pH_val;
201  float pH_vol;
202 
203  one_wire_rom_address_t rom_addr;
204  uint8_t config;
205  uint8_t slave_address;
206  float vref;
209 } ph2_t;
210 
215 typedef struct
216 {
217  pin_name_t an;
218  pin_name_t scl;
219  pin_name_t sda;
220  pin_name_t dq;
221  pin_name_t st1;
222  pin_name_t st2;
224  analog_in_resolution_t resolution;
225  float vref;
227  uint32_t i2c_speed;
228  uint8_t i2c_address;
232 } ph2_cfg_t;
233 
238 typedef enum
239 {
240  PH2_OK = 0,
241  PH2_ERROR = -1
242 
244 
260 void ph2_cfg_setup ( ph2_cfg_t *cfg );
261 
277 
291 err_t ph2_init ( ph2_t *ctx, ph2_cfg_t *cfg );
292 
304 err_t ph2_read_raw_adc ( ph2_t *ctx, uint16_t *raw_adc );
305 
317 err_t ph2_read_voltage ( ph2_t *ctx, float *voltage );
318 
330 err_t ph2_set_vref ( ph2_t *ctx, float vref );
331 
344 err_t ph2_calibrate ( ph2_t *ctx, float pH_value );
345 
358 err_t ph2_calculate_ph ( ph2_t *ctx, float *pH_value );
359 
373 
382 void ph2_enable_st1 ( ph2_t *ctx );
383 
392 void ph2_disable_st1 ( ph2_t *ctx );
393 
402 void ph2_enable_st2 ( ph2_t *ctx );
403 
412 void ph2_disable_st2 ( ph2_t *ctx );
413 
427 err_t ph2_ds18b20_init ( ph2_t *ctx, ph2_cfg_t *cfg );
428 
441 
454 
466 err_t ph2_ds18b20_read_scratchpad ( ph2_t *ctx, uint8_t *scratchpad );
467 
481 err_t ph2_ds18b20_write_scratchpad ( ph2_t *ctx, uint8_t temp_high, uint8_t temp_low, uint8_t config );
482 
495 
507 
519 err_t ph2_ds18b20_read_temperature ( ph2_t *ctx, float *temperature );
520 
521 #ifdef __cplusplus
522 }
523 #endif
524 #endif // PH2_H
525  // ph2
527 
528 // ------------------------------------------------------------------------ END
ph2_t
pH 2 Click context object.
Definition: ph2.h:192
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:193
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:198
ph2_cfg_t::scl
pin_name_t scl
Definition: ph2.h:218
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:227
ph2_return_value_t
ph2_return_value_t
pH 2 Click return value data.
Definition: ph2.h:239
PH2_ERROR
@ PH2_ERROR
Definition: ph2.h:241
ph2_cfg_t::drv_sel
ph2_drv_t drv_sel
Definition: ph2.h:230
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:196
ph2_t::drv_sel
ph2_drv_t drv_sel
Definition: ph2.h:207
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:222
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:221
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:206
ph2_t::pH_vol
float pH_vol
Definition: ph2.h:201
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:181
ph2_cfg_t
pH 2 Click configuration object.
Definition: ph2.h:216
ph2_t::rom_addr
one_wire_rom_address_t rom_addr
Definition: ph2.h:203
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:219
ph2_t::i2c
i2c_master_t i2c
Definition: ph2.h:197
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:200
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:225
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:204
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:217
PH2_DRV_SEL_I2C
@ PH2_DRV_SEL_I2C
Definition: ph2.h:183
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:194
PH2_DRV_SEL_ADC
@ PH2_DRV_SEL_ADC
Definition: ph2.h:182
ph2_cfg_t::resolution
analog_in_resolution_t resolution
Definition: ph2.h:224
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:220
ph2_cfg_t::i2c_address
uint8_t i2c_address
Definition: ph2.h:228
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:205
PH2_OK
@ PH2_OK
Definition: ph2.h:240