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.