ESPHome
2026.6.0-dev
Loading...
Searching...
No Matches
esphome
components
mcp3204
mcp3204.cpp
Go to the documentation of this file.
1
#include "
mcp3204.h
"
2
#include "
esphome/core/log.h
"
3
4
namespace
esphome::mcp3204
{
5
6
static
const
char
*
const
TAG =
"mcp3204"
;
7
8
float
MCP3204::get_setup_priority
()
const
{
return
setup_priority::HARDWARE
; }
9
10
void
MCP3204::setup
() { this->
spi_setup
(); }
11
12
void
MCP3204::dump_config
() {
13
ESP_LOGCONFIG(TAG,
14
"MCP3204:\n"
15
" Reference Voltage: %.2fV"
,
16
this->
reference_voltage_
);
17
LOG_PIN(
" CS Pin:"
, this->
cs_
);
18
}
19
20
float
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
esphome::mcp3204::MCP3204::reference_voltage_
float reference_voltage_
Definition
mcp3204.h:23
esphome::mcp3204::MCP3204::dump_config
void dump_config() override
Definition
mcp3204.cpp:12
esphome::mcp3204::MCP3204::get_setup_priority
float get_setup_priority() const override
Definition
mcp3204.cpp:8
esphome::mcp3204::MCP3204::setup
void setup() override
Definition
mcp3204.cpp:10
esphome::mcp3204::MCP3204::read_data
float read_data(uint8_t pin, bool differential)
Definition
mcp3204.cpp:20
esphome::spi::SPIClient::cs_
GPIOPin * cs_
Definition
spi.h:414
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ >::enable
void enable()
Definition
spi.h:501
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ >::spi_setup
void spi_setup() override
Definition
spi.h:438
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ >::disable
void disable()
Definition
spi.h:503
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ >::transfer_byte
uint8_t transfer_byte(uint8_t data)
Definition
spi.h:487
log.h
mcp3204.h
esphome::mcp3204
Definition
mcp3204.cpp:4
esphome::setup_priority::HARDWARE
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition
component.h:41
esphome::encode_uint16
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
Generated by
1.12.0