ESPHome  2024.10.2
ads1118.cpp
Go to the documentation of this file.
1 #include "ads1118.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace ads1118 {
6 
7 static const char *const TAG = "ads1118";
8 static const uint8_t ADS1118_DATA_RATE_860_SPS = 0b111;
9 
11  ESP_LOGCONFIG(TAG, "Setting up ads1118");
12  this->spi_setup();
13 
14  this->config_ = 0;
15  // Setup multiplexer
16  // 0bx000xxxxxxxxxxxx
17  this->config_ |= ADS1118_MULTIPLEXER_P0_NG << 12;
18 
19  // Setup Gain
20  // 0bxxxx000xxxxxxxxx
21  this->config_ |= ADS1118_GAIN_6P144 << 9;
22 
23  // Set singleshot mode
24  // 0bxxxxxxx1xxxxxxxx
25  this->config_ |= 0b0000000100000000;
26 
27  // Set data rate - 860 samples per second (we're in singleshot mode)
28  // 0bxxxxxxxx100xxxxx
29  this->config_ |= ADS1118_DATA_RATE_860_SPS << 5;
30 
31  // Set temperature sensor mode - ADC
32  // 0bxxxxxxxxxxx0xxxx
33  this->config_ |= 0b0000000000000000;
34 
35  // Set DOUT pull up - enable
36  // 0bxxxxxxxxxxxx0xxx
37  this->config_ |= 0b0000000000001000;
38 
39  // NOP - must be 01
40  // 0bxxxxxxxxxxxxx01x
41  this->config_ |= 0b0000000000000010;
42 
43  // Not used - can be 0 or 1, lets be positive
44  // 0bxxxxxxxxxxxxxxx1
45  this->config_ |= 0b0000000000000001;
46 }
47 
49  ESP_LOGCONFIG(TAG, "ADS1118:");
50  LOG_PIN(" CS Pin:", this->cs_);
51 }
52 
53 float ADS1118::request_measurement(ADS1118Multiplexer multiplexer, ADS1118Gain gain, bool temperature_mode) {
54  uint16_t temp_config = this->config_;
55  // Multiplexer
56  // 0bxBBBxxxxxxxxxxxx
57  temp_config &= 0b1000111111111111;
58  temp_config |= (multiplexer & 0b111) << 12;
59 
60  // Gain
61  // 0bxxxxBBBxxxxxxxxx
62  temp_config &= 0b1111000111111111;
63  temp_config |= (gain & 0b111) << 9;
64 
65  if (temperature_mode) {
66  // Set temperature sensor mode
67  // 0bxxxxxxxxxxx1xxxx
68  temp_config |= 0b0000000000010000;
69  } else {
70  // Set ADC mode
71  // 0bxxxxxxxxxxx0xxxx
72  temp_config &= 0b1111111111101111;
73  }
74 
75  // Start conversion
76  temp_config |= 0b1000000000000000;
77 
78  this->enable();
79  this->write_byte16(temp_config);
80  this->disable();
81 
82  // about 1.2 ms with 860 samples per second
83  delay(2);
84 
85  this->enable();
86  uint8_t adc_first_byte = this->read_byte();
87  uint8_t adc_second_byte = this->read_byte();
88  this->disable();
89  uint16_t raw_conversion = encode_uint16(adc_first_byte, adc_second_byte);
90 
91  auto signed_conversion = static_cast<int16_t>(raw_conversion);
92 
93  if (temperature_mode) {
94  return (signed_conversion >> 2) * 0.03125f;
95  } else {
96  float millivolts;
97  float divider = 32768.0f;
98  switch (gain) {
99  case ADS1118_GAIN_6P144:
100  millivolts = (signed_conversion * 6144) / divider;
101  break;
102  case ADS1118_GAIN_4P096:
103  millivolts = (signed_conversion * 4096) / divider;
104  break;
105  case ADS1118_GAIN_2P048:
106  millivolts = (signed_conversion * 2048) / divider;
107  break;
108  case ADS1118_GAIN_1P024:
109  millivolts = (signed_conversion * 1024) / divider;
110  break;
111  case ADS1118_GAIN_0P512:
112  millivolts = (signed_conversion * 512) / divider;
113  break;
114  case ADS1118_GAIN_0P256:
115  millivolts = (signed_conversion * 256) / divider;
116  break;
117  default:
118  millivolts = NAN;
119  }
120 
121  return millivolts / 1e3f;
122  }
123 }
124 
125 } // namespace ads1118
126 } // namespace esphome
void setup() override
Definition: ads1118.cpp:10
GPIOPin * cs_
Definition: spi.h:378
AlsGain501 gain
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition: helpers.h:182
float request_measurement(ADS1118Multiplexer multiplexer, ADS1118Gain gain, bool temperature_mode)
Helper method to request a measurement from a sensor.
Definition: ads1118.cpp:53
void dump_config() override
Definition: ads1118.cpp:48
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26