MCP4725

Dependents:   Cube_Mini_Template

Committer:
BoulusAJ
Date:
Fri Jan 31 17:59:17 2020 +0000
Revision:
6:0e58d3068eb8
Parent:
5:3e6ffce1eea2
Child:
7:5cfc0f623d5b
Latest changes.

Who changed what in which revision?

UserRevisionLine numberNew 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
BoulusAJ 6:0e58d3068eb8 54 int MCP4725::write(float dac_value, enum PowerMode mode, bool writeToEeprom)
donalm 0:3214e3bbf25c 55 {
BoulusAJ 6:0e58d3068eb8 56
BoulusAJ 6:0e58d3068eb8 57
BoulusAJ 6:0e58d3068eb8 58 int dac_value_2 = (int) (0xFFF * (dac_value/4.94f) );
donalm 0:3214e3bbf25c 59 char data[3]={0};
donalm 0:3214e3bbf25c 60 int write_command;
donalm 0:3214e3bbf25c 61
donalm 0:3214e3bbf25c 62 //Which write command are we to use?
donalm 0:3214e3bbf25c 63 if (writeToEeprom == true)
donalm 0:3214e3bbf25c 64 {
donalm 0:3214e3bbf25c 65 //Write DAC Register and EEPROM
donalm 0:3214e3bbf25c 66 write_command = 3;
donalm 0:3214e3bbf25c 67 }
donalm 0:3214e3bbf25c 68 else
donalm 0:3214e3bbf25c 69 {
donalm 0:3214e3bbf25c 70 //Write DAC Register
donalm 0:3214e3bbf25c 71 write_command = 2;
donalm 0:3214e3bbf25c 72 }
donalm 0:3214e3bbf25c 73
donalm 0:3214e3bbf25c 74 //Assemble our three bytes of data - Refer to MCP4725 ref manual, section 6.
donalm 0:3214e3bbf25c 75 data[0] = (write_command <<5) | ((int)mode<<1);
BoulusAJ 6:0e58d3068eb8 76 data[1] = (dac_value_2>>4);
BoulusAJ 6:0e58d3068eb8 77 data[2] = (dac_value_2<<4);
donalm 0:3214e3bbf25c 78
donalm 0:3214e3bbf25c 79 // Now write them to the device.
donalm 5:3e6ffce1eea2 80 return _i2c_interface.write(_device_address, data, sizeof(data)/sizeof(*data), false);
donalm 0:3214e3bbf25c 81 }