Controlling 8-bit DAC Output over I2C— decimal to binary char conversion help

cdacpic

Clarifying my question a bit. We're using a PIC32 board with an MCP4706 8-bit DAC, controlled over I2C. If we write a binary value (e.g. 0b00000100) to the DAC, it outputs as expected. However, we're trying to incrementally change the voltage output by the DAC, which goes into a voltage follower controlling a MOSFET that allows current to flow from a high-current source into a battery.

Here's a schematic of all that. It's not perfect, but it gets out point across.

enter image description here

An analog pin measures the value output by a current sensor in-line with the battery, which controls the current flow, essentially. Currently, our conversion between voltage and current is done arbitrarily, which is okay. I'm looking to get steadily increasing values from the DAC, but I'm reading 0.00V at all time from the output unless I manually set the DAC value. I'm adding some different code here:

// Control voltage sent to DAC as a function of Cvolt read from current sensor
void CurrentControl()
{
    double current;
    // Gets values for Cvolt and Bvolt
    getAnalog();
    // ARBITRARY CONVERSION, NEED TO CHANGE
    current = Cvolt*.0035;

    // Current should be between 8A and 9A at all times for safety
    if(current <= 8.0)
    {
        // if current is less than 8A, increase DAC value
        shift = shift + 1;

        // safety control; keep shift at 255 (max) if it tries to go higher
        if(shift > 255)
            shift = 255;
        // write value to DAC Vout register
        SendI2C3(DAC,0b00000000,shift);
    }
    else if(current >= 9.0)
    {
        // if current is more than 9A, decrease DAC value
        shift = shift - 1;

        // safety control; keep shift at 0 if it tries to go lower
        if( shift < 0)
            shift = 0;
        // write value to DAC Vout register
        SendI2C3(DAC,0b00000000,shift);
    }
}

.

.
.

// Send data to I2C line at given address
void SendI2C3(char addrs,char regis, char data)
{
    char ack;
    I2C_start();
    ack=I2C_write(addrs); //Address for LED is 0x50
    ack=I2C_write(regis); //0xFE for LED
    ack=I2C_write(data);  //0x20to0x7F standard
    I2C_stop();
}

Best Answer

Since "dec" is an int, it is already a binary value. You just need to ensure it is between 0 and 255 inclusive, and send it to the DAC - no need for any decimal to binary conversion (and your messing about with character arrays and strings is no use at all).

It might be useful to see how you set "dec" to the desired value...

Related Topic