ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mcp3204.cpp
Go to the documentation of this file.
1#include "mcp3204.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mcp3204 {
5
6ESPHOME_LOG_TAG(TAG, "mcp3204");
7
9
10void MCP3204::setup() { this->spi_setup(); }
11
13 ESP_LOGCONFIG(TAG,
14 "MCP3204:\n"
15 " Reference Voltage: %.2fV",
16 this->reference_voltage_);
17 LOG_PIN(" CS Pin:", this->cs_);
18}
19
20float MCP3204::read_data(uint8_t pin, bool differential) {
21 const uint8_t command = (1 << 6) | // start bit
22 ((differential ? 0 : 1) << 5) | // single or differential bit
23 ((pin & 0x07) << 2); // pin
24 // One full-duplex transaction: command out, 12-bit result back in bytes 1 and 2.
25 // Word aligned so ESP-IDF DMA uses the buffer in place; only ESP32-P4 also checks the length and bounces.
26 alignas(4) uint8_t buffer[3] = {command, 0x00, 0x00};
27
28 this->enable();
29 this->transfer_array(buffer, sizeof(buffer));
30 this->disable();
31
32 uint16_t digital_value = encode_uint16(buffer[1], buffer[2]) >> 4;
33 return float(digital_value) / 4096.000f * this->reference_voltage_; // in V
34}
35
36} // namespace esphome::mcp3204
void dump_config() override
Definition mcp3204.cpp:12
float get_setup_priority() const override
Definition mcp3204.cpp:8
void setup() override
Definition mcp3204.cpp:10
float read_data(uint8_t pin, bool differential)
Definition mcp3204.cpp:20
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "mcp3204")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
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:998