Mcp320x
|
#include <Mcp320x.h>
Data Structures | |
union | SpiData |
Public Types | |
using | Channel = ChannelType |
Public Member Functions | |
MCP320x (uint16_t vref, uint8_t csPin, SPIClass *spi) | |
MCP320x (uint16_t vref, uint8_t csPin) | |
void | calibrate (Channel ch) |
uint16_t | read (Channel ch) const |
template<typename T , size_t N> | |
void | read (Channel ch, T(&data)[N]) const |
template<typename T , size_t N> | |
void | read (Channel ch, T(&data)[N], uint32_t splFreq) |
template<typename T , size_t N, typename Predicate > | |
void | read_if (Channel ch, T(&data)[N], Predicate p) const |
template<typename T , size_t N, typename Predicate > | |
void | read_if (Channel ch, T(&data)[N], uint32_t splFreq, Predicate p) |
template<typename T > | |
void | readn (Channel ch, T *data, uint16_t num) const |
template<typename T > | |
void | readn (Channel ch, T *data, uint16_t num, uint32_t splFreq) |
template<typename T , typename Predicate > | |
void | readn_if (Channel ch, T *data, uint16_t num, Predicate p) const |
template<typename T , typename Predicate > | |
void | readn_if (Channel ch, T *data, uint16_t num, uint32_t splFreq, Predicate p) |
uint32_t | testSplSpeed (Channel ch) const |
uint32_t | testSplSpeed (Channel ch, uint16_t num) const |
uint32_t | testSplSpeed (Channel ch, uint16_t num, uint32_t splFreq) |
uint16_t | toAnalog (uint16_t raw) const |
uint16_t | toDigital (uint16_t val) const |
uint16_t | getVref () const |
uint16_t | getAnalogRes () const |
Static Public Attributes | |
static const uint8_t | kResBits = 12 |
static const uint16_t | kRes = (1 << kResBits) |
Private Types | |
template<typename > | |
using | Command = SpiData |
Private Member Functions | |
uint16_t | getSplDelay (Channel ch, uint32_t splFreq) |
uint16_t | execute (Command< Channel > cmd) const |
template<typename T > | |
void | execute (Command< Channel > cmd, T *data, uint16_t num) const |
template<typename T > | |
void | execute (Command< Channel > cmd, T *data, uint16_t num, uint16_t delay) const |
uint16_t | transfer () const |
uint16_t | transfer (SpiData cmd) const |
Static Private Member Functions | |
static Command< Channel > | createCmd (Channel ch) |
Private Attributes | |
uint16_t | mVref |
uint8_t | mCsPin |
uint32_t | mSplSpeed |
SPIClass * | mSpi |
Initiates a MCP320x object. The chip select pin must be already configured as output.
[in] | vref | ADC reference voltage in mV. |
[in] | csPin | pin number to use for chip select. |
[in] | spi | reference to the SPI interface to use. |
Definition at line 17 of file Mcp320x.cpp.
Initiates a MCP320x object. The chip select pin must be already configured as output. The default SPI interface will be used for communication.
[in] | vref | the ADC reference voltage in mV. |
[in] | csPin | the pin number to use for chip select. |
Definition at line 24 of file Mcp320x.cpp.
Calibrates read timing using the supplied channel. A calibration should be performed after evey SPI frequency changes or other events that could have an impact on the sampling speed. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to use for calibration. |
Definition at line 28 of file Mcp320x.cpp.
References MCP320x< ChannelType >::mSplSpeed, and MCP320x< ChannelType >::testSplSpeed().
Referenced by MCP320x< ChannelType >::getSplDelay().
|
staticprivate |
Creates a command from the supplied channel.
[in] | ch | the channel to create the command for. |
Definition at line 120 of file Mcp320x.cpp.
Referenced by MCP320x< ChannelType >::read(), and MCP320x< ChannelType >::testSplSpeed().
Executes the supplied command.
[in] | cmd | the command to execute. |
Definition at line 163 of file Mcp320x.cpp.
References MCP320x< ChannelType >::transfer().
Referenced by MCP320x< ChannelType >::read(), and MCP320x< ChannelType >::testSplSpeed().
uint16_t MCP320x< T >::getAnalogRes | ( | ) | const |
Returns the analog resolution in µV based on the defined reference voltage.
Definition at line 100 of file Mcp320x.cpp.
References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.
Returns the required delay for the requested sample rate.
[in] | ch | the channel to create the command for. |
Definition at line 106 of file Mcp320x.cpp.
References MCP320x< ChannelType >::calibrate(), div_round, and MCP320x< ChannelType >::mSplSpeed.
Referenced by MCP320x< ChannelType >::testSplSpeed().
uint16_t MCP320x< T >::getVref | ( | ) | const |
Returns the reference voltage.
Definition at line 94 of file Mcp320x.cpp.
References MCP320x< ChannelType >::mVref.
Reads the supplied channel. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
Definition at line 34 of file Mcp320x.cpp.
References MCP320x< ChannelType >::createCmd(), and MCP320x< ChannelType >::execute().
|
inline |
Reads the supplied channel limited to the specified frequency and stores the data in the supplied data array. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | splFreq | sample frequency limit in hz. |
|
inline |
Reads the supplied channel and stores the data in the supplied data array after the predicate is true. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | p | predicate funtion to control sampling start. |
|
inline |
Reads the supplied channel limited to the specified frequency and stores the data in the supplied data array after the predicate is true. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | splFreq | sample frequency limit in hz. |
[in] | p | predicate funtion to control sampling start. |
|
inline |
Reads the supplied channel and stores N values in the supplied data array. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | num | number of reads. The data array needs to be at least that size. |
|
inline |
Reads the supplied channel limited to the specified frequency and stores N values in the supplied data array. The sample rate limit is software controlled, and has a low precision. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | num | number of reads. The data array needs to be at least that size. |
[in] | splFreq | sample frequency limit in hz. |
|
inline |
Reads the supplied channel and stores N values in the supplied data array after the predicate is true. As long as the predicate is false, the function keeps sampling without storing any data. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | num | number of reads. The data array needs to be at least that size. |
[in] | p | predicate funtion to control sampling start. |
|
inline |
Reads the supplied channel limited to the specified frequency and stores N values in the supplied data array after the predicate is true. As long as the predicate is false, the function keeps sampling without storing any data. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | defines the channel to read from. |
[out] | data | array to store the values. |
[in] | num | number of reads. The data array needs to be at least that size. |
[in] | splFreq | sample frequency limit in hz. |
[in] | p | predicate funtion to control sampling start. |
Performs a sampling speed test over 64 reads. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | the channel to use for the speed test. |
Definition at line 40 of file Mcp320x.cpp.
Referenced by MCP320x< ChannelType >::calibrate().
Performs a sampling speed test. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | the channel to use for the speed test. |
[in] | num | the number of reads to perform. |
Definition at line 46 of file Mcp320x.cpp.
References MCP320x< ChannelType >::createCmd(), div_round, and MCP320x< ChannelType >::execute().
uint32_t MCP320x< T >::testSplSpeed | ( | Channel | ch, |
uint16_t | num, | ||
uint32_t | splFreq | ||
) |
Performs a sampling speed test limited to the specified frequency. The SPI interface must be initialized and put in a usable state before calling this function.
[in] | ch | the channel to use for the speed test. |
[in] | num | the number of reads to perform. |
[in] | splFreq | sample frequency limit in hz. |
Definition at line 61 of file Mcp320x.cpp.
References MCP320x< ChannelType >::createCmd(), div_round, MCP320x< ChannelType >::execute(), and MCP320x< ChannelType >::getSplDelay().
uint16_t MCP320x< T >::toAnalog | ( | uint16_t | raw | ) | const |
Converts the supplied raw value to an analog value in mV based on the defined reference voltage.
[in] | raw | the sampled ADC value. |
Definition at line 82 of file Mcp320x.cpp.
References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.
uint16_t MCP320x< T >::toDigital | ( | uint16_t | val | ) | const |
Converts the supplied analog value to the digital representation based on the defined reference voltage.
[in] | val | the analog value in mV. |
Definition at line 88 of file Mcp320x.cpp.
References MCP320x< ChannelType >::kRes, and MCP320x< ChannelType >::mVref.
|
private |
Transfers without SPI command data.
Definition at line 187 of file Mcp320x.cpp.
References MCP320x< ChannelType >::SpiData::hiByte, MCP320x< ChannelType >::SpiData::loByte, MCP320x< ChannelType >::mCsPin, MCP320x< ChannelType >::mSpi, and MCP320x< ChannelType >::SpiData::value.
Referenced by MCP320x< ChannelType >::execute().
Transfers the supplied SPI command data.
[in] | cmd | the SPI command data to transfer. |
Definition at line 208 of file Mcp320x.cpp.
References MCP320x< ChannelType >::SpiData::hiByte, MCP320x< ChannelType >::SpiData::loByte, MCP320x< ChannelType >::mCsPin, MCP320x< ChannelType >::mSpi, and MCP320x< ChannelType >::SpiData::value.
|
static |
ADC resolution.
Definition at line 122 of file Mcp320x.h.
Referenced by MCP320x< ChannelType >::getAnalogRes(), MCP320x< ChannelType >::toAnalog(), and MCP320x< ChannelType >::toDigital().
|
static |
|
private |
Definition at line 448 of file Mcp320x.h.
Referenced by MCP320x< ChannelType >::transfer().
|
private |
Definition at line 450 of file Mcp320x.h.
Referenced by MCP320x< ChannelType >::transfer().
|
private |
Definition at line 449 of file Mcp320x.h.
Referenced by MCP320x< ChannelType >::calibrate(), and MCP320x< ChannelType >::getSplDelay().
|
private |
Definition at line 447 of file Mcp320x.h.
Referenced by MCP320x< ChannelType >::getAnalogRes(), MCP320x< ChannelType >::getVref(), MCP320x< ChannelType >::toAnalog(), and MCP320x< ChannelType >::toDigital().