Jump to content

scientific notation

Recommended Posts

Hi everyone! im trying to make a vacuum sensor with msp430 and a lcd, the sensor gives me an output from 0 to 10v, i have done a voltage divider for get 3.3v, but i have to print the mbars in the lcd with exponential notation( like 1.04e-3), i have tried with de arduino glcd(doesnt work), and i tried the tiny printf() library but it doesnt have the exponential notation.


does someone knows how to print this?

Link to post
Share on other sites

Since you're going to read a digital value, start considering this. I assume the ADC value is 16 bits (probably better than you're going to get).

Now, since your sensor current is exponential with the pressure you're going to do some maths first. This should be done in integer if at all possible. But in this case, floating point is very attractive. Consider that floating point calculations are very big and slow. The MSP does not have an integer multiplier, so floating point logarithms are going to be huge.

Since we must convert it back to integer values to do sensefull printing, we're going to determine the range successively and then do a conversion to integer. I'll use plain math operations here, which is discouraged in most cases because of the heavy load.

unsigned short current = getADC(); // any form of input
float pressure = current2pressure(current * current_factor); // current_factor maps the ADC (voltage) value to the actual current, alternatively integrate this in your conversion function.
// Now pressure is in the range of 1e-7 and 1e3 for example (for simplicity I assume pressure will never exceed 1000mbar)
signed char exponent = (signed char)log10(pressure);
unsigned short pressure_mantissa = (unsigned short)(pressure * exponent * 100); // pressure_mantissa is in the range [100..1000)
char result[8]; // remember the string terminator
result[0] = (pressure_mantissa / 100) + '0'; // integer arithmetic (999/100 = 9)
result[1] = '.';
result[2] = (pressure_mantissa / 10) % 10 + '0';
result[3] = (pressure_mantissa) % 10 + '0';
result[4] = 'e';
if (exponent >= 0)
  result[5] = exponent + '0';
  result[6] = 0;
  result[5] = '-';
  result[6] = '0' - exponent; // '0' - -1 == '0' + 1
result[7] = 0;
Link to post
Share on other sites

this is a little confusing :-(


i have make the math formula and it transforms the voltage in mbars and it shows the mbars in the lcd, but it displays it like float:


volts=(analogRead(A0)*102.4); // transforms the reading in voltage
vacuum=pow(10, 1.667*volts-11.33); // transforms the voltage in mbars
i kind of understand the code, but i cant imagine how to implement in my code D:
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...