This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Multiplication Help Wanted!!

Dear Keil Community,

I have built a digital thermomter using AT89C52. I have to display the temp on a LCD. The ADC is 10 bits and after coversion routines, i get the hex equivalent of temperature as 368. Now 368 is to be diplayed as 3.406 on LCD. .406 is got as follows:
6/16 = 0.375
8/256= 0.031
Adding 0.375 + 0.031, we get 0.406

Please guide me as to how to do this conversion in C51.
Thanks for all the Help.

  • Hi,

    Why not do exactly as you have described the problem? by creating a float variable to store the result.

    Say something like:-

    unsigned short rawTemp; /* 10bit ADC value */
    float actualTemp;
    
    actualTemp = (rawTemp & 0xf00) >> 8; /* scale the units */
    actualTemp += ((rawTemp & 0xf0) >> 4)/16 /* scale the 1/16's */
    actualTemp += (rawTemp & 0x0f)/256; /* scale the 1/256's */
    

    Note you may for have to cast rawTemp to a float before doing the divides and now possibly 'sprintf' the value to a string ready to format for lcd display.

    Hope this helps,
    Mark.

  • unsigned short rawTemp; /* 10bit ADC value */
    float actualTemp;
    
    actualTemp = (rawTemp & 0xf00) >> 8; /* scale the units */
    actualTemp += ((rawTemp & 0xf0) >> 4)/16 /* scale the 1/16's */
    actualTemp += (rawTemp & 0x0f)/256; /* scale the 1/256's */

    Or simply:
    unsigned short rawTemp; /* 10bit ADC value */
    float actualTemp;
    
    actualTemp = rawTemp / 256.0;
    

  • You've been shown the straight-forward floating-point method. Let me show you the somewhat crude fixed-point alternative:

    fraction = ((unsigned long) (dac_val & 0xff)) * 1000 + 128) / 256

    For a dac_val of 0x368, this gives decimal 406, i.e. exactly the fractional part of the number you want to print.

    The trick is to multiply *first*, before you
    divide by 256, and to add the offset of 0.5 in the last digit.

  • Let me show you the somewhat crude fixed-point alternative:
    Hans-Bernhard, did you forget

    the much fasterfixed-point alternative

    Erik