Example programs of AD and DA using classes in UITDSP_ADDA for ST Nucleo F401RE. UITDSP_ADDA に含まれるクラスを使った AD および DA のためのプログラムの例.ST Nucleo F401 用.
Dependencies: UITDSP_ADDA mbed
Diff: main.cpp
- Revision:
- 4:3c6b3d9e5415
- Parent:
- 3:d7128a3520ac
- Child:
- 5:019c0f09adf3
diff -r d7128a3520ac -r 3c6b3d9e5415 main.cpp --- a/main.cpp Thu Mar 26 12:42:05 2015 +0000 +++ b/main.cpp Sun Apr 19 09:40:30 2015 +0000 @@ -1,12 +1,12 @@ //------------------------------------------------------------------------------ -// "UITDSP_ADDA2" に含まれるライブラリの使用例 +// "UITDSP_ADDA" に含まれるライブラリの使用例 // // アナログ入力 // A0: ライン // A1: マイク // A2: 可変抵抗器 // -// 2015/03/26, Copyright (c) 2015 MIKAMI, Naoki +// 2015/04/19, Copyright (c) 2015 MIKAMI, Naoki //------------------------------------------------------------------------------ #include "ADC_BuiltIn.hpp" // かならず必要 @@ -33,7 +33,7 @@ int main() { printf("\r\nPOLLING_1_CHANNEL\r\n"); - myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定 + myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定 while (true) { @@ -55,31 +55,31 @@ #ifdef POLLING_SW_CHANNEL #include "DAC_MCP4922.hpp" -const int FS_ = 10000; // Sampling frequency: 10 kHz -ADC_BuiltIn adc_(A0, FS_, A1); // for AD using channel A0 and A1 -DAC_MCP4921 myDac_; // for DA +const int FS_ = 10000; // 標本化周波数: 10 kHz +ADC_BuiltIn adc_(A0, FS_, A1); // ADC の A0, A1 を使うための設定 +DAC_MCP4921 myDac_; // DAC を使うための設定 DigitalIn sw_(D2, PullDown); int main() { printf("\r\nPOLLING_SW_CHANNEL\r\n"); - myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz + myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定する int swBefore = 0; while (true) { - float xn = adc_.Read(); // Read from A0 or A1 + float xn = adc_.Read(); // A0 または A1 からの入力信号を読み込む //----------------------------------------------- float yn = xn; //----------------------------------------------- - myDac_.Write(yn); // Write to DAC + myDac_.Write(yn); // DAC へ出力する int swNow = sw_.read(); if (swNow != swBefore) { - if (swNow == 0) adc_.Select1stChannel(); // line input - else adc_.Select2ndChannel(); // mic. input + if (swNow == 0) adc_.Select1stChannel(); // 入力: ライン + else adc_.Select2ndChannel(); // 入力: マイク swBefore = swNow; } } @@ -91,26 +91,27 @@ // ADC の EOC 割り込みを使う場合,入力:1チャンネル #ifdef INTERRUPT_1_CHANNEL -#include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt +#include "ADC_Interrupt.hpp" // ADC 変換終了割り込みを使う場合 #include "DAC_MCP4922.hpp" -const int FS_ = 10000; // Sampling frequency: 10 kHz -ADC_Intr adc_(A0, FS_); // for AD -DAC_MCP4922 myDac_; // for DA +const int FS_ = 10000; // 標本化周波数: 10 kHz +ADC_Intr adc_(A0, FS_); // ADC の A0 を使うための設定 +DAC_MCP4922 myDac_; // DAC を使うための設定 -// Interrupt service routine for ADC +// ADC 変換終了割り込みに対する割り込みサービス・ルーチン void AdcIsr() { - float xn = adc_.Read(); // Read from A0 - myDac_.Write(xn); // to DAC + float xn = adc_.Read(); // A0 からの入力信号を読み込む + myDac_.Write(xn); // DAC へ出力する } int main() { printf("\r\nINTERRUPT_1_CHANNEL\r\n"); - myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz + myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定する + // ADC 変換終了割り込みに対する割り込みサービス・ルーチンを割り当てる adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt while (true) {} @@ -120,53 +121,53 @@ //------------------------------------------------------------------------------ // ADC の EOC 割り込みを使う場合で,2チャンネル使う場合 +// A0 から読み込んだ信号の振幅を,A2 に接続されている半固定抵抗器で, +// コントロールして出力する #ifdef INTERRUPT_2_CHANNELS -#include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt +#include "ADC_Interrupt.hpp" // ADC 変換終了割り込みを使う場合 #include "DAC_MCP4922.hpp" -// A0: Line input -// A1: Not used -// A2: VR (controls volume of output signal) -const int FS_ = 10000; // Sampling frequency: 10 kHz -ADC_Intr adc_(A0, FS_, A1, A2); // for AD -DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A, D11, D13, D10, D12); // for DA +const int FS_ = 10000; // 標本化周波数: 10 kHz +ADC_Intr adc_(A0, FS_, A2); // ADC の A0, A2 を使うための設定 +DAC_MCP4922 myDac_; // DAC を使うための設定 -uint16_t a2_ = 0; -uint16_t a21_ = 0; -float vol_ = 1.0f; +uint16_t vr_ = 0; // A2 から読み込んだ現在の値 +uint16_t vr1_ = 0; // A2 から,一つ前に読み込んだ値 +float vol_ = 1.0f; // 出力の振幅の倍率 -// Interrupt service routine for ADC +// ADC 変換終了割り込みに対する割り込みサービス・ルーチン void AdcIsr() { - float xn = adc_.Read(); // Read from A0 + float xn = adc_.Read(); // A0 からの入力信号を読み込む - adc_.Select3rdChannel(); // Enable A2 - adc_.SoftStart(); // ADC start for A2 input + adc_.Select2ndChannel(); // 入力チャンネルとして A2 を選択する + adc_.SoftStart(); // AD 変換を開始する(ADC 変換終割り込みは禁止する) - float yn = vol_*xn; // Volume control by VR - myDac_.Write(yn); // to DAC + float yn = vol_*xn; // 出力信号の振幅を半固定抵抗器の値でコントロールする + myDac_.Write(yn); // DAC へ出力する - // Read value which controls volume - a2_ = adc_.ReadWait_u16(); + // 半固定抵抗器の値を A2 から読み込む + vr_ = adc_.ReadWait_u16(); - adc_.Select1stChannel(); // Enable A0 - adc_.ClearPending_EnableIRQ(); // Clear pending interrupt + adc_.Select1stChannel(); // 入力チャンネルとして A0 を選択する + adc_.ClearPending_EnableIRQ(); // 保留中の割り込みをクリアする } int main() { printf("\r\nINTERRUPT_2_CHANNELS\r\n"); - myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz + myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定する - adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt + // ADC 変換終了割り込みに対する割り込みサービス・ルーチンを割り当てる + adc_.SetIntrVec(AdcIsr); while (true) { - if (abs(a2_ - a21_) > 10) + if (abs(vr_ - vr1_) > 10) { - vol_ = ((float)a2_)/4095.0f; - a21_ = a2_; + vol_ = ((float)vr_)/4095.0f; + vr1_ = vr_; } wait(0.1f); } @@ -180,9 +181,9 @@ #ifdef POLLING_DA_2_CHANNELS #include "DAC_MCP4922Dual.hpp" -const int FS_ = 10000; // Sampling frequency: 10 kHz -ADC_BuiltIn adc_(A0, FS_); // for AD -DAC_MCP4922Dual myDac_; // for DA of 2 channels +const int FS_ = 10000; // 標本化周波数: 10 kHz +ADC_BuiltIn adc_(A0, FS_); // ADC の A0 を使うための設定 +DAC_MCP4922Dual myDac_; // DAC の2つのチャンネルを使うための設定 int main() { @@ -190,11 +191,11 @@ while (true) { - float xn = adc_.Read(); // Read from A0 + float xn = adc_.Read(); // A0 からの入力信号を読み込む //----------------------------------------------- float yn = xn; //----------------------------------------------- - myDac_.Write(yn, -yn); // Write to DAC + myDac_.Write(yn, -yn); // DAC の2つのチャンネルへ出力する } } #endif // POLLING_DA_2_CHANNEL