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
32extern "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
82 // ph2_cmd
83
98#define PH2_PH_CALCULATION_COEF 0.18
99
100#define PH2_CONFIG_ADDRESS 0x10
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
149 // 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
173 // ph2_map
174 // ph2
175
186
191typedef 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;
206 float vref;
209} ph2_t;
210
215typedef 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
238typedef enum
239{
241 PH2_ERROR = -1
242
244
261
277
291err_t ph2_init ( ph2_t *ctx, ph2_cfg_t *cfg );
292
304err_t ph2_read_raw_adc ( ph2_t *ctx, uint16_t *raw_adc );
305
317err_t ph2_read_voltage ( ph2_t *ctx, float *voltage );
318
330err_t ph2_set_vref ( ph2_t *ctx, float vref );
331
344err_t ph2_calibrate ( ph2_t *ctx, float pH_value );
345
358err_t ph2_calculate_ph ( ph2_t *ctx, float *pH_value );
359
373
382void ph2_enable_st1 ( ph2_t *ctx );
383
392void ph2_disable_st1 ( ph2_t *ctx );
393
402void ph2_enable_st2 ( ph2_t *ctx );
403
412void ph2_disable_st2 ( ph2_t *ctx );
413
427err_t ph2_ds18b20_init ( ph2_t *ctx, ph2_cfg_t *cfg );
428
441
454
466err_t ph2_ds18b20_read_scratchpad ( ph2_t *ctx, uint8_t *scratchpad );
467
481err_t ph2_ds18b20_write_scratchpad ( ph2_t *ctx, uint8_t temp_high, uint8_t temp_low, uint8_t config );
482
495
507
519err_t ph2_ds18b20_read_temperature ( ph2_t *ctx, float *temperature );
520
521#ifdef __cplusplus
522}
523#endif
524#endif // PH2_H
525
526 // ph2
527
528// ------------------------------------------------------------------------ END
void ph2_enable_st1(ph2_t *ctx)
Ph 2 enable LED STAT1 function.
void ph2_calibrate_offset(ph2_t *ctx)
Ph 2 calibrate offset function.
void ph2_disable_st2(ph2_t *ctx)
Ph 2 disable LED STAT2 function.
void ph2_drv_interface_selection(ph2_cfg_t *cfg, ph2_drv_t drv_sel)
pH 2 driver interface setup function.
err_t ph2_calculate_ph(ph2_t *ctx, float *pH_value)
Ph 2 calculate pH value function.
err_t ph2_ds18b20_start_measurement(ph2_t *ctx)
pH 2 DS18B20 start measurement function.
void ph2_enable_st2(ph2_t *ctx)
Ph 2 enable LED STAT2 function.
err_t ph2_ds18b20_select_device(ph2_t *ctx)
pH 2 DS18B20 select device function.
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.
err_t ph2_ds18b20_init(ph2_t *ctx, ph2_cfg_t *cfg)
pH 2 DS18B20 initialization function.
err_t ph2_read_raw_adc(ph2_t *ctx, uint16_t *raw_adc)
pH 2 read raw ADC value function.
err_t ph2_read_voltage(ph2_t *ctx, float *voltage)
pH 2 read voltage level function.
err_t ph2_ds18b20_default_cfg(ph2_t *ctx)
pH 2 DS18B20 default configuration function.
void ph2_disable_st1(ph2_t *ctx)
Ph 2 disable LED STAT1 function.
err_t ph2_ds18b20_check_communication(ph2_t *ctx)
pH 2 DS18B20 check communication function.
err_t ph2_init(ph2_t *ctx, ph2_cfg_t *cfg)
pH 2 initialization function.
err_t ph2_ds18b20_read_temperature(ph2_t *ctx, float *temperature)
pH 2 DS18B20 read temperature function.
err_t ph2_set_vref(ph2_t *ctx, float vref)
pH 2 set vref function.
err_t ph2_ds18b20_read_scratchpad(ph2_t *ctx, uint8_t *scratchpad)
pH 2 DS18B20 read scratchpad function.
err_t ph2_calibrate(ph2_t *ctx, float pH_value)
Ph 2 calibrate function.
void ph2_cfg_setup(ph2_cfg_t *cfg)
pH 2 configuration object setup function.
ph2_drv_t
pH 2 Click driver selector.
Definition ph2.h:181
@ PH2_DRV_SEL_I2C
Definition ph2.h:183
@ PH2_DRV_SEL_ADC
Definition ph2.h:182
ph2_return_value_t
pH 2 Click return value data.
Definition ph2.h:239
@ PH2_OK
Definition ph2.h:240
@ PH2_ERROR
Definition ph2.h:241
pH 2 Click configuration object.
Definition ph2.h:216
analog_in_resolution_t resolution
Definition ph2.h:224
float vref
Definition ph2.h:225
uint32_t i2c_speed
Definition ph2.h:227
pin_name_t st1
Definition ph2.h:221
ph2_drv_t drv_sel
Definition ph2.h:230
pin_name_t st2
Definition ph2.h:222
pin_name_t scl
Definition ph2.h:218
pin_name_t sda
Definition ph2.h:219
pin_name_t dq
Definition ph2.h:220
pin_name_t an
Definition ph2.h:217
uint8_t i2c_address
Definition ph2.h:228
pH 2 Click context object.
Definition ph2.h:192
one_wire_rom_address_t rom_addr
Definition ph2.h:203
digital_out_t st2
Definition ph2.h:194
float vref
Definition ph2.h:206
uint8_t config
Definition ph2.h:204
ph2_drv_t drv_sel
Definition ph2.h:207
float pH_val
Definition ph2.h:200
digital_out_t st1
Definition ph2.h:193
i2c_master_t i2c
Definition ph2.h:197
float pH_vol
Definition ph2.h:201
uint8_t slave_address
Definition ph2.h:205
analog_in_t adc
Definition ph2.h:196
one_wire_t ow
Definition ph2.h:198