ESPHome  2022.9.1
mcp3204.cpp
Go to the documentation of this file.
1 #include "mcp3204.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mcp3204 {
6 
7 static const char *const TAG = "mcp3204";
8 
10 
12  ESP_LOGCONFIG(TAG, "Setting up mcp3204");
13  this->spi_setup();
14 }
15 
17  ESP_LOGCONFIG(TAG, "MCP3204:");
18  LOG_PIN(" CS Pin:", this->cs_);
19  ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
20 }
21 
22 float MCP3204::read_data(uint8_t pin) {
23  uint8_t adc_primary_config = 0b00000110 | (pin >> 2);
24  uint8_t adc_secondary_config = pin << 6;
25  this->enable();
26  this->transfer_byte(adc_primary_config);
27  uint8_t adc_primary_byte = this->transfer_byte(adc_secondary_config);
28  uint8_t adc_secondary_byte = this->transfer_byte(0x00);
29  this->disable();
30  uint16_t digital_value = (adc_primary_byte << 8 | adc_secondary_byte) & 0b111111111111;
31  return float(digital_value) / 4096.000 * this->reference_voltage_;
32 }
33 
34 } // namespace mcp3204
35 } // namespace esphome
float read_data(uint8_t pin)
Definition: mcp3204.cpp:22
void dump_config() override
Definition: mcp3204.cpp:16
float get_setup_priority() const override
Definition: mcp3204.cpp:9
void setup() override
Definition: mcp3204.cpp:11
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
Definition: a4988.cpp:4