Modular Electronics  0.1
 All Classes Namespaces Functions Variables Pages
adc.h
1 // Class for a ADC chip or board
2 // Copyright (C) 2018 Carlos Kometter
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 
17 #ifndef ADC_h_
18 #define ADC_h_
19 
20 #include "Arduino.h"
21 #include "SPI.h"
22 #include "utils.h"
23 
24 typedef unsigned char byte;
25 
33 class AdcSpi {
34  private:
35  // sync_pin_ identifies the adc chip; each chip has a unique sync_pin_.
36  // spi_bus_config_pin_ identifies the bus configurations.
37  // More than one adc can share the same bus if they use the same spi configs.
38  uint8_t sync_pin_, spi_bus_config_pin_, data_ready_pin_, spi_mode_;
39  uint8_t clock_divider_; // For due frequency = 84 MHz / clock_divider_
40  BitOrder bit_order_; // MSBFIRST or LSBFIRST
41  uint8_t bit_resolution_;
42  uint8_t reset_pin_;
43  public:
44  AdcSpi(void) = default;
64  AdcSpi(uint8_t sync_pin, uint8_t spi_bus_config_pin,
65  uint8_t data_ready_pin, uint8_t bit_resolution,
66  uint8_t reset_pin=0, uint8_t clock_divider=7,
67  BitOrder bit_order=MSBFIRST, uint8_t spi_mode=SPI_MODE3);
72  bool Begin(void);
78  double ReadVoltage(uint8_t channel=0);
91  uint8_t ReadVoltage(uint8_t channel, byte previous_meas[], bool send);
92  protected:
101  virtual double BytesToVoltage(spi_utils::Message message) = 0;
106  virtual spi_utils::Message SingleConversionModeMessage(uint8_t channel=0) = 0;
111  virtual spi_utils::Message ReadDataRegisterMessage(uint8_t channel=0) = 0;
112 };
113 #endif
virtual spi_utils::Message ReadDataRegisterMessage(uint8_t channel=0)=0
Definition: utils.h:35
virtual double BytesToVoltage(spi_utils::Message message)=0
double ReadVoltage(uint8_t channel=0)
Definition: adc.cpp:48
bool Begin(void)
Definition: adc.cpp:30
Definition: adc.h:33
virtual spi_utils::Message SingleConversionModeMessage(uint8_t channel=0)=0