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.

CCS/LAUNCHXL-F28379D: Convert a Float number to String.

Part Number: LAUNCHXL-F28379D
Other Parts Discussed in Thread: TEST2

Tool/software: Code Composer Studio

I have been trying to come up with a good way to convert a floating point number that will be returned from the ADC to a char* array that can be printed to an external LCD. 

I was told by another user to try and get a function working that would replace char with int16_t because of the sizeof char on the board. I have found a function that works with int16_t. However, I can't seem to get it to work correctly with the microcontroller. 

//Assumes bytes* is at least 2-bytes long
void floatToBytes(int16_t* bytes, float flt)
{
  bytes[1] = (int16_t) flt;      //truncate whole numbers
  flt += 0.005;                 // adds .005 to the number to get two decimal place precision
  flt = (flt - bytes[1])*100;   //remove whole part of flt and shift 2 places over
  bytes[0] = (int16_t) flt;      //truncate the fractional part from the new "whole" part
}
//Example: 144.2345 -> bytes[1] = 144; -> bytes[0] = 23

I am testing the code on my computers C compiler

int16_t str2 [2];

float num = 3004.685436995;
char dot [3] = ".";

floatToBytes(str2, num);

printf("%d", str2[1]);
printf("%s", dot);
printf("%d", str2[0]);

Is there a good way to do this with a library built into the board? I have tried many different methods for converting but most assume a char is 8 not 16. Thanks for any help you are able to offer. 

  • Johnathon,

    Yes, as you have observed, the char on C28x is 16-bits..  There are some compiler intrinisics that can be used to manipulate 8-bit bytes that I think will help you.  

    If my answer has resolved your issue, please click Answer Verified.

    Thank you

    Lori

  • Hi Johnathon,

    I haven’t heard from you for about a week, so I’m assuming you were able to resolve your issue. If this isn’t the case, please click the "This did NOT resolve my issue" button and reply to this thread with more information. If this thread locks, please click the "Ask a related question" button and in the new thread describe the current status of your issue and any additional details you may have to assist us in helping to solve your issues.

    Regards,
    Lori
  • I have created a function that will convert from a float to char* in most cases. However, in some cases, the char* array will put a 0 in the first place instead of the desired number.  I have had a very difficult time debugging the issue because I get an error 

    Description Resource Path Location Type
    <a href="processors.wiki.ti.com/.../10099"> program will not fit into available memory. placement with alignment/blocking fails for section ".text" size 0x15e2 page 0. Available memory ranges: 2837xD_RAM_lnk_cpu1.cmd /LCD_Test2 line 70 C/C++ Problem

    So I try and use flash instead and the program will work.

    However, debugging with the limited hardware debugging is making it difficult to find the problem by stepping through the code. 

    I am confused as to how the project is taking up all of the RAM it is only 700 lines of code with no large include libraries.

  • Johnathon,

    Take a look at the generated .map file. This file will indicate which libraries are getting pulled in and may provide some info on why the code has grown so large.

    There is a lot of RAM on this device so one option is to combine RAM blocks in the linker command file as described here:

    processors.wiki.ti.com/.../C28x_Compiler_-_Understanding_Linking

    I understand your frustration with debug breakpoint limitations. The most recent device, F28004x, has additional hardware breakpoint and debug resources. Unfortunately the device you are using does not have this improvement.

    I hope this helps. If it resolves your question, please go ahead and press the "verified answer" button. Thank you!

    Lori
  • I have been messing with the linker file and was wondering if there is any way to use both RAM locations in page 0 and page 1?

    I have tried
    .text : >>RAMM0 | RAMD0 | RAMLS0 | RAMLS1 | RAMLS2 | RAMLS3 | RAMLS4, PAGE = 0
    .text : >>RAMGS0 | RAMGS1 | RAMGS2 | RAMGS3 | RAMGS4 | RAMGS5 | RAMGS6 | RAMGS7 | RAMGS8 | RAMGS9 | RAMGS10 | RAMGS11 | RAMGS12 | RAMGS13 | RAMGS14 | RAMGS15, PAGE = 1

    Which does not appear to work. If I put the page 1 Ram locations on top it will use those locations and not have to use flash and make it very difficult to debug the code. However, it's odd and is not going to breakpoints I am putting in the code. Thanks for all the help I am slowly getting better with this device every day.
  • RAM is unified. This means page 0 and page 1 are the same - if you write to the RAM on page 0 and then to the same RAM on page 1 you will overwrite the content. The RAM region (specific address range) should only be defined on page 0 or page 1 in the linker command file. Not both.
  • I looked back through the posts and see you are using printf. printf is extremely memory hungry. Here are some tips for using printf that may help.

    processors.wiki.ti.com/.../Printf_support_in_compiler
  • I appreciate all the feedback. I am not using printf in any of my code to try and avoid the memory problems it entails. I was just testing the code on my laptop compiler due to the debugging constraints the board is giving. I have managed to get the float to string working correctly. The problem is that I was not making my char* arrays large enough and they were overwriting each others values. I have remedied that problem and I am now having another. I am trying to set up a 3 digit precision on the number given so that if the float is 123.4566787 it will only print out 123 or if the number is 50.234565 it would show 50.2. I attempted to do this with the float number itself and was getting very strange behaviors. I think I will attempt to just trim the strings based on the number of digits and see how that performs.
  • Thank you for the feedback. Since your original question seems to be resolved, I will go ahead and close this thread. If that is not the case, press the "this did not resolve my issue" button.

    If you have a new question in the future please start a new thread.

    Best Regards,
    Lori
  • I was able to get it working correctly by using sprintf to put the char* into a buffer and modify it there.