hbridge13  2.1.0.0
hbridge13.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 HBRIDGE13_H
29 #define HBRIDGE13_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_i2c_master.h"
52 #include "drv_analog_in.h"
53 #include "drv_pwm.h"
54 
75 #define HBRIDGE13_REG_INPUT_PORT 0x00
76 #define HBRIDGE13_REG_OUTPUT_PORT 0x01
77 #define HBRIDGE13_REG_POLARITY_INV 0x02
78 #define HBRIDGE13_REG_CONFIG 0x03
79  // hbridge13_reg
81 
96 #define HBRIDGE13_NONE_PIN 0x00
97 #define HBRIDGE13_AIN1_IO_PIN 0x01
98 #define HBRIDGE13_AIN2_IO_PIN 0x02
99 #define HBRIDGE13_PWM_SW1_PIN 0x04
100 #define HBRIDGE13_PWM_SW2_PIN 0x08
101 #define HBRIDGE13_BIN1_IO_PIN 0x10
102 #define HBRIDGE13_BIN2_IO_PIN 0x20
103 #define HBRIDGE13_AN_SW_PIN 0x40
104 #define HBRIDGE13_FLT_PIN 0x80
105 #define HBRIDGE13_ALL_PIN 0xFF
106 
111 #define HBRIDGE13_DIR_FORWARD 0x01
112 #define HBRIDGE13_DIR_REVERSE 0x00
113 
118 #define HBRIDGE13_AN_A_PIN_SEL 0x01
119 #define HBRIDGE13_AN_B_PIN_SEL 0x00
120 
126 #define HBRIDGE13_DEVICE_ADDRESS_A1A0_00 0x70
127 #define HBRIDGE13_DEVICE_ADDRESS_A1A0_01 0x71
128 #define HBRIDGE13_DEVICE_ADDRESS_A1A0_10 0x72
129 #define HBRIDGE13_DEVICE_ADDRESS_A1A0_11 0x73
130 
135 #define HBRIDGE13_ADC_RESOLUTION 0x0FFF
136 #define HBRIDGE13_VREF_3V3 3.3
137 #define HBRIDGE13_VREF_5V 5.0
138 
143 #define HBRIDGE13_DEF_FREQ 5000
144  // hbridge13_set
146 
161 #define HBRIDGE13_MAP_MIKROBUS( cfg, mikrobus ) \
162  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
163  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
164  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
165  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
166  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
167  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
168  // hbridge13_map // hbridge13
171 
176 typedef struct
177 {
178  // Output pins
179  digital_out_t rst;
181  // Input pins
182  digital_in_t int_pin;
184  // Modules
185  i2c_master_t i2c;
186  pwm_t pwm;
187  analog_in_t adc;
189  // I2C slave address
190  uint8_t slave_address;
192  // ADC reference voltage
193  float vref;
196  // PWM variable
197  uint32_t pwm_freq;
199 } hbridge13_t;
200 
205 typedef struct
206 {
207  pin_name_t scl;
208  pin_name_t sda;
210  pin_name_t pwm;
212  pin_name_t an;
213  pin_name_t rst;
214  pin_name_t int_pin;
216  uint32_t i2c_speed;
217  uint8_t i2c_address;
219  // Static variable
220  uint32_t dev_pwm_freq;
221  analog_in_resolution_t resolution;
222  float vref;
225 
230 typedef enum
231 {
233  HBRIDGE13_ERROR = -1
234 
236 
253 
268 
282 
297 err_t hbridge13_generic_write ( hbridge13_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
298 
313 err_t hbridge13_generic_read ( hbridge13_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
314 
328 err_t hbridge13_write_reg ( hbridge13_t *ctx, uint8_t reg, uint8_t data_out );
329 
343 err_t hbridge13_read_reg ( hbridge13_t *ctx, uint8_t reg, uint8_t *data_out );
344 
357 err_t hbridge13_set_pins ( hbridge13_t *ctx, uint8_t set_mask, uint8_t clr_mask );
358 
370 err_t hbridge13_set_duty_cycle ( hbridge13_t *ctx, float duty_cycle );
371 
383 
395 
407 
419 
432 err_t hbridge13_set_direction ( hbridge13_t *ctx, uint8_t dir_set, uint8_t speed );
433 
445 err_t hbridge13_read_raw_adc ( hbridge13_t *ctx, uint16_t *raw_adc );
446 
458 err_t hbridge13_read_voltage ( hbridge13_t *ctx, float *voltage );
459 
471 err_t hbridge13_set_vref ( hbridge13_t *ctx, float vref );
472 
485 err_t hbridge13_get_an_voltage ( hbridge13_t *ctx, float *voltage, uint8_t an_sel );
486 
498 err_t hbridge13_get_flt_state ( hbridge13_t *ctx, uint8_t *pin_state );
499 
500 #ifdef __cplusplus
501 }
502 #endif
503 #endif // HBRIDGE13_H
504  // hbridge13
506 
507 // ------------------------------------------------------------------------ END
hbridge13_cfg_t::i2c_address
uint8_t i2c_address
Definition: hbridge13.h:217
hbridge13_set_duty_cycle
err_t hbridge13_set_duty_cycle(hbridge13_t *ctx, float duty_cycle)
H-Bridge 13 sets PWM duty cycle.
hbridge13_t
H-Bridge 13 Click context object.
Definition: hbridge13.h:177
hbridge13_t::adc
analog_in_t adc
Definition: hbridge13.h:187
hbridge13_set_brake
err_t hbridge13_set_brake(hbridge13_t *ctx)
H-Bridge 13 set brake function.
hbridge13_cfg_setup
void hbridge13_cfg_setup(hbridge13_cfg_t *cfg)
H-Bridge 13 configuration object setup function.
hbridge13_init
err_t hbridge13_init(hbridge13_t *ctx, hbridge13_cfg_t *cfg)
H-Bridge 13 initialization function.
hbridge13_cfg_t::vref
float vref
Definition: hbridge13.h:222
hbridge13_cfg_t::rst
pin_name_t rst
Definition: hbridge13.h:213
hbridge13_set_pins
err_t hbridge13_set_pins(hbridge13_t *ctx, uint8_t set_mask, uint8_t clr_mask)
H-Bridge 13 set output pin state function.
hbridge13_cfg_t::an
pin_name_t an
Definition: hbridge13.h:212
hbridge13_cfg_t
H-Bridge 13 Click configuration object.
Definition: hbridge13.h:206
hbridge13_set_direction
err_t hbridge13_set_direction(hbridge13_t *ctx, uint8_t dir_set, uint8_t speed)
H-Bridge 13 set direction function.
hbridge13_cfg_t::resolution
analog_in_resolution_t resolution
Definition: hbridge13.h:221
hbridge13_generic_write
err_t hbridge13_generic_write(hbridge13_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
H-Bridge 13 I2C writing function.
hbridge13_get_an_voltage
err_t hbridge13_get_an_voltage(hbridge13_t *ctx, float *voltage, uint8_t an_sel)
H-Bridge 13 get xIPROPI voltage function.
HBRIDGE13_ERROR
@ HBRIDGE13_ERROR
Definition: hbridge13.h:233
hbridge13_t::int_pin
digital_in_t int_pin
Definition: hbridge13.h:182
hbridge13_cfg_t::int_pin
pin_name_t int_pin
Definition: hbridge13.h:214
hbridge13_write_reg
err_t hbridge13_write_reg(hbridge13_t *ctx, uint8_t reg, uint8_t data_out)
H-Bridge 13 write register function.
hbridge13_get_flt_state
err_t hbridge13_get_flt_state(hbridge13_t *ctx, uint8_t *pin_state)
H-Bridge 13 get nFAULT state function.
hbridge13_generic_read
err_t hbridge13_generic_read(hbridge13_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
H-Bridge 13 I2C reading function.
hbridge13_default_cfg
err_t hbridge13_default_cfg(hbridge13_t *ctx)
H-Bridge 13 default configuration function.
hbridge13_t::pwm_freq
uint32_t pwm_freq
Definition: hbridge13.h:197
HBRIDGE13_OK
@ HBRIDGE13_OK
Definition: hbridge13.h:232
hbridge13_read_reg
err_t hbridge13_read_reg(hbridge13_t *ctx, uint8_t reg, uint8_t *data_out)
H-Bridge 13 read register function.
hbridge13_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: hbridge13.h:216
hbridge13_t::rst
digital_out_t rst
Definition: hbridge13.h:179
hbridge13_cfg_t::pwm
pin_name_t pwm
Definition: hbridge13.h:210
hbridge13_set_coast
err_t hbridge13_set_coast(hbridge13_t *ctx)
H-Bridge 13 set coast function.
hbridge13_t::vref
float vref
Definition: hbridge13.h:193
hbridge13_cfg_t::scl
pin_name_t scl
Definition: hbridge13.h:207
hbridge13_set_vref
err_t hbridge13_set_vref(hbridge13_t *ctx, float vref)
H-Bridge 13 set vref function.
hbridge13_cfg_t::sda
pin_name_t sda
Definition: hbridge13.h:208
hbridge13_t::slave_address
uint8_t slave_address
Definition: hbridge13.h:190
hbridge13_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: hbridge13.h:220
hbridge13_read_voltage
err_t hbridge13_read_voltage(hbridge13_t *ctx, float *voltage)
H-Bridge 13 read voltage level function.
hbridge13_t::i2c
i2c_master_t i2c
Definition: hbridge13.h:185
hbridge13_return_value_t
hbridge13_return_value_t
H-Bridge 13 Click return value data.
Definition: hbridge13.h:231
hbridge13_read_raw_adc
err_t hbridge13_read_raw_adc(hbridge13_t *ctx, uint16_t *raw_adc)
H-Bridge 13 read raw ADC value function.
hbridge13_pwm_start
err_t hbridge13_pwm_start(hbridge13_t *ctx)
H-Bridge 13 start PWM module.
hbridge13_pwm_stop
err_t hbridge13_pwm_stop(hbridge13_t *ctx)
H-Bridge 13 stop PWM module.
hbridge13_t::pwm
pwm_t pwm
Definition: hbridge13.h:186