MCP4725  1.0.0
MCP4725 library DAC for PICO rp2040
mcp4725.hpp
Go to the documentation of this file.
1 
10 #ifndef __MCP4725_DAC_H
11 #define __MCP4725_DAC_H
12 
13 // Libraries
14 #include <stdio.h> // optional for printf debug error messages
15 #include "pico/stdlib.h"
16 #include "hardware/i2c.h"
17 #include <cmath> // for pow() function
18 
19 // Section Enums
20 
22 typedef enum : uint8_t
23 {
29  MCP4725A2_Addr_A01 = 0x65
30 }MCP4725_I2C_Addr_e; // 8-bit i2c address
31 
33 typedef enum : uint8_t
34 {
37  MCP4725_EEPROM_Mode = 0x60
38 }MCP4725_CmdType_e; // DAC register, command bits C2C1C0 */
39 
41 typedef enum : uint8_t
42 {
47 }MCP4725_PowerDownType_e; // DAC register, power down bits PD1 PD0 , BSY,POR,xx,xx,xx,PD1,PD0,xx */
48 
50 typedef enum : uint8_t
51 {
55 }MCP4725_ReadType_e; // DAC library read register type
56 
58 typedef enum : uint8_t
59 {
63 }MCP4725_GeneralCallType_e; // DAC general call command datasheet 7.3
64 
65 // Section Definition's
66 
67 // I2C interface Comms related
68 #define MCP4725_I2C_DELAY 50000
69 #define MCP4725_ERROR 0xFFFF
70 #define MCP4725_EEPROM_WRITE_TIME 25
72 // DAC voltage levels
73 #define MCP4725_REFERENCE_VOLTAGE 3.3
74 #define MCP4725_RESOLUTION 12
75 #define MCP4725_STEPS pow(2, (MCP4725_RESOLUTION))
76 #define MCP4725_MAX_VALUE ((MCP4725_STEPS) - 1)
83 {
84  public:
86 
87  bool begin(MCP4725_I2C_Addr_e addr, i2c_inst_t* type, uint16_t speed, uint8_t SDA, uint8_t SCLK);
88  bool isConnected();
90  void deinitI2C();
91 
92  void setReferenceVoltage(float value);
93  float getReferenceVoltage(void);
94 
96  uint16_t getInputCode(void);
97 
99  float getVoltage(void);
100 
101  uint16_t getStoredInputCode(void);
102  float getStoredVoltage(void);
103 
104  uint16_t getPowerType(void);
105  uint16_t getStoredPowerType(void);
106 
107  void setSerialDebugFlag(bool onOff);
108  bool getSerialDebugFlag(void);
109 
110  void setSafetyCheckFlag(bool onOff);
111  bool getSafetyCheckFlag(void);
112 
113  private:
114  //I2c related
115  MCP4725_I2C_Addr_e _i2cAddress;
116  i2c_inst_t *_i2c; // i2C port number, i2c0 or i2c1
117  uint8_t _SDataPin;
118  uint8_t _SClkPin;
119  uint16_t _CLKSpeed = 100; // I2C bus speed in khz
120  bool _serialDebug = false; // Outputs Messages for debugging to serial port true = on
121 
122  float _refVoltage;
123  uint16_t _bitsPerVolt;
124  bool _safetyCheck = true; // Safety check for voltage level's , true = on
125 
126  bool getEEPROMBusyFlag(void);
127  bool writeCommand(uint16_t value, MCP4725_CmdType_e mode, MCP4725_PowerDownType_e powerType);
128  uint16_t readRegister(MCP4725_ReadType_e dataType);
129 };
130 
131 #endif // library file header guard endif
Class for MCP4725_PIC0 DAC.
Definition: mcp4725.hpp:83
uint16_t getStoredInputCode(void)
Read DAC inputCode from EEPROM.
Definition: mcp4725.cpp:181
float getVoltage(void)
get DAC inputCode from DAC register & convert to volts
Definition: mcp4725.cpp:168
uint16_t getInputCode(void)
get current DAC InputCode from DAC register
Definition: mcp4725.cpp:152
void setSafetyCheckFlag(bool onOff)
Setter for safety Check flag.
Definition: mcp4725.cpp:411
void setReferenceVoltage(float value)
Sets the reference voltage.
Definition: mcp4725.cpp:83
bool setVoltage(float voltage, MCP4725_CmdType_e=MCP4725_FastMode, MCP4725_PowerDownType_e=MCP4725_PowerDown_Off)
Set voltage out based on voltage input in volts.
Definition: mcp4725.cpp:124
bool getSafetyCheckFlag(void)
Gets the safety Check flag value.
Definition: mcp4725.cpp:417
bool begin(MCP4725_I2C_Addr_e addr, i2c_inst_t *type, uint16_t speed, uint8_t SDA, uint8_t SCLK)
Init & config i2c.
Definition: mcp4725.cpp:28
float getReferenceVoltage(void)
Gets the reference voltage.
Definition: mcp4725.cpp:97
uint16_t getStoredPowerType(void)
Get stored power type from EEPROM.
Definition: mcp4725.cpp:231
bool setInputCode(uint16_t inputCode, MCP4725_CmdType_e=MCP4725_FastMode, MCP4725_PowerDownType_e=MCP4725_PowerDown_Off)
Set voltage out based on DAC input code.
Definition: mcp4725.cpp:106
float getStoredVoltage(void)
Read stored DAC InputCode from EEPROM & convert to voltage.
Definition: mcp4725.cpp:197
bool getSerialDebugFlag(void)
Gets the serial Debug flag value.
Definition: mcp4725.cpp:405
void setSerialDebugFlag(bool onOff)
Setter for serial debug flag.
Definition: mcp4725.cpp:399
uint16_t getPowerType(void)
Get current power type from DAC register.
Definition: mcp4725.cpp:212
void deinitI2C()
Switch off the I2C interface and return I2C GPIO to default state.
Definition: mcp4725.cpp:51
bool GeneralCall(MCP4725_GeneralCallType_e)
General Call, name from datasheet section 7.3.
Definition: mcp4725.cpp:430
bool isConnected()
Checks if DAC is connected.
Definition: mcp4725.cpp:62
MCP4725_PICO(float refV=MCP4725_REFERENCE_VOLTAGE)
Constructor for class MCP4725_PIC0.
Definition: mcp4725.cpp:14
MCP4725_ReadType_e
Definition: mcp4725.hpp:51
@ MCP4725_ReadEEPROM
Definition: mcp4725.hpp:54
@ MCP4725_ReadDACReg
Definition: mcp4725.hpp:53
@ MCP4725_ReadSettings
Definition: mcp4725.hpp:52
MCP4725_GeneralCallType_e
Definition: mcp4725.hpp:59
@ MCP4725_GeneralCallWakeUp
Definition: mcp4725.hpp:62
@ MCP4725_GeneralCallAddress
Definition: mcp4725.hpp:60
@ MCP4725_GeneralCallReset
Definition: mcp4725.hpp:61
MCP4725_CmdType_e
Definition: mcp4725.hpp:34
@ MCP4725_EEPROM_Mode
Definition: mcp4725.hpp:37
@ MCP4725_FastMode
Definition: mcp4725.hpp:35
@ MCP4725_RegisterMode
Definition: mcp4725.hpp:36
#define MCP4725_REFERENCE_VOLTAGE
Definition: mcp4725.hpp:73
MCP4725_PowerDownType_e
Definition: mcp4725.hpp:42
@ MCP4725_PowerDown_Off
Definition: mcp4725.hpp:43
@ MCP4725_PowerDown_1kOhm
Definition: mcp4725.hpp:44
@ MCP4725_PowerDown_100kOhm
Definition: mcp4725.hpp:45
@ MCP4725_PowerDown_500kOhm
Definition: mcp4725.hpp:46
MCP4725_I2C_Addr_e
Definition: mcp4725.hpp:23
@ MCP4725A1_Addr_A01
Definition: mcp4725.hpp:27
@ MCP4725A0_Addr_A01
Definition: mcp4725.hpp:25
@ MCP4725A0_Addr_A00
Definition: mcp4725.hpp:24
@ MCP4725A2_Addr_A00
Definition: mcp4725.hpp:28
@ MCP4725A2_Addr_A01
Definition: mcp4725.hpp:29
@ MCP4725A1_Addr_A00
Definition: mcp4725.hpp:26