関連
MPLABXでPIC開発: シミュレータでprintf出力する方法
PIC12F1822でUART経由でprintf()の出力を行おうとしたところ
#include <stdio.h>
printf("hello");
はOKなのに
#include <stdio.h>
printf("%d", 10);
のコードでエラーが発生
NG mcc_generated_files/eusart.c:52:: error: (1250) could not find space (1 byte) for variable _eusartRxLastError
どうもメモリが足りないらしい。XC8関連の資料によると、stdio.h
のライブラリが大きいので、printf()
のフォーマットを解析してライブラリのテンプレート?を変えて、必要最小限のサイズにするためらしい。%d
のときに利用するライブラリのサイズが超えていると思われる。
PIC12F1822
はプログラムメモリが2048words, RAMが128Bしかない。そこで、もう少しメモリサイズが大きいPIC12F1572
(プログラムメモリが2048words, RAMが256B)でビルドしたところ成功した。
そこで、printf()がどのくらいリソースを消費するのか気になったので試してみた。
PIC12F1572でprintf()コンパイル
PIC12F1572のメモリは以下
PIC12F1572 | |
---|---|
RAM(bytes) | 256 |
プログラムメモリ(words/bytes) | 2,024/3.5k |
リテラル文字を出力するタイプ
#include <stdio.h>
printf("hello");
RAM(bytes) | 70 |
プログラムメモリ(words) | 290 |
1文字を出力するタイプ。%
のフォーマット使うことで増加
#include <stdio.h>
printf("%c", 'a');
RAM(bytes) | 83 |
プログラムメモリ(words) | 450 |
符号なし整数
#include <stdio.h>
printf("%u", 10);
RAM(bytes) | 124 |
プログラムメモリ(words) | 1003 |
hex表記
#include <stdio.h>
printf("%x", 10);
RAM(bytes) | 136 |
プログラムメモリ(words) | 1053 |
符号あり整数。PIC12F1822でエラーになったケース。RAM使用量が130bytesと超えていたのがわかる。プログラムメモリもすごい増えいている。
#include <stdio.h>
printf("%d", 10);
RAM(bytes) | 130 |
プログラムメモリ(words) | 1432 |
組み合わせその1。純粋に増加量の足し算?
#include <stdio.h>
printf("hello\n");
printf("%x\n",10);
RAM(bytes) | 136 |
プログラムメモリ(words) | 1069 |
組み合わせその2。純粋に増加量の足し算?
#include <stdio.h>
printf("hello\n");
printf("%d\n",10);
RAM(bytes) | 136 |
プログラムメモリ(words) | 1637 |
組み合わせその3。PIC12F1572でもエラーになる
#include <stdio.h>
printf("hello\n");
printf("%x\n",10);
printf("%d\n",10);
PIC16F1508
先程エラーになったケースを今度はプログラムメモリを倍増したPIC12F1572
で試してみる。
PIC12F1572 | |
---|---|
RAM(bytes) | 256 |
プログラムメモリ(words/bytes) | 4,096/7.0k |
初期状態
先程エラーになったケースでもビルド成功。
RAM(bytes) | 144 |
プログラムメモリ(words) | 2067 |
#include <stdio.h>
printf("hello\n");
printf("%x\n",10);
printf("%d\n",10);
もしシリアル出力が必要だったとすると、ほかのコード量にもよるが最低でも上記のメモリサイズが必要になる。むやみに無駄なコードを書かないのは基本としても、sprintf()
含めて上記ぐらいはしたくなる。だとすると、最低でもプログラムメモリが4kwordsぐらいは必要ってことか。