Jump to content
Sign in to follow this  
Roger

dtostrf() function works in arduino but not Energia

Recommended Posts

I need to convert a float value to a string to send it over an rf link. The below code works on my Arduino but not on my MPS430 2553. Is there a way to copy this function in to Energia?

// convert the float value to a string to send out RF
   char pbuff[8]= " ";
   String spres = dtostrf (bpres,1,2,pbuff);

Share this post


Link to post
Share on other sites

I think it is likely to do with mspgcc's double/float support. dtostrf is part of stdlib.h but not actually located in libc

Note that these functions are not located in the default library, libc.a, but in the mathematical library, libm.a. So when linking the application, the -lm option needs to be specified.

I think energia links with libm during linking. You could track down where this function exists...

 

A quick fix would be to use a more common function. something like sprintf. (May use more FLASH, but so does using floats on 16 bit microcontrollers)

char pbuff[8]= " ";
   
sprintf(pbuff,"%1.2f",bpres);
   
String spres = pbuff;
Serial.print(spres);

sprintf will create a C string output, this can be conveted to a String type with assignment to match your original code.

Share this post


Link to post
Share on other sites

I think it is likely to do with mspgcc's double/float support. dtostrf is part of stdlib.h but not actually located in libc

Note that these functions are not located in the default library, libc.a, but in the mathematical library, libm.a. So when linking the application, the -lm option needs to be specified.

 

That quote appears to come from avr-libc documentation (it may have come from an mspgcc-oriented site, but if so, it was either written more than five years ago or is simply wrong). mspgcc uses a completely different libc, and dtostrf is not in it.

 

A quick fix would be to use a more common function. something like sprintf.

%f formatting is not supported by mspgcc, so unless Energia's transitioned to msp430-elf that won't work either.

 

Using floating point on the MSP430 will simply cause pain. Get the value using a fixed point calculation, and format it in whatever way you need. "%u.%03u" would work if you split an integral milli-value into v/1000 and v%1000 components.

Share this post


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.

Guest
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.

Sign in to follow this  

×
×
  • Create New...