parent
6f7b9d2b53
commit
39b9426f71
@ -0,0 +1,76 @@
|
||||
#include "mcp4725.hpp"
|
||||
|
||||
MCP4725::MCP4725(pwrd_md power_down_mode,
|
||||
i2c_addr address,
|
||||
i2c_write_n_t i2c_write) :
|
||||
power_down_mode(power_down_mode),
|
||||
address(address),
|
||||
i2c_write(i2c_write)
|
||||
{
|
||||
uint8_t temp[6];
|
||||
|
||||
temp[0] = write_dac_and_eeprom | (power_down_mode << 1);
|
||||
temp[1] = static_cast<uint8_t>(dac_value >> 4);
|
||||
temp[2] = static_cast<uint8_t>(dac_value << 4);
|
||||
temp[3] = temp[0];
|
||||
temp[4] = temp[1];
|
||||
temp[5] = temp[2];
|
||||
|
||||
i2c_write(address, temp,6);
|
||||
}
|
||||
|
||||
MCP4725::MCP4725(i2c_addr address,
|
||||
i2c_write_n_t i2c_write)
|
||||
power_down_mode(MCP4725::normal),
|
||||
address(address),
|
||||
i2c_write(i2c_write)
|
||||
{
|
||||
uint8_t temp[6];
|
||||
|
||||
temp[0] = write_dac_and_eeprom | (power_down_mode << 1);
|
||||
temp[1] = static_cast<uint8_t>(dac_value >> 4);
|
||||
temp[2] = static_cast<uint8_t>(dac_value << 4);
|
||||
temp[3] = temp[0];
|
||||
temp[4] = temp[1];
|
||||
temp[5] = temp[2];
|
||||
|
||||
i2c_write(address, temp, 6);
|
||||
}
|
||||
|
||||
MCP4725::~MCP4725()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MCP4725::operator=(uint16_t dac_value)
|
||||
{
|
||||
uint8_t temp[3];
|
||||
this.dac_value = dac_value;
|
||||
|
||||
temp[0] = write_dac | (power_down_mode << 1);
|
||||
temp[1] = static_cast<uint8_t>(dac_value >> 4);
|
||||
temp[2] = static_cast<uint8_t>(dac_value << 4);
|
||||
|
||||
i2c_write(address, temp, 3);
|
||||
}
|
||||
|
||||
void MCP4725::operator==(uint16_t dac_and_eeprom_value)
|
||||
{
|
||||
uint8_t temp[6];
|
||||
dac_value = dac_and_eeprom_value;
|
||||
eeprom_value = dac_value;
|
||||
|
||||
temp[0] = write_dac_and_eeprom | (power_down_mode << 1);
|
||||
}
|
||||
|
||||
void MCP4725::write_dac(uint16_t dac_value)
|
||||
{
|
||||
&this = dac_value;
|
||||
}
|
||||
|
||||
void MCP4725::write_dac_and_eeprom(uint16_t dac_and_eeprom_value)
|
||||
{
|
||||
&this == dac_and_eeprom_value;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,75 @@
|
||||
#ifndef _MCP4725_HPP_
|
||||
#define _MCP4725_HPP_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <functional>
|
||||
|
||||
class MCP4725
|
||||
{
|
||||
public:
|
||||
// address list
|
||||
enum i2c_addr
|
||||
{
|
||||
addr_0x0 = 0b01100000,
|
||||
addr_0x1 = addr_0x0 + 1,
|
||||
addr_0x2 = addr_0x0 + 1,
|
||||
addr_0x3 = addr_0x0 + 1,
|
||||
addr_0x4 = addr_0x0 + 1,
|
||||
addr_0x5 = addr_0x0 + 1,
|
||||
addr_0x6 = addr_0x0 + 1,
|
||||
addr_0x7 = addr_0x0 + 1
|
||||
}
|
||||
|
||||
// power down impedance modes
|
||||
enum pwrd_md
|
||||
{
|
||||
normal = 0x04,
|
||||
ohm_1k = 0x01,
|
||||
ohm_100k = 0x02,
|
||||
ohm_500k = 0x03
|
||||
};
|
||||
|
||||
//using std::functional<uint8_t(uint8_t, uint8_t)> i2c_read_n_t;
|
||||
|
||||
using i2c_write_n_t = std::functional<void(uint8_t, uint8_t*,uint8_t)>;
|
||||
|
||||
MCP4725(pwrd_md power_down_mode,
|
||||
i2c_addr address,
|
||||
i2c_write_n_t i2c_write);
|
||||
|
||||
MCP4725(i2c_addr address,
|
||||
i2c_write_n_t i2c_write);
|
||||
|
||||
~MCP4725();
|
||||
|
||||
void operator=(uint16_t dac_value);
|
||||
|
||||
void operator==(uint16_t dac_and_eeprom_value);
|
||||
|
||||
void write_dac_and_eeprom(uint16_t value);
|
||||
|
||||
void set_powerdown_impedance(pwrd_md mode);
|
||||
|
||||
i2c_addr get_i2c_addr();
|
||||
|
||||
void set_power
|
||||
|
||||
|
||||
private:
|
||||
|
||||
enum commands_t
|
||||
{
|
||||
fast_mode = 0x00,
|
||||
write_dac = 0x40,
|
||||
write_dac_and_eeprom = 0x60
|
||||
};
|
||||
|
||||
i2c_addr address;
|
||||
pwrd_md power_down_mode;
|
||||
i2c_write_n_t i2c_write;
|
||||
uint16_t dac_value;
|
||||
uint16_t eeprom_value;
|
||||
|
||||
};
|
||||
|
||||
#endif // _MCP4725_HPP_
|
Loading…
Reference in new issue