hello
Fork of MCP4725 by
mcp4725.cpp@0:3214e3bbf25c, 2013-10-27 (annotated)
- Committer:
- donalm
- Date:
- Sun Oct 27 13:49:35 2013 +0000
- Revision:
- 0:3214e3bbf25c
- Child:
- 5:3e6ffce1eea2
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donalm | 0:3214e3bbf25c | 1 | #include "mcp4725.h" |
donalm | 0:3214e3bbf25c | 2 | #include "mbed.h" |
donalm | 0:3214e3bbf25c | 3 | |
donalm | 0:3214e3bbf25c | 4 | MCP4725::MCP4725(PinName sda, PinName scl, BusFrequency bus_frequency, int device_address_bits) |
donalm | 0:3214e3bbf25c | 5 | { |
donalm | 0:3214e3bbf25c | 6 | _i2c_interface = new I2C(sda, scl); |
donalm | 0:3214e3bbf25c | 7 | |
donalm | 0:3214e3bbf25c | 8 | //Set the frequency |
donalm | 0:3214e3bbf25c | 9 | int freq=0; |
donalm | 0:3214e3bbf25c | 10 | switch(bus_frequency) |
donalm | 0:3214e3bbf25c | 11 | { |
donalm | 0:3214e3bbf25c | 12 | case Standard100kHz: |
donalm | 0:3214e3bbf25c | 13 | freq = 100000; |
donalm | 0:3214e3bbf25c | 14 | break; |
donalm | 0:3214e3bbf25c | 15 | case Fast400kHz: |
donalm | 0:3214e3bbf25c | 16 | freq = 400000; |
donalm | 0:3214e3bbf25c | 17 | break; |
donalm | 0:3214e3bbf25c | 18 | case HighSpeed3_4Mhz: |
donalm | 0:3214e3bbf25c | 19 | freq = 3400000; |
donalm | 0:3214e3bbf25c | 20 | break; |
donalm | 0:3214e3bbf25c | 21 | } |
donalm | 0:3214e3bbf25c | 22 | _i2c_interface->frequency(freq); |
donalm | 0:3214e3bbf25c | 23 | |
donalm | 0:3214e3bbf25c | 24 | // Assemble the full I2C device address. |
donalm | 0:3214e3bbf25c | 25 | _device_address = 0xC0; // Prime the full device address with the device code. |
donalm | 0:3214e3bbf25c | 26 | _device_address |= (device_address_bits<<1); |
donalm | 0:3214e3bbf25c | 27 | } |
donalm | 0:3214e3bbf25c | 28 | |
donalm | 0:3214e3bbf25c | 29 | int MCP4725::read(enum PowerMode* mode, enum PowerMode* mode_eeprom, int* dac_value, int* dac_value_eeprom, bool* eeprom_write_in_progress) |
donalm | 0:3214e3bbf25c | 30 | { |
donalm | 0:3214e3bbf25c | 31 | char data[5]; |
donalm | 0:3214e3bbf25c | 32 | int result; |
donalm | 0:3214e3bbf25c | 33 | |
donalm | 0:3214e3bbf25c | 34 | // Read the raw data from the device. |
donalm | 0:3214e3bbf25c | 35 | result = _i2c_interface->read(_device_address, data, sizeof(data)/sizeof(*data), false); |
donalm | 0:3214e3bbf25c | 36 | |
donalm | 0:3214e3bbf25c | 37 | // Parse the raw data, extracting our fields. Refer to MCP4725 ref manual, section 6.2 |
donalm | 0:3214e3bbf25c | 38 | if (result == 0) |
donalm | 0:3214e3bbf25c | 39 | { |
donalm | 0:3214e3bbf25c | 40 | *eeprom_write_in_progress = (data[0] & 0x80)? false:true; |
donalm | 0:3214e3bbf25c | 41 | |
donalm | 0:3214e3bbf25c | 42 | *mode = (enum PowerMode) ((data[0] & 0x06)>>1); |
donalm | 0:3214e3bbf25c | 43 | |
donalm | 0:3214e3bbf25c | 44 | *dac_value = (data[1]<<4) + (data[2]>>4); |
donalm | 0:3214e3bbf25c | 45 | |
donalm | 0:3214e3bbf25c | 46 | *mode_eeprom = (enum PowerMode)((data[3] & 0x60)>>5); |
donalm | 0:3214e3bbf25c | 47 | |
donalm | 0:3214e3bbf25c | 48 | *dac_value_eeprom = ((data[3] & 0x0F) <<8) + data[4]; |
donalm | 0:3214e3bbf25c | 49 | } |
donalm | 0:3214e3bbf25c | 50 | |
donalm | 0:3214e3bbf25c | 51 | return result; |
donalm | 0:3214e3bbf25c | 52 | } |
donalm | 0:3214e3bbf25c | 53 | |
donalm | 0:3214e3bbf25c | 54 | |
donalm | 0:3214e3bbf25c | 55 | |
donalm | 0:3214e3bbf25c | 56 | int MCP4725::write(enum PowerMode mode, int dac_value, bool writeToEeprom) |
donalm | 0:3214e3bbf25c | 57 | { |
donalm | 0:3214e3bbf25c | 58 | char data[3]={0}; |
donalm | 0:3214e3bbf25c | 59 | int write_command; |
donalm | 0:3214e3bbf25c | 60 | |
donalm | 0:3214e3bbf25c | 61 | //Which write command are we to use? |
donalm | 0:3214e3bbf25c | 62 | if (writeToEeprom == true) |
donalm | 0:3214e3bbf25c | 63 | { |
donalm | 0:3214e3bbf25c | 64 | //Write DAC Register and EEPROM |
donalm | 0:3214e3bbf25c | 65 | write_command = 3; |
donalm | 0:3214e3bbf25c | 66 | } |
donalm | 0:3214e3bbf25c | 67 | else |
donalm | 0:3214e3bbf25c | 68 | { |
donalm | 0:3214e3bbf25c | 69 | //Write DAC Register |
donalm | 0:3214e3bbf25c | 70 | write_command = 2; |
donalm | 0:3214e3bbf25c | 71 | } |
donalm | 0:3214e3bbf25c | 72 | |
donalm | 0:3214e3bbf25c | 73 | //Assemble our three bytes of data - Refer to MCP4725 ref manual, section 6. |
donalm | 0:3214e3bbf25c | 74 | data[0] = (write_command <<5) | ((int)mode<<1); |
donalm | 0:3214e3bbf25c | 75 | data[1] = (dac_value>>4); |
donalm | 0:3214e3bbf25c | 76 | data[2] = (dac_value<<4); |
donalm | 0:3214e3bbf25c | 77 | |
donalm | 0:3214e3bbf25c | 78 | // Now write them to the device. |
donalm | 0:3214e3bbf25c | 79 | return _i2c_interface->write(_device_address, data, sizeof(data)/sizeof(*data), false); |
donalm | 0:3214e3bbf25c | 80 | } |