Multiplying/dividing an integer resulting in a float and displaying on LCD problems

Thread Starter

Jswale

Joined Jun 30, 2015
121
I have a 10 bit ADC value and I am trying to multiply/divide it and then display the resulting float. The float displays 0.00 and doesn't change, this has something to do with the calculations. perhaps it is because I am trying to multiply/divide a 10 bit binary number and I need to break it down into a decimal?...But I have declared it as an integer so I would have thought the calculations would be fine and the result would be e.g 7.65 and then that is broken into a char ready to be displayed.

Here is the relevant code;

C:
unsigned int ADC_result;
float ADC_result_new;
float Power_result_new;
float Power_result;
unsigned char temp[6];
unsigned char temp_p[6];


void main (void)
{
while(1)
    {  
      
        ADCON0bits.GO=1;                                    //Sets Go_Done bit
        while(ADCON0bits.GO==1);                            //Waits for conversion to finish
        __delay_us(10);
        ADC_result=ADRESL+(ADRESH*256);                        //gets 10 bit result

        //ADC_result=0xAF;        //Test purposes COMMENT OUT BEFORE BUILD.

        Power_result=(ADC_result*(23/1023));                    //*(5/1023))/5)*current)*230)/1000);     changes ADC value to power in Watt

        lcd_cmd(0x80);                                        //Start of Line 1
        lcd_WriteStr("Input V = ");
        ADC_result_new =((ADC_result/1023)*5);                //changes digital to analog

        //At this point both ADC/Power are ready to be displayed, both floats.

        sprintf(temp, "%4.2f", ADC_result_new);              
        lcd_WriteStr(temp);                                    //writes the value (0-5.0V)

        lcd_cmd(0xC0);                                        //Starts Line 2
        lcd_WriteStr("Power W = ");
        sprintf(temp_p, "%4.2f", Power_result);             //Turns float into a ASCII char that can be displayed
        lcd_WriteStr(temp_p);                                 //write it

    }//while
}    //main


Cheers
JSwale
 

djsfantasi

Joined Apr 11, 2010
9,156
There are several reasons why the result is always zero.

First, you are dividing ADC_RESULT by 1023. The result will always be less than 0. Since all operands are integers, the result will be an integer. Try something like:
Code:
ADC_result_new=(float(ADC_result)*5)/1023)
 

Thread Starter

Jswale

Joined Jun 30, 2015
121
Ahh I assumed that when the calculations are being done internally (although the input, "ADC_result" is an integer) the number will not be stay as an integer and the final result will just be outputted as the operand specified, (ADC_result_new is a float).

Changed ADC_result to a float and all is well!
 
Top