/*****************************************/ /* シリアルデータ出力のテスト */ /* TX0:GP0 123456 ABCDEFG. */ /* TX1:GP4 123456 123ms rst-uvw-xyz. */ /* 連番と文字列を送信 */ /* TX0データは1秒ごと出力 */ /* TX1のサイクルは0.3~2秒ごと */ /*****************************************/ // テストポート // GP6 TX0タイミング // GP7 TX1タイミング /***** 初期設定 *****/ UART Serial2(4, 5, NC, NC); // そのままではSerial2が使えないので // ピン番号指定を実行 /***** マクロ *****/ #define LED_BLINK digitalWrite(LED_BUILTIN, f_xLED) #define GP6_H gpio_put(6, HIGH) #define GP6_L gpio_put(6, LOW) #define GP7_H gpio_put(7, HIGH) #define GP7_L gpio_put(7, LOW) /***** データ *****/ uint32_t tm_tx0; // tx0 送出タイマー (ms) uint32_t tm_tx1; // tx1 送出タイマー uint32_t int_tx0; // tx0 送出繰り返し時間 (ms) uint32_t int_tx1; // tx1 送出繰り返し時間 (ms) uint32_t cnt_tx0; // tx0 データ送出回数 max999999 uint32_t cnt_tx1; // tx1 データ送出回数 max999999 byte f_xLED; // LED点滅用 char tx_bff0[64]; // 文字出力バッファ char tx_bff1[64]; /***** SETUP *****/ void setup() { pinMode(LED_BUILTIN, OUTPUT); // LED pinMode(6, OUTPUT); // GP6 TX0送出タイミング pinMode(7, OUTPUT); // GP7 TX1送出タイミング Serial.begin(9600); // USB Serial1.begin(9600); // TX0 Serial2.begin(9600); // TX1 tm_tx0 = tm_tx1 = millis(); // 現時間 int_tx0 = int_tx1 = 1000; // 送出間隔 1秒に } /***** LOOP *****/ void loop() { uint32_t tm1; while(1){ // loop tm1 = millis(); // 現時間 if((tm1 - tm_tx0) >= int_tx0){ // 1秒経過 tm_tx0 = tm1; f_xLED ^= 1; // LED点滅 LED_BLINK; GP6_H; // (!!!) sprintf(tx_bff0,"%6ld ABCDEFG.", cnt_tx0); // カウント値出力 Serial1.println(tx_bff0); GP6_L; // (!!!) cnt_tx0++; if(cnt_tx0 > 999999) cnt_tx0 = 0; } if((tm1 - tm_tx1) >= int_tx1){ // 送出インターバル時間 tm_tx1 = tm1; GP7_H; // (!!!) sprintf(tx_bff1,"%6ld %4ldms rst-uvw-xyz.", cnt_tx1, int_tx1); // カウンタと間隔 Serial2.println(tx_bff1); GP7_L; // (!!!) cnt_tx1++; if(cnt_tx1 > 999999) cnt_tx1 = 0; int_tx1 = random(300, 2000+1); // 間隔=0.3秒~2秒 } } } /*==== end of "tx_data_2ch1.ino" ====*/