I2C UART 変換IC SC16IS750を使ってみた。
Arduino(ATMEGA328P-PU)を使っているとシリアルポートが足りない事ありますよね。
センサー類はI2C対応が多いけど
GPSモジュールとかはシリアルでしか信号出ていないし。
今回そんな悩みを解消してくれる【I2C UART 変換IC SC16IS750】を使ってみました。
テストしたモジュールは
Switch science の https://www.switch-science.com/catalog/2310/ です。
このボードは I2C固定でデフォルトのアドレスは (0xBA)となっています。
Arduino用のライブラリは
ボード供給元の Sparkfun では提供されていないので
https://github.com/SandboxElectronics/UART_Bridge を使いました。
githubからzipファイルをダウンロードして
Arduino IDE のライブラリに追加します。
(追加の手順は割愛します。)
ライブラリを追加したら
ファイル → スケッチの例 → UART_Bridge-master → I2CSELFTEST を開きます。
6行目の
SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AD);
を
SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_BA);
に変更します。(変換ボードのアドレスを変更した場合は変更したアドレスに合わせます。)
このサンプルスケッチはループバックテストなので
TXとRXを接続してください。
シリアルモニタで確認すると
device found
start serial communication
とだけ表示されます。なんか動いていないように見えますが
このサンプルプログラムを見ると
送信したデータと違うデータを受信したときのみ
"serial communication error" と表示して
正常の場合は何も表示しないようになっています。
使ったサンプルプログラム
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <Wire.h> #include <SC16IS750.h> #include <string.h> #include <SPI.h> SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_BA); //Connect TX and RX with a wire and run this sketch void setup() { Serial.begin(9600); // UART to Serial Bridge Initialization i2cuart.begin(9600); //baudrate setting if (i2cuart.ping()!=1) { Serial.println("device not found"); while(1); } else { Serial.println("device found"); } Serial.println("start serial communication"); }; void loop() { i2cuart.write(0x55); while(i2cuart.available()==0); if (i2cuart.read()!=0x55) { Serial.println("serial communication error"); while(1); } delay(200); i2cuart.write(0xAA); while(i2cuart.available()==0); if (i2cuart.read()!=0xAA) { Serial.println("serial communication error"); while(1); } delay(200); }; |
次回はシリアル出力の GPSモジュール を接続してみます。