Classes to interact with our Mbed.
Dependents: MCP4725I2Ctest MCP4725Translator
Fork of MCP4728 by
mcp4725.cpp@5:fa31978934fd, 2016-04-01 (annotated)
- Committer:
- katrijnverhasselt
- Date:
- Fri Apr 01 08:40:34 2016 +0000
- Revision:
- 5:fa31978934fd
- Child:
- 6:e83ec147be06
Classes with functions to interact with our chip.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
katrijnverhasselt | 5:fa31978934fd | 1 | #include "mcp4725.h" |
katrijnverhasselt | 5:fa31978934fd | 2 | #include "mbed.h" |
katrijnverhasselt | 5:fa31978934fd | 3 | |
katrijnverhasselt | 5:fa31978934fd | 4 | |
katrijnverhasselt | 5:fa31978934fd | 5 | #define VERSION_MCP4725_SRC "0.20" |
katrijnverhasselt | 5:fa31978934fd | 6 | |
katrijnverhasselt | 5:fa31978934fd | 7 | MCP4725::MCP4725(I2CInterface* i2cinterface, int device_address_bits, float Vdd_): |
katrijnverhasselt | 5:fa31978934fd | 8 | _i2c_interface(i2cinterface),getVersion( VERSION_MCP4725_HDR,VERSION_MCP4725_SRC, __TIME__, __DATE__) |
katrijnverhasselt | 5:fa31978934fd | 9 | { |
katrijnverhasselt | 5:fa31978934fd | 10 | Vdd=Vdd_; |
katrijnverhasselt | 5:fa31978934fd | 11 | // Assemble the full I2C device address. |
katrijnverhasselt | 5:fa31978934fd | 12 | _device_address = 0xC0; // Prime the full device address with the device code. |
katrijnverhasselt | 5:fa31978934fd | 13 | _device_address |= (device_address_bits<<1); |
katrijnverhasselt | 5:fa31978934fd | 14 | } |
katrijnverhasselt | 5:fa31978934fd | 15 | |
katrijnverhasselt | 5:fa31978934fd | 16 | int MCP4725::getDACvalue(int& value, int ch){ |
katrijnverhasselt | 5:fa31978934fd | 17 | // has to limit to reading only the dac value not the other items. |
katrijnverhasselt | 5:fa31978934fd | 18 | enum PowerMode mode; enum PowerMode mode_eeprom; int dac_value; int dac_value_eeprom; bool eeprom_write_in_progress; |
katrijnverhasselt | 5:fa31978934fd | 19 | int status = read( mode, mode_eeprom, dac_value, dac_value_eeprom, eeprom_write_in_progress); |
katrijnverhasselt | 5:fa31978934fd | 20 | value=dac_value; |
katrijnverhasselt | 5:fa31978934fd | 21 | return status; |
katrijnverhasselt | 5:fa31978934fd | 22 | } |
katrijnverhasselt | 5:fa31978934fd | 23 | |
katrijnverhasselt | 5:fa31978934fd | 24 | |
katrijnverhasselt | 5:fa31978934fd | 25 | int MCP4725::getVoltage(float &voltage, int ch){ |
katrijnverhasselt | 5:fa31978934fd | 26 | int value; |
katrijnverhasselt | 5:fa31978934fd | 27 | int status =getDACvalue(value); |
katrijnverhasselt | 5:fa31978934fd | 28 | voltage = Vdd * (float) value / 4096; |
katrijnverhasselt | 5:fa31978934fd | 29 | return status; |
katrijnverhasselt | 5:fa31978934fd | 30 | } |
katrijnverhasselt | 5:fa31978934fd | 31 | |
katrijnverhasselt | 5:fa31978934fd | 32 | |
katrijnverhasselt | 5:fa31978934fd | 33 | int MCP4725::read(enum PowerMode& mode, enum PowerMode& mode_eeprom, int& dac_value, int& dac_value_eeprom, bool& eeprom_write_in_progress) |
katrijnverhasselt | 5:fa31978934fd | 34 | { |
katrijnverhasselt | 5:fa31978934fd | 35 | char data[5]; |
katrijnverhasselt | 5:fa31978934fd | 36 | int result; |
katrijnverhasselt | 5:fa31978934fd | 37 | |
katrijnverhasselt | 5:fa31978934fd | 38 | // Read the raw data from the device. |
katrijnverhasselt | 5:fa31978934fd | 39 | result = _i2c_interface->read(_device_address, data, sizeof(data)/sizeof(*data), false); |
katrijnverhasselt | 5:fa31978934fd | 40 | |
katrijnverhasselt | 5:fa31978934fd | 41 | // Parse the raw data, extracting our fields. Refer to MCP4725 ref manual, section 6.2 |
katrijnverhasselt | 5:fa31978934fd | 42 | if (result == 0) |
katrijnverhasselt | 5:fa31978934fd | 43 | { |
katrijnverhasselt | 5:fa31978934fd | 44 | eeprom_write_in_progress = (data[0] & 0x80)? false:true; |
katrijnverhasselt | 5:fa31978934fd | 45 | |
katrijnverhasselt | 5:fa31978934fd | 46 | mode = (enum PowerMode) ((data[0] & 0x06)>>1); |
katrijnverhasselt | 5:fa31978934fd | 47 | |
katrijnverhasselt | 5:fa31978934fd | 48 | dac_value = (data[1]<<4) + (data[2]>>4); |
katrijnverhasselt | 5:fa31978934fd | 49 | |
katrijnverhasselt | 5:fa31978934fd | 50 | mode_eeprom = (enum PowerMode)((data[3] & 0x60)>>5); |
katrijnverhasselt | 5:fa31978934fd | 51 | |
katrijnverhasselt | 5:fa31978934fd | 52 | dac_value_eeprom = ((data[3] & 0x0F) <<8) + data[4]; |
katrijnverhasselt | 5:fa31978934fd | 53 | } |
katrijnverhasselt | 5:fa31978934fd | 54 | |
katrijnverhasselt | 5:fa31978934fd | 55 | return result; |
katrijnverhasselt | 5:fa31978934fd | 56 | } |
katrijnverhasselt | 5:fa31978934fd | 57 | |
katrijnverhasselt | 5:fa31978934fd | 58 | |
katrijnverhasselt | 5:fa31978934fd | 59 | int MCP4725::setDACvalue( int value, int ch){ |
katrijnverhasselt | 5:fa31978934fd | 60 | return write( Normal, value, false); |
katrijnverhasselt | 5:fa31978934fd | 61 | } |
katrijnverhasselt | 5:fa31978934fd | 62 | |
katrijnverhasselt | 5:fa31978934fd | 63 | |
katrijnverhasselt | 5:fa31978934fd | 64 | int MCP4725::setVoltage (float voltage, int ch){ |
katrijnverhasselt | 5:fa31978934fd | 65 | int value = 4096 * voltage /Vdd; |
katrijnverhasselt | 5:fa31978934fd | 66 | return write( Normal, value, false); |
katrijnverhasselt | 5:fa31978934fd | 67 | } |
katrijnverhasselt | 5:fa31978934fd | 68 | |
katrijnverhasselt | 5:fa31978934fd | 69 | |
katrijnverhasselt | 5:fa31978934fd | 70 | |
katrijnverhasselt | 5:fa31978934fd | 71 | |
katrijnverhasselt | 5:fa31978934fd | 72 | |
katrijnverhasselt | 5:fa31978934fd | 73 | int MCP4725::write(enum PowerMode mode, int dac_value, bool writeToEeprom) |
katrijnverhasselt | 5:fa31978934fd | 74 | { |
katrijnverhasselt | 5:fa31978934fd | 75 | char data[3]={0}; |
katrijnverhasselt | 5:fa31978934fd | 76 | int write_command; |
katrijnverhasselt | 5:fa31978934fd | 77 | |
katrijnverhasselt | 5:fa31978934fd | 78 | //Which write command are we to use? |
katrijnverhasselt | 5:fa31978934fd | 79 | if (writeToEeprom == true) |
katrijnverhasselt | 5:fa31978934fd | 80 | { |
katrijnverhasselt | 5:fa31978934fd | 81 | //Write DAC Register and EEPROM |
katrijnverhasselt | 5:fa31978934fd | 82 | write_command = 3; |
katrijnverhasselt | 5:fa31978934fd | 83 | } |
katrijnverhasselt | 5:fa31978934fd | 84 | else |
katrijnverhasselt | 5:fa31978934fd | 85 | { |
katrijnverhasselt | 5:fa31978934fd | 86 | //Write DAC Register |
katrijnverhasselt | 5:fa31978934fd | 87 | write_command = 2; |
katrijnverhasselt | 5:fa31978934fd | 88 | } |
katrijnverhasselt | 5:fa31978934fd | 89 | |
katrijnverhasselt | 5:fa31978934fd | 90 | //Assemble our three bytes of data - Refer to MCP4725 ref manual, section 6. |
katrijnverhasselt | 5:fa31978934fd | 91 | data[0] = (write_command <<5) | ((int)mode<<1); |
katrijnverhasselt | 5:fa31978934fd | 92 | data[1] = (dac_value>>4); |
katrijnverhasselt | 5:fa31978934fd | 93 | data[2] = (dac_value<<4); |
katrijnverhasselt | 5:fa31978934fd | 94 | |
katrijnverhasselt | 5:fa31978934fd | 95 | // Now write them to the device. |
katrijnverhasselt | 5:fa31978934fd | 96 | return _i2c_interface->write(_device_address, data, sizeof(data)/sizeof(*data), false); |
katrijnverhasselt | 5:fa31978934fd | 97 | } |