public_notes/content/20240201182238 ATMega系のArduinoでprintfする方法.md

36 lines
788 B
Markdown
Raw Permalink Normal View History

2024-02-14 23:30:02 +09:00
# 20240201182238 ATMega系のArduinoでprintfする方法
#tech #arduino #printf
いつも忘れるのでメモ。
この、`fdevopen`を使う方法は、この関数がAVR用のavr-libcでしか定義されていないため、
ESP32やARMなど他のMCUがベースになっている場合には使えないので注意。
Aruino UNO3、 Arduino Nano、Pro Microなどでは使える。
```cpp
// 名前はなんでもよい
int my_putc( char c, FILE *t) {
return Serial.write( c );
}
void setup()
{
Serial.begin(115200);
fdevopen( &my_putc, 0);
// 以降printfが使える。例↓
2024-10-10 12:00:05 +09:00
printf("Hello world! A0 = %d\n", analogRead(0));
2024-02-14 23:30:02 +09:00
}
void loop()
{
}
```
cf.
- <https://forum.arduino.cc/t/how-to-make-printf-work/6456>
- <https://cega.jp/avr/avr-libc_list/>