printf support for MSP430 micro-controller

你。 提交于 2019-12-12 18:28:27

问题


I am upgrading a fully tested C program for the Texas Instruments (TI) MSP430 micro-controller using a different C compiler, changing from the Quadravox, AQ430 Development Tool to the VisualGDB C compiler.

The program compiles with zero errors and zero warnings with VisualGDB. The interrupt service routines, timers, UART control, and more all seem to be working. Certain uses of sprintf, however, are not working. For example:

unsigned int duration;
float msec;

msec = (float)duration;

msec = msec / 125.0;

sprintf(glb_response,"break: %3.1f msec",msec);

This returns: break: %3.1f msec

This is what is expected: break: 12.5 msec

I have learned about the following (from Wikipedia):

The compiler option --printf_support=[full | minimal | nofloat] allows you to use a smaller, feature limited, variant of printf/sprintf, and make that choice at build time.

The valid values are:

full: Supports all format specifiers. This is the default.

nofloat: Excludes support for printing floating point values. Supports all format specifiers except %f, %g, %G, %e, and %E.

minimal: Supports the printing of integer, char, or string values without width or precision flags. Specifically, only the %%, %d, %o, %c, %s, and %x format specifiers are supported

I need full support for printf. I know the MSP430 on my product will support this, as this C program has been in service for years.

My problem is that I can't figure out 1) if VisualGDB has the means to set printf support to full and 2) if so, where and to set it.

Any and all comments and answers will be appreciated.


回答1:


I would suggest that full support for floating point is both unnecessary and ill-advised. It is a large amount of code to solve a trivial problem; and without floating-point hardware, floating point operations are usually best avoided in any case for performance, code space and memory usage reasons.

So it appears that duration is in units of 1/125000 seconds and that you wish to output a value to a precision of 0.1 milliseconds. So:

unsigned msec_x10 = duration * 10u / 125u ;

sprintf( glb_response, 
         "break: %u.%u msec", 
         msec_x10 / 10,  
         msec_x10 % 10 ) ;

If you want rounding to the nearest tenth (as opposed to rounding down), then:

unsigned msec_x10 = ((duration * 20u) + 125 ) / 250u ;



回答2:


I agree with Clifford that if you don't need floats (or only need them for printing), don't use them.

However, if your program is already using floats extensively, and you need a way to print them, consider adapting a public domain printf such as the one from SQLite.



来源:https://stackoverflow.com/questions/12545317/printf-support-for-msp430-micro-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!