Mega644版Arduino(Sanguino)・・・アナログ編

次にアナログ入力を試してみたのですが、サンプル「AnalogInput」をコンパイル&実行しようとしてみると、エラーが発生します。

エラーの原因は

int sensorPin = A0

の行で、「A0」が未定義ということのようです。で、元からある arduino-0022/hardware/arduino の下のファイルと、追加した arduino-0022/hardware/sanguino の下のファイルを見比べてみると、WProgram.h のファイルにA0~A15の設定が抜けていることがわかりました。

そこで、arduino-0022/hardware/arduino/cores/arduino/WProgram.h から以下の部分のうちの黒字部分と、ATmega644Pの記述(赤字の部分)を追加しました。

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
const static uint8_t A0 = 54;
const static uint8_t A1 = 55;
const static uint8_t A2 = 56;
const static uint8_t A3 = 57;
const static uint8_t A4 = 58;
const static uint8_t A5 = 59;
const static uint8_t A6 = 60;
const static uint8_t A7 = 61;
const static uint8_t A8 = 62;
const static uint8_t A9 = 63;
const static uint8_t A10 = 64;
const static uint8_t A11 = 65;
const static uint8_t A12 = 66;
const static uint8_t A13 = 67;
const static uint8_t A14 = 68;
const static uint8_t A15 = 69;
#elif defined(__AVR_ATmega644P__)
const static uint8_t A0 = 24;
const static uint8_t A1 = 25;
const static uint8_t A2 = 26;
const static uint8_t A3 = 27;
const static uint8_t A4 = 28;
const static uint8_t A5 = 29;
const static uint8_t A6 = 30;
const static uint8_t A7 = 31;
#else
const static uint8_t A0 = 14;
const static uint8_t A1 = 15;
const static uint8_t A2 = 16;
const static uint8_t A3 = 17;
const static uint8_t A4 = 18;
const static uint8_t A5 = 19;
const static uint8_t A6 = 20;
const static uint8_t A7 = 21;
#endif

さらに、これを処理する部分があるはずなので、探したところ、同様に wiring_analog.c の中の、analogRead()という関数で処理していそうだったので、同様に以下のように黒字部分を arduino-0022/hardware/arduino/cores/arduino/wiring_analog.c から追加し、さらに赤字部分をオリジナルで追加しました。

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega644P__)
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif

これでサンプル「AnalogInput」も無事に動作するようになりました。
ただ、これだけの記述だけではデジタルピンが sanguino.ccに掲載されている端子配置にならないような気がしたのですが、上記の変更でデジタルピンもこの順番で動作するようです。

・・・もう少し調べてみたら、arduino-0022/hardware/sanguino/cores/arduino/pins_arduino.c の中の配列 digital_pin_to_bit_mask_PGM[] の中で、PortAだけ逆順に並んでいました。ここでデジタルピンの並び順を変換しているようですね。

ライセンスを見ると、LGPLのようなので、差分を取り込んだファイルを添付(sanguino_analog.tar.gz)しておきます。同名のファイルを差し替えれば同じレベルになるはずです

“Mega644版Arduino(Sanguino)・・・アナログ編” への2件の返信

  1. 勉強不足で申し訳ありません
    最近Arduino328を始めたのですが、RTCの時刻設定をキー入力で
    行うようにすると他の機器の取り込み等で、メモリー不足になり
    Sanguino(Atmega1284)を知り、格闘しているのですが、アナログ入力が出来ないと分かり(?)ここに至りました
    しかし、上記の内容がどこに書いてあるのかわかりません(その程度の人間です)またsanguino.ccをくりっくすると望遠鏡の画面に移ります
    なんとか教えてください
    こちらのsanguinoは0101r1です
    arduinoのIDEは1.0.3です
    なおsanguinoのピン接続は変更したくはありません
    年末でお忙しいでしょうがよろしくお願いいたします。

  2. 自己解決しました
    どうもA0からA7の配列順がひっくりかえっていました
    ゆえにA7でプログラムを実行しなおしたら動作しました
    どうもお騒がせいたしました

大野輝久 へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)