ESPHome 2026.6.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
6static const char *const 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 uint8_t command, b0, b1;
22
23 command = (1 << 6) | // start bit
24 ((differential ? 0 : 1) << 5) | // single or differential bit
25 ((pin & 0x07) << 2); // pin
26
27 this->enable();
28 this->transfer_byte(command);
29 b0 = this->transfer_byte(0x00);
30 b1 = this->transfer_byte(0x00);
31 this->disable();
32
33 uint16_t digital_value = encode_uint16(b0, b1) >> 4;
34 return float(digital_value) / 4096.000 * this->reference_voltage_; // in V
35}
36
37} // 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
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
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:859