ESPHome 2026.1.5
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 {
5namespace mcp3204 {
6
7static const char *const TAG = "mcp3204";
8
10
11void MCP3204::setup() { this->spi_setup(); }
12
14 ESP_LOGCONFIG(TAG,
15 "MCP3204:\n"
16 " Reference Voltage: %.2fV",
17 this->reference_voltage_);
18 LOG_PIN(" CS Pin:", this->cs_);
19}
20
21float MCP3204::read_data(uint8_t pin, bool differential) {
22 uint8_t command, b0, b1;
23
24 command = (1 << 6) | // start bit
25 ((differential ? 0 : 1) << 5) | // single or differential bit
26 ((pin & 0x07) << 2); // pin
27
28 this->enable();
29 this->transfer_byte(command);
30 b0 = this->transfer_byte(0x00);
31 b1 = this->transfer_byte(0x00);
32 this->disable();
33
34 uint16_t digital_value = encode_uint16(b0, b1) >> 4;
35 return float(digital_value) / 4096.000 * this->reference_voltage_; // in V
36}
37
38} // namespace mcp3204
39} // namespace esphome
void dump_config() override
Definition mcp3204.cpp:13
float get_setup_priority() const override
Definition mcp3204.cpp:9
void setup() override
Definition mcp3204.cpp:11
float read_data(uint8_t pin, bool differential)
Definition mcp3204.cpp:21
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:463