メインコンテンツに移動

printf,sprintf

標準のstdioに、printf、sprintfが組み込まれているが、初期設定では、minimal設定で最低限の引数のみ有効になっているとのことで、%fや%Xなどのコードはエラーになる。プロジェクトのpropetyの中にあるLanguage option設定で、nofloatかfullを選べば%Xが使えるが、それでも、30%以上のコード消費がある。%fに至ってはほぼ使い切りそうな勢いだ。

msp430のCompilerマニュアルには次の記載になっていた。

Enables support for smaller, limited versions of the printf function family (sprintf, fprintf, etc.) and the scanf function family (sscanf, fscanf, etc.) run-time-support functions. The valid values are:
• full: Supports all format specifiers. This is the default.
• nofloat: Excludes support for printing and scanning floating-point values. Supports all format specifiers except %a, %A, %f, %F, %g, %G, %e, and %E.
• minimal: Supports the printing and scanning of integer, char, or string values without width or precision flags. Specifically, only the %%, %d, %o, %c, %s, and %x format specifiers are supported

There is no run-time error checking to detect if a format specifier is used for which support is not included. 

minimalでは%xをサポートしているとあるが、私の環境(Compiler versionはV18となっていた)ではエラーになったので、%xを作るのに、下記のようやった(固定長)

#include <msp430.h>				
#include <stdio.h>
char str_payload[32];    // AT$SF=[payload] payload is hex
char buff[16];
unsigned int i;
int mods;
long temp;

main(){
・・・
  temp = ADC10MEM;
  i=3;
  while( i<4 ){
    mods = temp % 16;
    if(mods<10){
      buff[i]=0x30+mods;
    } else {
    buff[i]=(mods-10)+0x41;
    }
    temp = temp/16;
    i--;
  }
  buff[4]=0;
  sprintf(str_payload,"AT$SF=%s\r",buff);
・・・
}

%fの場合、100倍や1000倍にして小数部と整数部を分けてやればよいと思う。

 

コメントを追加

Plain text

  • HTMLタグは利用できません。
  • ウェブページのアドレスとメールアドレスは自動的にリンクに変換されます。
  • 行と段落は自動的に折り返されます。