2024年3月24日日曜日

RP2040にてarudino-picoでWireを使うときの注意点

まず、ArduinoでRP2040を動かすにはMbed OS RP2040という公式系と、arduino picoというphilhowerさんが作ったバージョンがある。少なくともI2Cの使い方がそれぞれ違う。

公式版は、I2Cで任意のピンを使うのが難しいらしい。なので、arduino-picoを選ぶ。

そして、Wire/Wire1は、setup()やloop()のスコープ中でしか使えない。
グローバルスコープで使おうとすると、未定義となりうまくコンパイルできない。
RP2040-ZeroにてArduino IDEでI2Cを使うとき、micropythonの感覚で書くと失敗する。
こんなのにハマるのは私だけかもしれないが、誰かの役に立つかもしれないので一応公開しておく。

■OKのとき

#inclue <Wire.h>
void setup(){
  Wire1.setSDA(14);
  Wire1.setSCL(15);
}

■NGのとき

#inclue <Wire.h>
Wire1.setSDA(14);
Wire1.setSCL(15); void setup(){ }
ちなみに、NGのときは次のようなエラーが出る。
<ファイルパス>:18:3: error: 'Wire1' does not name a type
   18 |   Wire1.setSDA(14);
      |   ^~~~~
<ファイルパス>:19:3: error: 'Wire1' does not name a type
   19 |   Wire1.setSCL(15);
      |   ^~~~~
<ファイルパス>:20:3: error: 'Wire1' does not name a type
   20 |   Wire1.begin();
      |   ^~~~~
exit status 1
Compilation error: 'Wire1' does not name a type
Wire1が定義されていない、となっている。

0 件のコメント:

コメントを投稿