MIKROE 4-20mA RT Click
MIKROE_4_20mA_RT_Click.h
Go to the documentation of this file.
1
8#ifndef MIKROE_4_20mA_RT_CLICK_H_
9#define MIKROE_4_20mA_RT_CLICK_H_
10
11#include <Arduino.h>
12#include <SPI.h>
13
23const uint32_t DEFAULT_RT_CLICK_SPI_CLOCK = 1000000;
24
32const float R_CLICK_FAULT_mA = 3.8;
33
34/*******************************************************************************
35 RT_Click_Calibration
36*******************************************************************************/
37
50 float p1_mA;
51 float p2_mA;
52 uint16_t p1_bitval;
53 uint16_t p2_bitval;
54};
55
56/*******************************************************************************
57 T_Click
58*******************************************************************************/
59
63class T_Click {
64public:
71 T_Click(uint8_t CS_pin, const RT_Click_Calibration calib);
72
82 void set_SPI_clock(uint32_t clk_freq_Hz);
83
88 void begin();
89
97 uint16_t mA2bitval(float mA);
98
104 void set_mA(float mA);
105
112 uint16_t get_last_set_bitval();
113
114private:
115 uint32_t SPI_clock_ = DEFAULT_RT_CLICK_SPI_CLOCK; // SPI clock frequency [Hz]
116 uint8_t CS_pin_; // Cable select pin
117 RT_Click_Calibration calib_; // Calibration parameters [bitval] to [mA]
118 uint16_t bitval_; // Last set bit value
119};
120
121/*******************************************************************************
122 R_Click
123*******************************************************************************/
124
128class R_Click {
129public:
139 R_Click(uint8_t CS_pin, const RT_Click_Calibration calib);
140
161 R_Click(uint8_t CS_pin, const RT_Click_Calibration calib,
162 uint32_t EMA_interval, float EMA_LP_freq);
163
173 void set_SPI_clock(uint32_t clk_freq_Hz);
174
178 void begin();
179
192 float bitval2mA(float bitval);
193
199 uint16_t read_bitval();
200
207 float read_mA();
208
221 bool poll_EMA();
222
233 float get_EMA_bitval();
234
246 float get_EMA_mA();
247
254 uint32_t get_EMA_obtained_interval();
255
256private:
257 uint32_t SPI_clock_ = DEFAULT_RT_CLICK_SPI_CLOCK; // SPI clock frequency [Hz]
258 uint8_t CS_pin_; // Cable select pin
259 RT_Click_Calibration calib_; // Calibration parameters [bitval] to [mA]
260
261 // Optional exponential moving average (EMA)
262 uint32_t EMA_interval_ = 2000; // Desired oversampling interval [µs]
263 float EMA_LP_freq_ = 10e-6; // Low-pass filter cut-off frequency [MHz]
264 float EMA_bitval_ = NAN; // EMA output value [fractional bitval]
265 bool EMA_at_startup_ = true; // Are we at startup?
266 uint32_t EMA_tick_ = micros(); // Time of last oversampled reading [µs]
267 uint32_t EMA_obtained_interval_; // Last obtained oversampling interval [µs]
268};
269
270#endif
const uint32_t DEFAULT_RT_CLICK_SPI_CLOCK
Default SPI clock frequency in Hz for the R and T Click Boards.
Definition: MIKROE_4_20mA_RT_Click.h:23
const float R_CLICK_FAULT_mA
Current threshold in mA below which to indicate a fault state in the R Click reading....
Definition: MIKROE_4_20mA_RT_Click.h:32
Class to manage a MIKROE 4-20 mA R Click Board (MIKROE-1387).
Definition: MIKROE_4_20mA_RT_Click.h:128
float read_mA()
Read out the R Click once and return the current in mA.
Definition: MIKROE_4_20mA_RT_Click.cpp:199
float get_EMA_mA()
Return the exponential moving average value of the R Click readings in mA.
Definition: MIKROE_4_20mA_RT_Click.cpp:228
void set_SPI_clock(uint32_t clk_freq_Hz)
Adjust the initially set SPI clock frequency of 1 MHz to another frequency.
Definition: MIKROE_4_20mA_RT_Click.cpp:164
bool poll_EMA()
This method is crucial for the exponential moving average (EMA) to work correctly....
Definition: MIKROE_4_20mA_RT_Click.cpp:201
uint32_t get_EMA_obtained_interval()
Return the last obtained interval of the oversampled R Click readings of the exponential moving avera...
Definition: MIKROE_4_20mA_RT_Click.cpp:230
R_Click(uint8_t CS_pin, const RT_Click_Calibration calib)
Construct a new R Click object.
Definition: MIKROE_4_20mA_RT_Click.cpp:150
float bitval2mA(float bitval)
Transform the bit value into a current [mA] given the calibration parameters.
Definition: MIKROE_4_20mA_RT_Click.cpp:172
void begin()
Start SPI and set up the cable select pin.
Definition: MIKROE_4_20mA_RT_Click.cpp:166
float get_EMA_bitval()
Return the exponential moving average value of the R Click readings in bit value.
Definition: MIKROE_4_20mA_RT_Click.cpp:226
uint16_t read_bitval()
Read out the R Click once and return the bit value.
Definition: MIKROE_4_20mA_RT_Click.cpp:182
Class to manage a MIKROE 4-20 mA T Click Board (MIKROE-1296).
Definition: MIKROE_4_20mA_RT_Click.h:63
uint16_t mA2bitval(float mA)
Transform the current [mA] into a bit value given the calibration parameters.
Definition: MIKROE_4_20mA_RT_Click.cpp:112
void set_SPI_clock(uint32_t clk_freq_Hz)
Adjust the initially set SPI clock frequency of 1 MHz to another frequency.
Definition: MIKROE_4_20mA_RT_Click.cpp:103
T_Click(uint8_t CS_pin, const RT_Click_Calibration calib)
Construct a new T Click object.
Definition: MIKROE_4_20mA_RT_Click.cpp:98
void set_mA(float mA)
Set the output current of the T Click Board in mA.
Definition: MIKROE_4_20mA_RT_Click.cpp:118
uint16_t get_last_set_bitval()
Return the bit value belonging to the last set current by set_mA().
Definition: MIKROE_4_20mA_RT_Click.cpp:137
void begin()
Start SPI and set up the cable select pin. The output current will be set to 4 mA.
Definition: MIKROE_4_20mA_RT_Click.cpp:105
Structure to hold the [bitval] to [mA] calibration points of either an R Click or a T Click Board.
Definition: MIKROE_4_20mA_RT_Click.h:49
uint16_t p2_bitval
Calibration point 2 in [bitval].
Definition: MIKROE_4_20mA_RT_Click.h:53
float p2_mA
Calibration point 2 in [mA].
Definition: MIKROE_4_20mA_RT_Click.h:51
uint16_t p1_bitval
Calibration point 1 in [bitval].
Definition: MIKROE_4_20mA_RT_Click.h:52
float p1_mA
Calibration point 1 in [mA].
Definition: MIKROE_4_20mA_RT_Click.h:50