Modular Electronics  0.1
 All Classes Namespaces Functions Variables Pages
dac.h
1 // Class for a DAC chip or board
2 // Copyright (C) 2018 Sasha Zibrov
3 // Copyright (C) 2018 Carlos Kometter
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 #ifndef DAC_h_
19 #define DAC_h_
20 
21 #include "Arduino.h"
22 #include "SPI.h"
23 #include "utils.h"
24 
25 typedef unsigned char byte;
26 
35 class DacSpi {
36  private:
37  // sync_pin_ identifies the dac chip; each chip has a unique sync_pin_.
38  // spi_bus_config_pin_ identifies the bus configurations.
39  // More than one dac can share the same bus if they use the same spi configs.
40  uint8_t sync_pin_, spi_bus_config_pin_, ldac_pin_, spi_mode_;
41  uint8_t clock_divider_; // For due frequency = 84 MHz / clock_divider_
42  BitOrder bit_order_; // MSBFIRST or LSBFIRST
43  uint8_t bit_resolution_;
44  public:
45  DacSpi(void) = default;
63  DacSpi(uint8_t sync_pin, uint8_t spi_bus_config_pin,
64  uint8_t ldac_pin, uint8_t bit_resolution,
65  uint8_t clock_divider=4, BitOrder bit_order=MSBFIRST,
66  uint8_t spi_mode=SPI_MODE1);
72  uint8_t Begin(void);
77  uint8_t Initialize(void);
87  double SetVoltage(uint8_t channel, double voltage,
88  bool update_outputs = true);
92  void UpdateAnalogOutputs(void);
98  double GetVoltage(uint8_t channel);
99  protected:
108  virtual double BytesToVoltage(spi_utils::Message message) {};
114  virtual spi_utils::Message SetVoltageMessage(uint8_t channel, double voltage) {};
119 };
120 #endif
double SetVoltage(uint8_t channel, double voltage, bool update_outputs=true)
Definition: dac.cpp:66
uint8_t Initialize(void)
Definition: dac.cpp:46
Definition: dac.h:35
void UpdateAnalogOutputs(void)
Definition: dac.cpp:62
Definition: utils.h:35
uint8_t Begin(void)
Definition: dac.cpp:31
virtual spi_utils::Message InitializeMessage(void)
Definition: dac.h:118
virtual spi_utils::Message SetVoltageMessage(uint8_t channel, double voltage)
Definition: dac.h:114
virtual double BytesToVoltage(spi_utils::Message message)
Definition: dac.h:108
double GetVoltage(uint8_t channel)
Definition: dac.cpp:87