nobcha23の日記

PICマイコンやArduinoを使う電子回路遊びを紹介します

シリアル信号線モニター by Arduino

最近友人の会社で制御回路開発を手伝っております。対象の制御回路ではシリアル通信経由で内蔵ユニットを制御するのでデバッグ実験にはシリアル回線モニターが欠かせません。

以前作成したエアバリアブルさん設計のモニターを使ってました。
ローコストLCDシリアル通信モニター(外部クロック版)

今回さらに必要となり、Arduinoで同じようなものを作ってみました。

Arduino Nanoで作ったシリアルモニター

Arduino NANOとi2cインターフェイス液晶をつなぎ、Arduino標準シリアルポートを使います。RX端子にモニター信号を入力します。

Arduino Nano用ヘッダー拡張シールドを使用

通信速度は立ち上げ時にポート設定を参照して、4800,9600,19200,38400を選べるようにしました。



// Serial monitor by using serial monitor
// Received data goes to i2c LCD of 2004
// i2c接続の2004液晶にシリアルポートで受信したデータを流し表示します。
// 電源投入あるいはリセット時のD3・D4入力状態でシリアル通信の速度を
// D3/D4 00:4800,01:9600bps, 10:19.2, 11:38.4kbpsで選びます。
// データが0x0Aだと、カーソルを次の行先頭に移動させます。
#include
int char_count = 0;
char data;
volatile int lcd_cursor;
long Speed[4] = {4800,9600,19200,38400};
int speed_sel;
// LiquidCrystal_I2C型変数の宣言
LiquidCrystal_I2C lcd(0x27, 20 , 4); // 0x27のアドレス,20列4行のLCDを使用
void setup() {
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
speed_sel = digitalRead(3); // Speed D3/D4 00:4800,01:9600, 10:19.2, 11:38.4
speed_sel = (speed_sel << 1) + digitalRead(4);
Serial.begin(Speed[speed_sel]);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Monitor");
lcd.print(Speed[speed_sel]);
lcd.print(" 2004 ");
lcd_cursor = 1;
lcd.setCursor(0, lcd_cursor);
}
void loop() {
if (Serial.available() > 0) {
data = Serial.read();
char_count++;
lcd.print( data );
if((char_count%20) == 0){
lcd_cursor++;
if(lcd_cursor >= 4) lcd_cursor = 0;
lcd.setCursor(0, lcd_cursor);
delay(1);
}
if(data == 0x0A){
lcd_cursor ++;
if(lcd_cursor>4) lcd_cursor = 0;
lcd.setCursor(0, lcd_cursor);
delay(1);
}
}
}