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

Discussion in 'Embedded Systems and Microcontrollers' started by Jswale, Aug 5, 2015.

  1. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    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;

    Code (C):
    1. unsigned int ADC_result;
    2. float ADC_result_new;
    3. float Power_result_new;
    4. float Power_result;
    5. unsigned char temp[6];
    6. unsigned char temp_p[6];
    7.  
    8.  
    9. void main (void)
    10. {
    11. while(1)
    12.     {  
    13.      
    14.         ADCON0bits.GO=1;                                    //Sets Go_Done bit
    15.         while(ADCON0bits.GO==1);                            //Waits for conversion to finish
    16.         __delay_us(10);
    17.         ADC_result=ADRESL+(ADRESH*256);                        //gets 10 bit result
    18.  
    19.         //ADC_result=0xAF;        //Test purposes COMMENT OUT BEFORE BUILD.
    20.  
    21.         Power_result=(ADC_result*(23/1023));                    //*(5/1023))/5)*current)*230)/1000);     changes ADC value to power in Watt
    22.  
    23.         lcd_cmd(0x80);                                        //Start of Line 1
    24.         lcd_WriteStr("Input V = ");
    25.         ADC_result_new =((ADC_result/1023)*5);                //changes digital to analog
    26.  
    27.         //At this point both ADC/Power are ready to be displayed, both floats.
    28.  
    29.         sprintf(temp, "%4.2f", ADC_result_new);              
    30.         lcd_WriteStr(temp);                                    //writes the value (0-5.0V)
    31.  
    32.         lcd_cmd(0xC0);                                        //Starts Line 2
    33.         lcd_WriteStr("Power W = ");
    34.         sprintf(temp_p, "%4.2f", Power_result);             //Turns float into a ASCII char that can be displayed
    35.         lcd_WriteStr(temp_p);                                 //write it
    36.  
    37.     }//while
    38. }    //main
    39.  
    40.  
    41. Cheers
    42. JSwale
    43.  
    44.  
     
  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,810
    834
    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 (Text):
    1. ADC_result_new=(float(ADC_result)*5)/1023)
     
  3. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    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!
     
Loading...