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.
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();
}
"0b00000000"
the the like: you are treating an binary integer as a string. \$\endgroup\$dec
is an int, it is already binary, so no conversion is required - although you may want to check that it is less than 256, and set it to 255 if not. Also, you declaredbits
as a char array, then tried to assign it to a char. \$\endgroup\$