Skyward boardcore
Loading...
Searching...
No Matches
Boardcore::ADS1118 Class Reference

Driver for ADS1118 adc. More...

#include <ADS1118.h>

Inheritance diagram for Boardcore::ADS1118:
Collaboration diagram for Boardcore::ADS1118:

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 }
 
- Public Types inherited from Boardcore::Sensor< ADS1118Data >
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.
 
- Public Member Functions inherited from Boardcore::Sensor< ADS1118Data >
 Sensor ()
 
 Sensor (Sensor &&other)
 
virtual ~Sensor ()
 
void sample () override
 Sample the sensor.
 
virtual ADS1118Data getLastSample ()
 
- Public Member Functions inherited from Boardcore::AbstractSensor
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

- Protected Attributes inherited from Boardcore::Sensor< ADS1118Data >
DataType lastSample
 
miosix::FastMutex mutex
 
- Protected Attributes inherited from Boardcore::AbstractSensor
SensorErrors lastError = SensorErrors::NO_ERRORS
 

Detailed Description

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:

  • CONTINUOUS_CONV_MODE: Continuously read the last configured channel, when you make a read you'll obtain the lates reading
  • SINGLE_SHOT_MODE: A single conversion is performed when the configuration is written

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)

Definition at line 71 of file ADS1118.h.

Member Enumeration Documentation

◆ ADS1118DataRate

Enumerator
DR_8 

8 SPS

DR_16 

16 SPS

DR_32 

32 SPS

DR_64 

64 SPS

DR_128 

128 SPS (default)

DR_250 

250 SPS

DR_475 

475 SPS

DR_860 

860 SPS

Definition at line 102 of file ADS1118.h.

◆ ADS1118Mode

Enumerator
CONTINUOUS_CONV_MODE 

Continuous-conversion mode.

SINGLE_SHOT_MODE 

Power-down and single-shot mode (default)

Definition at line 96 of file ADS1118.h.

◆ ADS1118Mux

Enumerator
MUX_AIN0_AIN1 

AINp is AIN0 and AINn is AIN1 (default)

MUX_AIN0_AIN3 

AINp is AIN0 and AINn is AIN3.

MUX_AIN1_AIN3 

AINp is AIN1 and AINn is AIN3.

MUX_AIN2_AIN3 

AINp is AIN2 and AINn is AIN3.

MUX_AIN0_GND 

AINp is AIN0 and AINn is GND.

MUX_AIN1_GND 

AINp is AIN1 and AINn is GND.

MUX_AIN2_GND 

AINp is AIN2 and AINn is GND.

MUX_AIN3_GND 

AINp is AIN3 and AINn is GND.

Definition at line 74 of file ADS1118.h.

◆ ADS1118Pga

Enumerator
FSR_6_144 

FSR is ±6.144 V.

FSR_4_096 

FSR is ±4.096 V.

FSR_2_048 

FSR is ±2.048 V (default)

FSR_1_024 

FSR is ±1.024 V.

FSR_0_512 

FSR is ±0.512 V.

FSR_0_256 

FSR is ±0.256 V.

Definition at line 86 of file ADS1118.h.

◆ ADS1118PullUp

Enumerator
PULL_UP_DIS 

Pullup resistor disabled on DOUT pin.

PULL_UP_EN 

Pullup resistor enabled on DOUT pin (default)

Definition at line 120 of file ADS1118.h.

◆ ADS1118TempMode

Enumerator
ADC_MODE 

ADC mode (default)

TEMP_SENSOR_MODE 

Temperature sensor mode.

Definition at line 114 of file ADS1118.h.

Constructor & Destructor Documentation

◆ ADS1118() [1/2]

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.

◆ ADS1118() [2/2]

Boardcore::ADS1118::ADS1118 ( SPISlave spiSlave_,
ADS1118Config config_ = ADS1118_DEFAULT_CONFIG,
bool busyWait_ = false,
int16_t tempDivider_ = 100 )
explicit

Construct a new ADS1118 object.

Parameters
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.

Member Function Documentation

◆ disableAllInputs()

void Boardcore::ADS1118::disableAllInputs ( )

Definition at line 93 of file ADS1118.cpp.

◆ disableConfigCheck()

void Boardcore::ADS1118::disableConfigCheck ( )

Disables the configuration check after it's writing to the device.

Definition at line 112 of file ADS1118.cpp.

◆ disableInput()

void Boardcore::ADS1118::disableInput ( ADS1118Mux mux)

Definition at line 91 of file ADS1118.cpp.

◆ disablePullUpResistor()

void Boardcore::ADS1118::disablePullUpResistor ( )

Definition at line 108 of file ADS1118.cpp.

◆ disableTemperature()

void Boardcore::ADS1118::disableTemperature ( )

Definition at line 104 of file ADS1118.cpp.

◆ enableConfigCheck()

void Boardcore::ADS1118::enableConfigCheck ( )

Enables the configuration check after it's writing to the device.

Definition at line 110 of file ADS1118.cpp.

◆ enableInput() [1/2]

void Boardcore::ADS1118::enableInput ( ADS1118Mux mux)

Enables the sapling of a specific mux configuration with the main configuration specified in the constructor.

Parameters
muxMux configuration to enable.

Definition at line 71 of file ADS1118.cpp.

◆ enableInput() [2/2]

void Boardcore::ADS1118::enableInput ( ADS1118Mux mux,
ADS1118DataRate rate,
ADS1118Pga pga )

Enables the sampling of a specific mux configuration.

Parameters
muxMux configuration to enable.
rateData rate for this specific channel.
pgaGain for this specific channel.

Definition at line 76 of file ADS1118.cpp.

◆ enablePullUpResistor()

void Boardcore::ADS1118::enablePullUpResistor ( )

Definition at line 106 of file ADS1118.cpp.

◆ enableTemperature()

void Boardcore::ADS1118::enableTemperature ( )

Definition at line 99 of file ADS1118.cpp.

◆ getConversionTime()

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.

◆ getDefaultSPIConfig()

SPIBusConfig Boardcore::ADS1118::getDefaultSPIConfig ( )
static

Constructs the default config for SPI Bus.

Returns
the default SPIBusConfig.

Definition at line 61 of file ADS1118.cpp.

◆ getTemperature()

TemperatureData Boardcore::ADS1118::getTemperature ( )

Returns the last temperature value.

Definition at line 128 of file ADS1118.cpp.

◆ getVoltage()

ADS1118Data Boardcore::ADS1118::getVoltage ( ADS1118Mux mux)

Returns the last read voltage value for the specified channel.

Definition at line 126 of file ADS1118.cpp.

◆ init()

bool Boardcore::ADS1118::init ( )
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.

◆ readInputAndWait()

ADS1118Data Boardcore::ADS1118::readInputAndWait ( ADS1118Mux mux)

Reads on the fly the specified input.

Parameters
muxMux configuration to read.
Returns
Voltage value sampled from the channel in mV.

Definition at line 114 of file ADS1118.cpp.

◆ readTemperatureAndWait()

TemperatureData Boardcore::ADS1118::readTemperatureAndWait ( )

Reads on the fly the temperature.

Returns
Temperature in degree.

Definition at line 120 of file ADS1118.cpp.

◆ sampleImpl()

ADS1118Data Boardcore::ADS1118::sampleImpl ( )
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.

◆ selfTest()

bool Boardcore::ADS1118::selfTest ( )
overridevirtual

Writes the temperature configuration and check if it is read back correctly.

Returns
True if everything ok.

Implements Boardcore::AbstractSensor.

Definition at line 142 of file ADS1118.cpp.

Member Data Documentation

◆ ADS1118_DEFAULT_CONFIG

const ADS1118::ADS1118Config Boardcore::ADS1118::ADS1118_DEFAULT_CONFIG
static
Initial value:
= {
@ FSR_2_048
FSR is ±2.048 V (default)
Definition ADS1118.h:90
@ MUX_AIN0_AIN1
AINp is AIN0 and AINn is AIN1 (default)
Definition ADS1118.h:76
@ PULL_UP_EN
Pullup resistor enabled on DOUT pin (default)
Definition ADS1118.h:123
@ SINGLE_SHOT_MODE
Power-down and single-shot mode (default)
Definition ADS1118.h:99
static constexpr uint8_t VALID_OPERATION
Indicates a valid configuration.
Definition ADS1118.h:156
@ DR_128
128 SPS (default)
Definition ADS1118.h:108
@ ADC_MODE
ADC mode (default)
Definition ADS1118.h:116

Default configuration.

Definition at line 160 of file ADS1118.h.

◆ INVALID_CHANNEL

constexpr int8_t Boardcore::ADS1118::INVALID_CHANNEL = -1
staticconstexpr

Definition at line 166 of file ADS1118.h.

◆ NUM_OF_CHANNELS

constexpr int8_t Boardcore::ADS1118::NUM_OF_CHANNELS = 9
staticconstexpr

Definition at line 164 of file ADS1118.h.

◆ TEMP_CHANNEL

constexpr int8_t Boardcore::ADS1118::TEMP_CHANNEL = 8
staticconstexpr

Temperature channel number.

Definition at line 162 of file ADS1118.h.

◆ VALID_OPERATION

constexpr uint8_t Boardcore::ADS1118::VALID_OPERATION
staticconstexpr
Initial value:
=
0x1

Indicates a valid configuration.

Definition at line 156 of file ADS1118.h.


The documentation for this class was generated from the following files: