Skyward boardcore
|
Driver for ADS1118 adc. More...
#include <ADS1118.h>
Classes | |
union | ADS1118Config |
Structure of configuration word. More... | |
struct | ADS1118InputConfig |
Driver's input config. More... | |
Public Types | |
enum | ADS1118Mux { MUX_AIN0_AIN1 = 0x0 , MUX_AIN0_AIN3 = 0x1 , MUX_AIN1_AIN3 = 0x2 , MUX_AIN2_AIN3 = 0x3 , MUX_AIN0_GND = 0x4 , MUX_AIN1_GND = 0x5 , MUX_AIN2_GND = 0x6 , MUX_AIN3_GND = 0x7 } |
enum | ADS1118Pga { FSR_6_144 = 0x0 , FSR_4_096 = 0x1 , FSR_2_048 = 0x2 , FSR_1_024 = 0x3 , FSR_0_512 = 0x4 , FSR_0_256 = 0x5 } |
enum | ADS1118Mode { CONTINUOUS_CONV_MODE = 0x0 , SINGLE_SHOT_MODE = 0x1 } |
enum | ADS1118DataRate { DR_8 = 0x0 , DR_16 = 0x1 , DR_32 = 0x2 , DR_64 = 0x3 , DR_128 = 0x4 , DR_250 = 0x5 , DR_475 = 0x6 , DR_860 = 0x7 } |
enum | ADS1118TempMode { ADC_MODE = 0x0 , TEMP_SENSOR_MODE = 0x1 } |
enum | ADS1118PullUp { PULL_UP_DIS = 0x0 , PULL_UP_EN = 0x1 } |
![]() | |
using | DataType |
Public Member Functions | |
ADS1118 (SPIBusInterface &bus, miosix::GpioPin cs, ADS1118Config config_, SPIBusConfig spiConfig=getDefaultSPIConfig()) | |
Construct a new ADS1118 object specifying spi bus, spi config and cs pin as well as device configuration. | |
ADS1118 (SPISlave spiSlave_, ADS1118Config config_=ADS1118_DEFAULT_CONFIG, bool busyWait_=false, int16_t tempDivider_=100) | |
Construct a new ADS1118 object. | |
bool | init () override |
Initialize the configuration. | |
void | enableInput (ADS1118Mux mux) |
Enables the sapling of a specific mux configuration with the main configuration specified in the constructor. | |
void | enableInput (ADS1118Mux mux, ADS1118DataRate rate, ADS1118Pga pga) |
Enables the sampling of a specific mux configuration. | |
void | disableInput (ADS1118Mux mux) |
void | disableAllInputs () |
void | enableTemperature () |
void | disableTemperature () |
void | enablePullUpResistor () |
void | disablePullUpResistor () |
void | enableConfigCheck () |
Enables the configuration check after it's writing to the device. | |
void | disableConfigCheck () |
Disables the configuration check after it's writing to the device. | |
ADS1118Data | readInputAndWait (ADS1118Mux mux) |
Reads on the fly the specified input. | |
TemperatureData | readTemperatureAndWait () |
Reads on the fly the temperature. | |
ADS1118Data | getVoltage (ADS1118Mux mux) |
Returns the last read voltage value for the specified channel. | |
TemperatureData | getTemperature () |
Returns the last temperature value. | |
int | getConversionTime (int8_t channel) |
Returns the conversion time in us for the specified channel. | |
bool | selfTest () override |
Writes the temperature configuration and check if it is read back correctly. | |
![]() | |
Sensor () | |
Sensor (Sensor &&other) | |
virtual | ~Sensor () |
void | sample () override |
Sample the sensor. | |
virtual ADS1118Data | getLastSample () |
![]() | |
virtual | ~AbstractSensor () |
SensorErrors | getLastError () |
Get last error for debugging purposes. Avoid silent fails. | |
Static Public Member Functions | |
static SPIBusConfig | getDefaultSPIConfig () |
Static Public Attributes | |
static constexpr uint8_t | VALID_OPERATION |
Indicates a valid configuration. | |
static const ADS1118Config | ADS1118_DEFAULT_CONFIG |
Default configuration. | |
static constexpr int8_t | TEMP_CHANNEL = 8 |
Temperature channel number. | |
static constexpr int8_t | NUM_OF_CHANNELS = 9 |
static constexpr int8_t | INVALID_CHANNEL = -1 |
Protected Member Functions | |
ADS1118Data | sampleImpl () override |
Reads the previously configured channel while writing the next enabled configuration. | |
Additional Inherited Members | |
![]() | |
DataType | lastSample |
miosix::FastMutex | mutex |
![]() | |
SensorErrors | lastError = SensorErrors::NO_ERRORS |
Driver for ADS1118 adc.
The ADS1118 is a 16-bit delta-sigma analog-to-digital converter. It measures the difference of two inputs configured by a MUX. The device allows also to read each of the four pins in reference to GND. A temperature sensor is also included.
The communication uses the SPI protocol and the maximum allowed frequency is 4MHz. Configuration is applied by writing a 16bit value to the device. Sampled voltages or temperature readings are obtained also by reading a 16bit value while writing the configuration.
Data rate between 8Hz and 860Hz can be programmed and an internal programmable gain amplifier can be set with a sensitivity range from ±0.256V to ±6.144V (note that the inputs must remain between VCC or GND).
The data rate should be choosen as low as possible to allow the delta-sigma adc to average the input voltage (this allows a less noisy reading).
The device can work in two modes:
The ADS1118 is a simple device, it has a single data register where it can store the reading, therefore it can sample a single input at a time. sample() cycles through the enabled channels one at a time and writes it's configuration while reading the value of the previous written one.
As an example if you need to read 4 inputs at 50Hz you should set all the data rates at 250Hz (50Hz x * 4 = 200Hz) and call sample() at a rate of 200Hz.
An example for how to use the driver can be found in the test code (src/tests/drivers/test-ads1118.cpp)
Boardcore::ADS1118::ADS1118 | ( | SPIBusInterface & | bus, |
miosix::GpioPin | cs, | ||
ADS1118Config | config_, | ||
SPIBusConfig | spiConfig = getDefaultSPIConfig() ) |
Construct a new ADS1118 object specifying spi bus, spi config and cs pin as well as device configuration.
Definition at line 35 of file ADS1118.cpp.
|
explicit |
Construct a new ADS1118 object.
spiSlave_ | Spi slave configured with spi interface, spi config and ss pin. |
config_ | Device main configuration used as default while enabling channels. |
busyWait_ | Enable busy wait instead normal wait (uses delayUs instead of sleep ), only useful when sampling at close to the maximum frequency!. |
tempDivider_ | Specify how many onSimpleUpdate calls between each temperature reading. |
Definition at line 41 of file ADS1118.cpp.
void Boardcore::ADS1118::disableAllInputs | ( | ) |
Definition at line 93 of file ADS1118.cpp.
void Boardcore::ADS1118::disableConfigCheck | ( | ) |
Disables the configuration check after it's writing to the device.
Definition at line 112 of file ADS1118.cpp.
void Boardcore::ADS1118::disableInput | ( | ADS1118Mux | mux | ) |
Definition at line 91 of file ADS1118.cpp.
void Boardcore::ADS1118::disablePullUpResistor | ( | ) |
Definition at line 108 of file ADS1118.cpp.
void Boardcore::ADS1118::disableTemperature | ( | ) |
Definition at line 104 of file ADS1118.cpp.
void Boardcore::ADS1118::enableConfigCheck | ( | ) |
Enables the configuration check after it's writing to the device.
Definition at line 110 of file ADS1118.cpp.
void Boardcore::ADS1118::enableInput | ( | ADS1118Mux | mux | ) |
Enables the sapling of a specific mux configuration with the main configuration specified in the constructor.
mux | Mux configuration to enable. |
Definition at line 71 of file ADS1118.cpp.
void Boardcore::ADS1118::enableInput | ( | ADS1118Mux | mux, |
ADS1118DataRate | rate, | ||
ADS1118Pga | pga ) |
Enables the sampling of a specific mux configuration.
mux | Mux configuration to enable. |
rate | Data rate for this specific channel. |
pga | Gain for this specific channel. |
Definition at line 76 of file ADS1118.cpp.
void Boardcore::ADS1118::enablePullUpResistor | ( | ) |
Definition at line 106 of file ADS1118.cpp.
void Boardcore::ADS1118::enableTemperature | ( | ) |
Definition at line 99 of file ADS1118.cpp.
int Boardcore::ADS1118::getConversionTime | ( | int8_t | channel | ) |
Returns the conversion time in us for the specified channel.
Definition at line 134 of file ADS1118.cpp.
|
static |
Constructs the default config for SPI Bus.
Definition at line 61 of file ADS1118.cpp.
TemperatureData Boardcore::ADS1118::getTemperature | ( | ) |
Returns the last temperature value.
Definition at line 128 of file ADS1118.cpp.
ADS1118Data Boardcore::ADS1118::getVoltage | ( | ADS1118Mux | mux | ) |
Returns the last read voltage value for the specified channel.
Definition at line 126 of file ADS1118.cpp.
|
overridevirtual |
Initialize the configuration.
It resets all the channels thus you must call init() before enabling any channel.
Implements Boardcore::AbstractSensor.
Definition at line 69 of file ADS1118.cpp.
ADS1118Data Boardcore::ADS1118::readInputAndWait | ( | ADS1118Mux | mux | ) |
Reads on the fly the specified input.
mux | Mux configuration to read. |
Definition at line 114 of file ADS1118.cpp.
TemperatureData Boardcore::ADS1118::readTemperatureAndWait | ( | ) |
Reads on the fly the temperature.
Definition at line 120 of file ADS1118.cpp.
|
overrideprotectedvirtual |
Reads the previously configured channel while writing the next enabled configuration.
Multiple calls are needed to read all the enabled channels.
The sampling of all the enabled inputs is performed in sequence starting from the input with the lowest mux value.
The first configuration is written and then, at the next call, read back the result while transmitting the next configuration
Implements Boardcore::Sensor< ADS1118Data >.
Definition at line 173 of file ADS1118.cpp.
|
overridevirtual |
Writes the temperature configuration and check if it is read back correctly.
Implements Boardcore::AbstractSensor.
Definition at line 142 of file ADS1118.cpp.
|
static |
Default configuration.
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |