Hi All,
I am using MCU MSP430F5529 for my current project,I am getting some difficulty in it.Please find below the problems.
In our project we want to convert character/int value to string, for this we are using sprint() inbuilt function
Provided by compiler. If the character/int value is less than 10 then we want to append ‘0’
For e.g. if value is 9 we want in string as “09”. To achieve this we are using the following format
Sprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs);
But this is not working we are getting the same content in Buffer as mentioned in format specifier.
Is there any setting in CCS IDE to achieve this.
Please help me out of this asap.
The default for (s)printf is just basic support. That means, the format string is jsut printed. It doesn't make much sens for sprintf, but is meant for simple debug text output throught he debug console without need to write your own outptu funciton or have all the overhead of a full printf in your binary.
To enable interpretation of the format string, you must change the project stettings. there mey be different levels of priintf support. Keep in mind that the code behind full printf support is huge, as printf is a very complex function.
_____________________________________Before posting bug reports or ask for help, do at least quick scan over this article. It applies to any kind of problem reporting. On any forum. And/or look here.If you cannot discuss your problem in the public, feel free to start a private conversation: click on my name and then 'start conversation'. But please do so only if you really cannot do it in a public thread, as I usually read all threads. And I prefer to answer where others can profit from it (or contribute to it) too.
Sprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs); is missing a ".
elturySprint (Buffer, “%02d%02d%04d%02d%02d%02d, Day,Month,Year,Hour,Min,Secs); is missing a ".
utpal kumar... I am using MCU MSP430F5529 for my current project,I am getting some difficulty in it... Is there any setting in CCS IDE to achieve this...
Your problem is not related to the MSP430F5529. It is caused by CCS and the way you use it (or, abuse it).
hi jens,
thanks for reply.
i will try it ,and come back with feedback soon.
Hi jens,
Missing " in the sprintf function was my typing mistake but in the code it was there. You have mentioned as sprint instead of
sprintf, this also we tried but giving error as TI doesn’t have sprint library function.
We have used syntax as follows in code:
sprintf (Buffer, “%02d%02d%04d%02d%02d%02d”, Day,Month,Year,Hour,Min,Secs);
So please give the solution.
hi ocy,
utpal kumarsprintf (Buffer, “%02d%02d%04d%02d%02d%02d”, Day,Month,Year,Hour,Min,Secs);
However, you'll have to change the default project settings (not your code) so the linker will include the printf/sprintf code with full funcitonality. The default will just discard all parameters and only print the format string as-is.
The reason is that the full functionality code is some kb size and won't even fit into the smaller MSPs. It also requires quite some stack space, also not available on some of the smaller MSPs.
utpal kumarYou have mentioned as sprint instead of sprintf
Jens-Michael GrossThe reason is that the full functionality code is some kb size and won't even fit into the smaller MSPs. It also requires quite some stack space, also not available on some of the smaller MSPs.
Not sure how sprintf is implemented on CCS but some implementations actually use the Heap, so you'll have to deal with dynamic allocation also...
The best way IMHO is to use something like itoa() which unfortunately is not available on CCS or IAR, but it's much more compact than sprintf.
Tony
utpal kumarSo please give the solution.
Version 4.0.0 has the following bug:
------------------------------------------------------------------------------FIXED SDSCM00042077------------------------------------------------------------------------------Summary : Printf only prints first character of argument correctlyFixed in : 4.0.1Severity : S2 - MajorAffected Component : Runtime Support Libraries (RTS)Description: Printf only prints first character of argument correctly.For example, printf("%d\n", 123 ) is printed as 1cÿ
If you only want to convert a small integer (0 to 99) to two ASCII characters, you can simply do:
first_char = small_int / 10 +'0';
second_char = small_int % 10 + '0';
old_cow_yellow If you only want to convert a small integer (0 to 99) to two ASCII characters, you can simply do: first_char = small_int / 10 +'0'; second_char = small_int % 10 + '0';
The library fixed point division and modulo add up to almost 100 bytes; in the same space you can implement a more general solution for converting integers to ASCII:
#include <stdint.h> // div & mod through series decomposition class Modulo { public: uint16_t quotient; uint16_t remainder; //num = number to be divided, d = divisor Modulo(uint16_t num, uint16_t d) { quotient = 0; remainder = num; do { num -= d; if(num > remainder) break; remainder = num; quotient ++; } while(num); } private: Modulo(); }; char* utoa(uint16_t num, char* s) { int count = 0; char buffer[8]; do { Modulo result = Modulo(num, 10); buffer[count] = '0' + result.remainder; count++; num = result.quotient; } while(num); int i; for(count--,i=0; i < count; i++) { s[i] = buffer[count - i]; } // null terminate string s[i] = '\0'; return s; } int main(void) { char temp[8]; utoa(16384, temp); // temp is {'1','6','3','8','4','\0'} return 0; }
The whole thing including main() compiles to about 100 bytes (on IAR). I'm sure it can be further optimized since I didn't spend much time tweaking.
For an even more general solution, you can "templatize" the factory class, then use specialization in each case of signed/unsigned/int/long/etc. You'd have to specifically check for signs in signed specializations but should be fairly trivial.
TonyKaoThe library fixed point division and modulo add up to almost 100 bytes;
Your implementation is nice and is more or less a class-bound, recursive implementazion of my proposals/sample code made earlier in this board.
Jens-Michael Gross Your implementation is nice and is more or less a class-bound, recursive implementazion of my proposals/sample code made earlier in this board.
I almost squealed unseemly like a schoolgirl. Thanks Jens-Michael :)
And the implementation is not quite recursive, since there's only once instance of the factory class at any given time; only the computed results are "recursed". The C/C++ language is not meant to be (shudders) functional after all. Although funnily enough, the C++ template semantics can be metaprogrammed functionally, and is actually Turing-complete...
The Modulo constructor might result in a very large number of repeated subtractions. Worst case for a number of 65535, there would be 6553+655+65+6 = 7279. Here's my implementation of an algorithm purposed by Jens-Michael Gross. It uses repeated subraction of 10^n bases. It also creates the digits in order. Plain C. Not as visually clean are your C++ code.
/*-----------------------------------------------------------------------------Lookup table for units for each position. Order is from largest to smallest.Example below assumes "int" is 16 bits. Last value must always be one.-----------------------------------------------------------------------------*/static const unsigned int g_units[]={ 100000, /* 10^5 */ 10000, /* 10^4 */ 1000, /* 10^3 */ 100, /* 10^2 */ 10, /* 10^1 */ 1 /* 10^0 */};/*-----------------------------------------------------------------------------Converts signed integer to base 10 string. Caller must allocate enoughspace for result. Returns pointer to beginning of string. Same as passed in.Assuming 16 bit signed integers and value is 29999, there will be 38subractions.-----------------------------------------------------------------------------*/char *itod(int v, char *s){ register char *p = s; register unsigned int vabs; register unsigned int unit; register int n; register int i; /* Handle 0 as special case. Code below assumes a non-zero value. */ if(v == 0) { *p++ = '0'; *p++ = '\0'; return(s); } /* Make the value absolute and emit a '-' is required. */ if(v < 0) { *p++ = '-'; vabs = -v; } else vabs = v; /* Find first unit */ i = 0; for(;;) { unit = g_units[i]; if(vabs > unit) break; i++; }; /* Loop through each unit. */ for(;;) { unit = g_units[i]; if(unit==1) break; /* Subtract units until no more of this unit. */ n = 0; while(vabs > unit) { vabs -= unit; n++; } *p++ = (char)('0' + n); /* Emit character. */ i++; /* Advance index to next unit. */ } /* Last unit is always one. Special case. Finish up. */ *p++ = (char)('0' + vabs); /* Emit last character. */ *p++ = '\0'; /* Null terminate.*/ return(s);}
I don't have a MSP430 platform. Can't say how much code space would be used.