How to convert a voltage reading back to twos compliment

i have an ADC i am using and i want to calibrate it

reading the adc i use this function to get me the voltage reading

    double      toVoltage           (uint32_t val, double referenceVoltage)
    {
        return (double)(val*referenceVoltage/8388607);
    }

now with the voltage reading i am able to calculate the offset and let us say that offset value is 0.0007 volts

how do i convert this back to 16 bits twos compliment that my adc calibration registers accept?

which ADC ?


Mathematically, if
volt = val * referenceVoltage / 8388607
then
val = volt * 8388607 / referenceVoltage

(you'll need to play with precision, possible interval etc... to get to 16 bit)

1 Like

Not sure that I understand your question, but isn't it just to swap "*" and "/":

uint32_t      fromVoltage           (double val, double referenceVoltage)
{
        return (uint32_t)(val/referenceVoltage*8388607);
}

the register is 16bits will truncating the 16 most significant bit be okay?

the register is 16bits, val is 32bits if i truncate it wont i loose the sign bit?

Would this be correct>? This is my idea on how to preserve the sign. But there might be better ways like utilizing int16_t maybe instead of uint16_t

return (uint16_t) ( (val/referenceVoltage*8388607) & ( (val & 0x8000) >> 16) )

How about answering that question?
How about showing a complete code that compiles so we can test?

At the moment the maths would deliver a double.

gives us more info about the ADC. if you can have negative values, using an uint32_t won't get you very far...

Your A/D must be a 23 bit (?) converter? (i.e. 8,388,607 steps)

what registers are you referring to? Does your A/D have specific registers for any offset?

I once read the Arduino does not do "double" values. What processor are you using?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.