hello
Fork of MCP4725 by
Revision 7:f5e11acbe221, committed 2014-12-11
- Comitter:
- Skyyl
- Date:
- Thu Dec 11 21:39:37 2014 +0000
- Parent:
- 6:35e3e80b804c
- Commit message:
- hello
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP4725.cpp Thu Dec 11 21:39:37 2014 +0000 @@ -0,0 +1,78 @@ +#include "MCP4725.h" +#include "mbed.h" + +MCP4725::MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits): _i2c_interface(sda, scl) +{ + //Set the frequency + int freq=0; + switch(bus_frequency) + { + case Standard100kHz: + freq = 100000; + break; + case Fast400kHz: + freq = 400000; + break; + case HighSpeed3_4Mhz: + freq = 3400000; + break; + } + _i2c_interface.frequency(freq); + + // Assemble the full I2C device address. + _device_address = 0xC4; // Prime the full device address with the device code. + //_device_address |= (device_address_bits<<1); +} + +int MCP4725::read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress) +{ + char data[5]; + int result; + + // Read the raw data from the device. + result = _i2c_interface.read(_device_address, data, sizeof(data)/sizeof(*data), false); + + // Parse the raw data, extracting our fields. Refer to MCP4725 ref manual, section 6.2 + if (result == 0) + { + *eeprom_write_in_progress = (data[0] & 0x80)? false:true; + + *mode = (enum PowerMode) ((data[0] & 0x06)>>1); + + *dac_value = (data[1]<<4) + (data[2]>>4); + + *mode_eeprom = (enum PowerMode)((data[3] & 0x60)>>5); + + *dac_value_eeprom = ((data[3] & 0x0F) <<8) + data[4]; + } + + return result; +} + + + +int MCP4725::write(enum PowerMode mode, int dac_value, bool writeToEeprom) +{ + char data[3]={0}; + int write_command; + + //Which write command are we to use? + if (writeToEeprom == true) + { + //Write DAC Register and EEPROM + write_command = 3; + } + else + { + //Write DAC Register + write_command = 2; + } + + //Assemble our three bytes of data - Refer to MCP4725 ref manual, section 6. + data[0] = (write_command <<5) | ((int)mode<<1); + data[1] = (dac_value>>4); + data[2] = (dac_value<<4); + + // Now write them to the device. + return _i2c_interface.write(_device_address, data, sizeof(data)/sizeof(*data), false); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP4725.h Thu Dec 11 21:39:37 2014 +0000 @@ -0,0 +1,91 @@ +#ifndef MCP_4725_H +#define MCP_4725_H + +#include "stdbool.h" +#include "mbed.h" +#include "I2C.h" + + +/** MCP4725 class. + * Used for interfacing with a mcp4725 12-Bit Digital-to-Analog Converter. + * To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is + * the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts. + * + * Note: There is an accompanying test suite program "MCP_4725_Library_Test" that can be used to test this library. + * + */ +class MCP4725 +{ + public: + /** The device supports two types of power modes: normal and power-down. In normal mode the device + * operates a normal digital to analog conversion. In power-down mode all digitial to analog + * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power + * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ + enum PowerMode { + /** In normal mode the device operates a normal D2A conversion. */ + Normal=0, + /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ + PowerDown1k=1, + /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ + PowerDown100k=2, + /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ + PowerDown500k=3 + }; + + /** The device supports 3 different I2C bus frequencies.*/ + enum BusFrequency { + /** Standard 100kHz bus. */ + Standard100kHz, + /** Fast 400kHz bus. */ + Fast400kHz, + /** High Speed 3.4Mhz bus. WARNING: the test suite fails for the mbed LPC1768 when this frequency is selected - not tested on other platforms.*/ + HighSpeed3_4Mhz + }; + + /** Create an mcp4725 I2C interface + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + * @param bus_frequency the frequency at which the I2C bus is running. + * @param device_address_bits The 3bit address bits of the device. + */ + MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits); + + + /* Update the power mode and DAC value. + * + * @param mode The Power mode to set the device into. + * @param dac_value The 12bit dac register data value. + * @param writeToEeprom True if the config is to be stored + * in eeprom, otherwise false + * + * @returns + * 0 on success, + * non-0 on failure + */ + int write(enum PowerMode mode, int dac_value, bool writeToEeprom); + + + /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. + * + * @param mode Pointer to variable to store the current power mode. + * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. + * @param dac_value Pointer to variable to store the current dac value. + * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. + * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. + * + * @returns + * 0 on success, + * non-0 on failure + */ + int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress); + + protected: + /** mbed I2C interface driver. */ + I2C _i2c_interface; + /** The full i2c device address. */ + int _device_address; + +}; + +#endif \ No newline at end of file
--- a/mcp4725.cpp Tue Apr 01 04:45:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -#include "mcp4725.h" -#include "mbed.h" - -MCP4725::MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits): _i2c_interface(sda, scl) -{ - //Set the frequency - int freq=0; - switch(bus_frequency) - { - case Standard100kHz: - freq = 100000; - break; - case Fast400kHz: - freq = 400000; - break; - case HighSpeed3_4Mhz: - freq = 3400000; - break; - } - _i2c_interface.frequency(freq); - - // Assemble the full I2C device address. - _device_address = 0xC4; // Prime the full device address with the device code. - //_device_address |= (device_address_bits<<1); -} - -int MCP4725::read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress) -{ - char data[5]; - int result; - - // Read the raw data from the device. - result = _i2c_interface.read(_device_address, data, sizeof(data)/sizeof(*data), false); - - // Parse the raw data, extracting our fields. Refer to MCP4725 ref manual, section 6.2 - if (result == 0) - { - *eeprom_write_in_progress = (data[0] & 0x80)? false:true; - - *mode = (enum PowerMode) ((data[0] & 0x06)>>1); - - *dac_value = (data[1]<<4) + (data[2]>>4); - - *mode_eeprom = (enum PowerMode)((data[3] & 0x60)>>5); - - *dac_value_eeprom = ((data[3] & 0x0F) <<8) + data[4]; - } - - return result; -} - - - -int MCP4725::write(enum PowerMode mode, int dac_value, bool writeToEeprom) -{ - char data[3]={0}; - int write_command; - - //Which write command are we to use? - if (writeToEeprom == true) - { - //Write DAC Register and EEPROM - write_command = 3; - } - else - { - //Write DAC Register - write_command = 2; - } - - //Assemble our three bytes of data - Refer to MCP4725 ref manual, section 6. - data[0] = (write_command <<5) | ((int)mode<<1); - data[1] = (dac_value>>4); - data[2] = (dac_value<<4); - - // Now write them to the device. - return _i2c_interface.write(_device_address, data, sizeof(data)/sizeof(*data), false); -} \ No newline at end of file
--- a/mcp4725.h Tue Apr 01 04:45:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -#ifndef MCP_4725_H -#define MCP_4725_H - -#include "stdbool.h" -#include "mbed.h" -#include "I2C.h" - - -/** MCP4725 class. - * Used for interfacing with a mcp4725 12-Bit Digital-to-Analog Converter. - * To convert between the 12bit dac_value and Vout, use the following formula: dac_value = (Vout*4096/Vref), where Vout is - * the desired output analog voltage, Vref is the voltage connected to the Vdd pin of the device. Typically Vdd will be 3.3volts. - * - * Note: There is an accompanying test suite program "MCP_4725_Library_Test" that can be used to test this library. - * - */ -class MCP4725 -{ - public: - /** The device supports two types of power modes: normal and power-down. In normal mode the device - * operates a normal digital to analog conversion. In power-down mode all digitial to analog - * conversion is stopped, resulting in the device using less power (typically 60nA). Also, in power - * down mode Vout will be pulled to ground using either a 1k, 100k or 500k ohm internal resistors. */ - enum PowerMode { - /** In normal mode the device operates a normal D2A conversion. */ - Normal=0, - /** Enter the device into a power down mode, and pull Vout to ground using an internal 1k resistor. */ - PowerDown1k=1, - /** Enter the device into a power down mode, and pull Vout to ground using an internal 100k resistor. */ - PowerDown100k=2, - /** Enter the device into a power down mode, and pull Vout to ground using an internal 500k resistor. */ - PowerDown500k=3 - }; - - /** The device supports 3 different I2C bus frequencies.*/ - enum BusFrequency { - /** Standard 100kHz bus. */ - Standard100kHz, - /** Fast 400kHz bus. */ - Fast400kHz, - /** High Speed 3.4Mhz bus. WARNING: the test suite fails for the mbed LPC1768 when this frequency is selected - not tested on other platforms.*/ - HighSpeed3_4Mhz - }; - - /** Create an mcp4725 I2C interface - * - * @param sda I2C data line pin - * @param scl I2C clock line pin - * @param bus_frequency the frequency at which the I2C bus is running. - * @param device_address_bits The 3bit address bits of the device. - */ - MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits); - - - /* Update the power mode and DAC value. - * - * @param mode The Power mode to set the device into. - * @param dac_value The 12bit dac register data value. - * @param writeToEeprom True if the config is to be stored - * in eeprom, otherwise false - * - * @returns - * 0 on success, - * non-0 on failure - */ - int write(enum PowerMode mode, int dac_value, bool writeToEeprom); - - - /** Read the contents of the dac register, the contents of eeprom, and if an eeprom write is currently active. - * - * @param mode Pointer to variable to store the current power mode. - * @param mode_eeprom Pointer to variable to store the power mode as is stored in eeprom. - * @param dac_value Pointer to variable to store the current dac value. - * @param dac_value_eeprom Pointer to variable to store the dac value as is stored in eeprom. - * @param eeprom_write_in_progress Pointer to variable to store the current eeprom write status. - * - * @returns - * 0 on success, - * non-0 on failure - */ - int read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress); - - protected: - /** mbed I2C interface driver. */ - I2C _i2c_interface; - /** The full i2c device address. */ - int _device_address; - -}; - -#endif \ No newline at end of file