经过大量的搜索和反复试验,我找到了解决方案。
我误解了我看到的错误,Thomas 是对的。添加到我自己的功能时,该功能对于芯片来说太大了。
显而易见的选择没有去任何地方,但我会在这里列出它们以帮助其他新手遇到这个问题。
itoa() - 16 位和ultoa() - 32 位已实现,但太小了。
sprintf(%d)太小,并且sprintf(%lld)未在 WinAVR (AVR-GCC) 中实现。
此代码有效(有警告):
void main()
{
unsigned long long tagid;
char tagid_str[12];
tagid = 109876543210ull
convert_to_decimal(tagid_str, tagid);
}
void convert_to_decimal(char* dst, unsigned long long src)
{
int i;
for (i = 0; i < 12; i ++)
{
dst[11 - i] = '0' + (int)(src % 10);
src /= 10;
}
dst[12] = 0;
}
但是看看统计数据:
程序:7358 字节(89.8% 已满)(.text + .data + .bootloader)
数据:256 字节(25.0% 已满)(.data + .bss + .noinit)
罪魁祸首是%运算符。我无法解释为什么使用 long long 会生成近 8k 的代码!
这是一个可行的选择。我将其修改为仅使用unsigned long long(64 位)最多 12 个十进制数字,以适应我正在使用的 RFID 阅读器格式。
void main()
{
unsigned long long tagid;
char tagid_str[12];
tagid = 000000000000ull;
ulltostr((unsigned long long)tagid, tagid_str);
tagid = 000000000001ull;
ulltostr((unsigned long long)tagid, tagid_str);
tagid = 109876543210ull;
ulltostr((unsigned long long)tagid, tagid_str);
tagid = 900000000000ull;
ulltostr((unsigned long long)tagid, tagid_str);
tagid = 999999999999ull;
ulltostr((unsigned long long)tagid, tagid_str);
}
//http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=31199
void ulltostr(unsigned long long val, char *s )
{
char *p;
unsigned char d, i;
unsigned char zero;
unsigned long long test;
unsigned long long uval = val;
p = s;
zero = 1;
i = 12;
do{
i--;
if ( i==0) test =10;
else if ( i==1) test =100;
else if ( i==2) test =1000;
else if ( i==3) test =10000;
else if ( i==4) test =100000;
else if ( i==5) test =1000000;
else if ( i==6) test =10000000;
else if ( i==7) test =100000000;
else if ( i==8) test =1000000000;
else if ( i==9) test =10000000000;
else if ( i==10) test=100000000000;
else if ( i==11) test=1000000000000;
else if ( i==12) test=10000000000000;
for( d = '0'; uval >= test; uval -= test )
{
d++;
zero = 0;
}
if( zero == 0 )
*p++ = d ;
}while( i );
*p++ = (unsigned char)uval + '0';
}
和统计数据:
程序:758 字节(9.3%已满)(.text + .data + .bootloader)
数据:0 字节(0.0%已满)(.data + .bss + .noinit)
好多了:)
我大部分时间都和Douglas Jones在一起,但答案最终来自AVR Freaks。