public_notes/content/20240201182238 ATMega系のArduinoでprintfする方法.md
Kaz Saita(raspi5) 92aba7c016
Some checks are pending
Build and Test / build-and-test (macos-latest) (push) Waiting to run
Build and Test / build-and-test (ubuntu-latest) (push) Waiting to run
Build and Test / build-and-test (windows-latest) (push) Waiting to run
Build and Test / publish-tag (push) Waiting to run
/ test (push) Waiting to run
Deploy Quartz site to GitHub Pages / build (push) Waiting to run
Deploy Quartz site to GitHub Pages / deploy (push) Blocked by required conditions
sync notes(auto)
2024-10-10 12:00:05 +09:00

788 B

20240201182238 ATMega系のArduinoでprintfする方法

#tech #arduino #printf

いつも忘れるのでメモ。 この、fdevopenを使う方法は、この関数がAVR用のavr-libcでしか定義されていないため、 ESP32やARMなど他のMCUがベースになっている場合には使えないので注意。

Aruino UNO3、 Arduino Nano、Pro Microなどでは使える。

// 名前はなんでもよい
int my_putc( char c, FILE *t) {
  return Serial.write( c );
}

void setup()
{
    Serial.begin(115200);
    fdevopen( &my_putc, 0);
    // 以降printfが使える。例↓
    printf("Hello world! A0 = %d\n", analogRead(0));
}

void loop()
{

}

cf.