Search code examples
cmicrocontroller

What's wrong with this function? not assignable?


float tempC(unsigned int adc_value) {
    multiplier = adc_value / 1023.0f;
    tempC = -40 * multiplier * 90;
    return tempC;
}

I am trying to use the ADC on a micro-controller to convert a potentiometer into temperature between -40 and 50 degrees C, the adc_value is the range given by the ADC however I get the error:

Main.c:110:11: error: non-object type 'float (unsigned int)' is not assignable

I can provide more code if needed but I don't know where I'm going wrong as I am quite new to C and programming.


Solution

  • tempC is not a variable but the function, so you cannot assign there.

    You should declare another variable instead of that like this:

    float tempC(unsigned int adc_value) {
        float tempC_ret;
        multiplier = adc_value / 1023.0f;
        tempC_ret = -40 * multiplier * 90;
        return tempC_ret;
    }
    

    Or you can return the calculated value directly like this:

    float tempC(unsigned int adc_value) {
        multiplier = adc_value / 1023.0f;
        return -40 * multiplier * 90;
    }