mirror of
https://github.com/esphome/esphome.git
synced 2025-01-07 13:21:44 +01:00
MCP3204 differential mode support
This commit is contained in:
parent
c90dcfc0ca
commit
b063fa17f8
5 changed files with 41 additions and 16 deletions
|
@ -19,16 +19,25 @@ void MCP3204::dump_config() {
|
|||
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
|
||||
}
|
||||
|
||||
float MCP3204::read_data(uint8_t pin) {
|
||||
uint8_t adc_primary_config = 0b00000110 | (pin >> 2);
|
||||
uint8_t adc_secondary_config = pin << 6;
|
||||
float MCP3204::read_data(uint8_t channel, bool differential) {
|
||||
|
||||
uint8_t command, sgldiff;
|
||||
uint8_t b0, b1, b2;
|
||||
|
||||
sgldiff = differential ? 0 : 1;
|
||||
|
||||
command = ((0x01 << 7) | // start bit
|
||||
(sgldiff << 6) | // single or differential
|
||||
((channel & 0x07) << 3)); // channel number
|
||||
|
||||
this->enable();
|
||||
this->transfer_byte(adc_primary_config);
|
||||
uint8_t adc_primary_byte = this->transfer_byte(adc_secondary_config);
|
||||
uint8_t adc_secondary_byte = this->transfer_byte(0x00);
|
||||
b0 = this->transfer_byte(command);
|
||||
b1 = this->transfer_byte(0x00);
|
||||
b2 = this->transfer_byte(0x00);
|
||||
this->disable();
|
||||
uint16_t digital_value = (adc_primary_byte << 8 | adc_secondary_byte) & 0b111111111111;
|
||||
return float(digital_value) / 4096.000 * this->reference_voltage_;
|
||||
|
||||
uint16_t digital_value = 0xFFF & ((b0 & 0x01) << 11 | (b1 & 0xFF) << 3 | (b2 & 0xE0) >> 5);
|
||||
return float(digital_value) / 4096.000 * this->reference_voltage_; // in V
|
||||
}
|
||||
|
||||
} // namespace mcp3204
|
||||
|
|
|
@ -8,17 +8,22 @@ namespace esphome {
|
|||
namespace mcp3204 {
|
||||
|
||||
class MCP3204 : public Component,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST,
|
||||
spi::CLOCK_POLARITY_LOW,
|
||||
spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_1MHZ> {
|
||||
public:
|
||||
MCP3204() = default;
|
||||
|
||||
void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
|
||||
void set_reference_voltage(float reference_voltage)
|
||||
{
|
||||
this->reference_voltage_ = reference_voltage;
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
float read_data(uint8_t pin);
|
||||
float read_data(uint8_t pin, bool diffmode);
|
||||
|
||||
protected:
|
||||
float reference_voltage_;
|
||||
|
|
|
@ -12,6 +12,7 @@ MCP3204Sensor = mcp3204_ns.class_(
|
|||
"MCP3204Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
|
||||
)
|
||||
CONF_MCP3204_ID = "mcp3204_id"
|
||||
CONF_DIFF_MODE = "diff_mode"
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(MCP3204Sensor)
|
||||
|
@ -19,6 +20,7 @@ CONFIG_SCHEMA = (
|
|||
{
|
||||
cv.GenerateID(CONF_MCP3204_ID): cv.use_id(MCP3204),
|
||||
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=7),
|
||||
cv.Optional(CONF_DIFF_MODE, default = 'false'): cv.boolean,
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
|
@ -29,6 +31,7 @@ async def to_code(config):
|
|||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
config[CONF_NUMBER],
|
||||
config[CONF_DIFF_MODE],
|
||||
)
|
||||
await cg.register_parented(var, config[CONF_MCP3204_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
|
|
@ -7,17 +7,24 @@ namespace mcp3204 {
|
|||
|
||||
static const char *const TAG = "mcp3204.sensor";
|
||||
|
||||
MCP3204Sensor::MCP3204Sensor(uint8_t pin) : pin_(pin) {}
|
||||
MCP3204Sensor::MCP3204Sensor(uint8_t pin, bool diffmode) : pin_(pin), diffmode_(diffmode) {}
|
||||
|
||||
float MCP3204Sensor::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
void MCP3204Sensor::dump_config() {
|
||||
LOG_SENSOR("", "MCP3204 Sensor", this);
|
||||
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
|
||||
ESP_LOGCONFIG(TAG, " PIN: %u", this->pin_);
|
||||
ESP_LOGCONFIG(TAG, " DIFFERENTIAL MODE: %u", this->diffmode_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
float MCP3204Sensor::sample() { return this->parent_->read_data(this->pin_); }
|
||||
void MCP3204Sensor::update() { this->publish_state(this->sample()); }
|
||||
float MCP3204Sensor::sample()
|
||||
{
|
||||
return this->parent_->read_data(this->pin_, this->diffmode_);
|
||||
}
|
||||
void MCP3204Sensor::update()
|
||||
{
|
||||
this->publish_state(this->sample());
|
||||
}
|
||||
|
||||
} // namespace mcp3204
|
||||
} // namespace esphome
|
||||
|
|
|
@ -15,7 +15,7 @@ class MCP3204Sensor : public PollingComponent,
|
|||
public sensor::Sensor,
|
||||
public voltage_sampler::VoltageSampler {
|
||||
public:
|
||||
MCP3204Sensor(uint8_t pin);
|
||||
MCP3204Sensor(uint8_t pin, bool diffmode);
|
||||
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
@ -24,6 +24,7 @@ class MCP3204Sensor : public PollingComponent,
|
|||
|
||||
protected:
|
||||
uint8_t pin_;
|
||||
uint8_t diffmode_;
|
||||
};
|
||||
|
||||
} // namespace mcp3204
|
||||
|
|
Loading…
Reference in a new issue