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