This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

writing decimal numbers to LCD - msp430f2112

Hi all,
I have acquired data from ADC and want to write it to LCD. When I write ADC10MEM data into LCD it writes 1022 corresponding to 5V. Here, I want to write it as 5V on LCD, so I need to divide it by 1022 and multiply by 5. However there are two problems.
1. It does not give exact solution at 3V or 2V, it just shows "0" on LCD. Is it because the numbers are rounded to "0" when divided by 1022?
2. I want to show up to 2 decimals as "2.34V". How could I prevent rounding and write as "x.xx V"?
Thanks in advance.

  • Well, 1022/1022*5 is 5. But 1021/1022*5 is 0. because everything smaller 1022 divided by 1022 is smaller than 1 and therefore 0.

    You may first multiply an dhten divide. Then you'll at least get 0,1,2,3,4 or 5 as result. Hoever, no fractional results still.

    You may multiply with 50 first, then divide. It will give yo 50 for 5.0V, 49 for 4.9V etc.

    For two decimals you'd have to multiply with 500, but then the result would be >65535. Or you multiply with 50 and divide by 102 (well, 0.2% error due to the missing 0.2). It gives 500 (almost 501) for 5.00V etc.

    Converting the 500 into "5.00V" is another thing. You could, of course, float numbers. Then the above mult/div stuff isn't necessary at all. However, floats are big and slow. They add much code overhead. Also, the float support for (S)printf is even bigger and slower.

    But you can implement something straight:
    If 'voltage' is your result (0..500):

    int x = voltage / 100;
    voltage -=100*x;
    int y = voltage/10;
    voltage -= 10*y;
    printf ("%d.%d%dV", x,y,voltage); // or any other means to assemble and output the desired text

    It is plain integer and quite small and fast, even though it has two integer divisions. There are slightly larger but even faster implementations. There are threads about outputting an integer value without printf.

  • I have also found this function (modf) which seems to be a simpler way in C however it keeps giving memory range errors while compiling.
    http://www.cplusplus.com/reference/clibrary/cmath/modf/

    float p= (ADC_Result % 100);
    double fract = modf (p , &int);
    int z = floor (int);
    int y = floor (fract*100);

    IF ADC10MEM gives 514 for instance (about 2.5V) , p = 5.14, with modf function, it is separated into 5.0000 and 0.140000. Then, I convert them into integers by rounding down with floor function. The compiler keeps on giving a memory range overflow error. Is it a wrong function to use considering float and integers?

  • Levent Erb��k said:
    I have also found this function (modf) which seems to be a simpler way in C

    Simple in usage. However, not simple in means of active code.
    It requires the linekr to add float and double support, float division funciton, modf function, floor function and double multiplication to your few lines of application code.

    Levent Erb��k said:
    however it keeps giving memory range errors while compiling.

    While compiling or while linking? All this float and double math stuff is likely much larger than teh available flash size on your MSP. Hence the error.

    What you do here is buying a child a MAC with calculator program, instead of teaching it to do a simple addition with pencil and paper. Sure it is faster and more convenient - if you have the MAC at hand. :)

**Attention** This is a public forum