ESPHome 2025.12.2
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, "MCP3204:");
15 LOG_PIN(" CS Pin:", this->cs_);
16 ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
17}
18
19float MCP3204::read_data(uint8_t pin, bool differential) {
20 uint8_t command, b0, b1;
21
22 command = (1 << 6) | // start bit
23 ((differential ? 0 : 1) << 5) | // single or differential bit
24 ((pin & 0x07) << 2); // pin
25
26 this->enable();
27 this->transfer_byte(command);
28 b0 = this->transfer_byte(0x00);
29 b1 = this->transfer_byte(0x00);
30 this->disable();
31
32 uint16_t digital_value = encode_uint16(b0, b1) >> 4;
33 return float(digital_value) / 4096.000 * this->reference_voltage_; // in V
34}
35
36} // namespace mcp3204
37} // 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:19
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:397