ファンクション・ジェネレータ.出力信号:正弦波,矩形波,矩形波 (5倍波まで).ノイズの付加が可能.

Dependencies:   mbed SerialTxRxIntr Random

Revision:
3:e4e95731cab9
Parent:
2:46bbe00aecc3
Child:
4:925a2ab34c03
--- a/main.cpp	Fri Sep 07 13:02:15 2018 +0000
+++ b/main.cpp	Fri Sep 28 03:33:01 2018 +0000
@@ -1,21 +1,27 @@
 //----------------------------------------------------------------------
-//  ファンクション・ジェネレータ
+//  ファンクション・ジェネレータ (Function generator)
 //
 //  設定できる項目
-//      波形の種類:  正弦波,矩形波,矩形波(フーリエ級数の最初の3項の和)
+//      波形の種類:  正弦波,矩形波,矩形波(フーリエ級数の5倍波までの和)
 //      振幅:         0.00 ~ 1.00 倍
-//      周波数:       100 Hz ~ 5 kHz
+//      周波数:       10 Hz ~ 5 kHz
 //      ノイズ付加の有無
 //  標本化間隔:5 μs
 //  使用タイマ:TIM7
-//  信号出力のピン:   A3
-//  同期信号出力のピン: A5
+//  信号出力のピン:    A3
+//  同期信号出力のピン:  A5
 //
 //  PC 側のプログラム
 //      F446_FunctionGenerator
 //      端末エミュレータでも使用可能(確認しているのは Tera Term のみ)
 //
-//  2018/09/07, Copyright (c) 2018 MIKAMI, Naoki
+//  注意
+//      コマンド等が不正であるかどうかは,PC 側の処理にゆだねられている.そのため,
+//      端末エミュレータから操作する場合,不正なコマンド等に対しては,基本的に無視す
+//      るようなプログラムになっている.
+//      端末エミュレータからの Back Space キーの入力には対応していない.
+//
+//  2018/09/28, Copyright (c) 2018 MIKAMI, Naoki
 //----------------------------------------------------------------------
 
 #include "F446_DAC.hpp"
@@ -92,20 +98,23 @@
     
     rxTx_.EchobackEnable();     // エコーバックを有効にする
 
-    printf("\r\nMicrocontroller based function generator: (C) MIKAMI, Naoki 2018.\r\n");
-    printf("You can alse use with terminal emulator like Tera Term.\r\n");
-    printf("Command:\r\n");
+    printf("\r\n\n+-------------------------------------------------------------------+\r\n");
+    printf("| Microcontroller-based function generator, (C) MIKAMI, Naoki 2018. |\r\n");
+    printf("|     You can alse use with terminal emulator like Tera Term.       |\r\n");
+    printf("|     You can use lowercase letters as command.                     |\r\n");
+    printf("+-------------------------------------------------------------------+\r\n\n");
+    printf("<Command>\r\n");
     printf("ON    Ountput enabled.\r\n");
     printf("OFF   Ountput disabled.\r\n");
     printf("SIN   Output: Sinusoidal wave.\r\n");
     printf("RECT  Output: Square wave.\r\n");
-    printf("COMPO Output: Square wave (Sum of 3rd term for Fourier series).\r\n");
+    printf("COMPO Output: Square wave (Sum of up to 5th harmonics).\r\n");
     printf("NsOn  With noize.\r\n");
     printf("NsOff Without noise.\r\n\n");
-    printf("Numeric value:\r\n");
+    printf("<Command with numeric value (n: 0 to 9)>\r\n");
     printf("#+'n.nn'  Relative ampliude of output: 0.00 to 1.00.\r\n");
-    printf("$+'nnnn'  Frequency: 0 to 5000 [Hz].\r\n");
-    printf("%%+'n.nn'  Relative magnitude of noize: 0.00 to 1.00.\r\n");
+    printf("$+'nnnn'  Frequency: 10 to 5000 [Hz].\r\n");
+    printf("%%+'n.nn'  Relative magnitude of noize: 0.00 to 1.00.\r\n\n");
     rxTx_.Tx("? ", false);
     
     timer_.Attach_us(&TimerIsr, T0_);   // タイマ割り込み設定
@@ -115,6 +124,7 @@
         if (rxTx_.IsEol())          // 受信バッファのデータが有効になった場合の処理
         {
             string str = rxTx_.GetBuffer();
+            for (int n=0; n<str.size(); n++) str[n] = toupper(str[n]);
             if (isalpha(str[0]))    // 先頭が A ~ Z, a ~ z の場合
             {
                 if (str.find("FG") != string::npos)
@@ -134,12 +144,21 @@
             {
                 char c1 = str[0];   // 先頭の文字を取得
                 float x = atof(str.substr(1, str.size()-1).c_str());
-                if (c1 == '#') volume_ = x;         // 出力振幅の変更
-                if (c1 == '$') dPhi_ = PI2_*x*TS_;  // 周波数の変更
-                if (c1 == '%') volNoize_ = x;       // ノイズの大きさの変更
+                if (x >= 0.0f)
+                {
+                    if (c1 == '#')              // 出力振幅の変更
+                        volume_ = (x < 1.0f) ? x : 1.0f;
+                    if (c1 == '$')              // 周波数の変更
+                    {
+                        x = (x < 10) ? 10 : x;
+                        x = (x > 5000) ? 5000 : x;
+                        dPhi_ = PI2_*x*TS_;
+                    }
+                    if (c1 == '%')              // ノイズの大きさの変更
+                        volNoize_ = (x < 1.0f) ? x : 1.0f;
+                }
             }
             rxTx_.Tx("? ", false);
         }
     }
 }
-