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.

Msp430/floating numbers/LCD

Hello guys,

 

This question might be better suited for a beginner C forum but i think you guys can help :).

 

What I am trying to do is extract the BCD of a decimal value. For example; I'm using the ADC12 on a MSP 2xx series MCU to represent 12v of a battery.(voltage indicator).

So, I'll have 4095 points measuring 12v, or .003/volt.

Now, here is the code for outputting the integer format of the battery voltage:

 

void LCD_output_result(unsigned char cursorpos, float data)          //data is the contents of ADC12MEM0 x slope(12/4095)
{
    int tmp_result, decimal1;
        float decimal;
    unsigned char bcd0,bcd1,bcd2,bcd3;

    tmp_result = (int)data;
    bcd0 = (tmp_result / 1000) + 48;    // divide by 1000 and add 48 for ASCI
    tmp_result = tmp_result % 1000;
    bcd1 = (tmp_result / 100) + 48;        // divide by 100 and add 48 for ASCI
    tmp_result = tmp_result % 100;
    bcd2 = (tmp_result / 10) + 48;        // divide by 10 and add 48 for ASCI
    bcd3 = (tmp_result % 10) +48;
    LCD_cursor(cursorpos);
    if (!bcd0)
    {
        LCD_char(0x20);
        LCD_char(0x20);
    }
    else
    {
        LCD_data(bcd0);
    }
    LCD_data(bcd1);
    LCD_data(bcd2);
    LCD_data(bcd3);

 

So as you can see a series of modulus and division operators are used to get the bcd values of the voltage in order to send each character to the LCD_Data function. My question has to do with trying to extract the .x value from that voltage. Say my voltage is 9.5v. I'll put my code and logic down here:

 

        decimal =fmod(data,1);      // i use Fmod operator since normal modulus doesn't work with floats    example (9.504 % 1 = .504
        decimal = decimal * 1000.0;   // .504 * 1000 = 504
        decimal = decimal / 100.0;     // 504 / 100 = 5.0
        decimal1 = (int)decimal;  //  5.0> 5
        decimal1 = decimal1+48; // add 48 for ascii
        LCD_data(decimal1); //send digit to LCD_Data function

 

Is my logic/code correct here? Something doesn't seem to be working as my digit after the decimal doesn't change when changing my input to the ADC pin. Any tips/suggestions are appreciated!

 

Thanks,

 

 

  • nevermind i think i found my error

     

    decimal =fmod(data,1);   i should have had 1.0 instead of 1

  • This is probably not the answer you were looking for but have you considered ditching the floats?  If you actually used 1bit ADC = 0.003V (therefore max input 4095bits = 12.285V) then you could say Input voltage (mV) = ADC12MEM * 3 instead of using the " * 12/4095".

    Then you just need to use an extra step at the start of the integer conversion stage as the max expected value would be ~12000.

    I reckon you'd see an enormous efficiency improvement...

**Attention** This is a public forum