![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@2:7706df2df05a, 2015-03-17 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Mar 17 00:00:16 2015 +0000
- Revision:
- 2:7706df2df05a
- Parent:
- 1:a5792f051953
- Child:
- 3:d7128a3520ac
3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:230fb37e5561 | 1 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 2:7706df2df05a | 2 | // "UITDSP_ADDA2" に含まれるライブラリの使用例 |
MikamiUitOpen | 0:230fb37e5561 | 3 | // |
MikamiUitOpen | 0:230fb37e5561 | 4 | // アナログ入力 |
MikamiUitOpen | 0:230fb37e5561 | 5 | // A0: ライン |
MikamiUitOpen | 0:230fb37e5561 | 6 | // A1: マイク |
MikamiUitOpen | 0:230fb37e5561 | 7 | // A2: 可変抵抗器 |
MikamiUitOpen | 0:230fb37e5561 | 8 | // |
MikamiUitOpen | 2:7706df2df05a | 9 | // 2015/03/14, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 0:230fb37e5561 | 10 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 11 | |
MikamiUitOpen | 2:7706df2df05a | 12 | #include "ADC_BuiltIn.hpp" // かならず必要 |
MikamiUitOpen | 0:230fb37e5561 | 13 | |
MikamiUitOpen | 0:230fb37e5561 | 14 | // 以下の "#define" 文を一つだけ有効にし,他はコメントとすること |
MikamiUitOpen | 2:7706df2df05a | 15 | //#define POLLING_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 16 | //#define POLLING_SW_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 17 | //#define INTERRUPT_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 18 | //#define INTERRUPT_2_CHANNELS |
MikamiUitOpen | 2:7706df2df05a | 19 | #define POLLING_DA_2_CHANNELS |
MikamiUitOpen | 0:230fb37e5561 | 20 | |
MikamiUitOpen | 2:7706df2df05a | 21 | using namespace Mikami; // かならず必要 |
MikamiUitOpen | 0:230fb37e5561 | 22 | |
MikamiUitOpen | 0:230fb37e5561 | 23 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 24 | // 基本的な使い方:ポーリング,1チャンネル |
MikamiUitOpen | 0:230fb37e5561 | 25 | |
MikamiUitOpen | 0:230fb37e5561 | 26 | #ifdef POLLING_1_CHANNEL |
MikamiUitOpen | 2:7706df2df05a | 27 | #include "DAC_MCP4922.hpp" // DAC MCP4922 用 |
MikamiUitOpen | 0:230fb37e5561 | 28 | |
MikamiUitOpen | 2:7706df2df05a | 29 | const int FS_ = 10000; // 標本化周波数: 10 kHz |
MikamiUitOpen | 2:7706df2df05a | 30 | ADC_BuiltIn adc_(A0, FS_); // A0 からの入力を使うように設定 |
MikamiUitOpen | 2:7706df2df05a | 31 | DAC_MCP4922 myDac_; // DAC を使うための設定 |
MikamiUitOpen | 0:230fb37e5561 | 32 | |
MikamiUitOpen | 0:230fb37e5561 | 33 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 34 | { |
MikamiUitOpen | 0:230fb37e5561 | 35 | printf("\r\nPOLLING_1_CHANNEL\r\n"); |
MikamiUitOpen | 2:7706df2df05a | 36 | myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定 |
MikamiUitOpen | 0:230fb37e5561 | 37 | |
MikamiUitOpen | 0:230fb37e5561 | 38 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 39 | { |
MikamiUitOpen | 0:230fb37e5561 | 40 | float xn = adc_.Read(); // A0 からの入力信号を読み込む |
MikamiUitOpen | 0:230fb37e5561 | 41 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 42 | // この部分に信号処理のプログラムを記述する |
MikamiUitOpen | 0:230fb37e5561 | 43 | |
MikamiUitOpen | 0:230fb37e5561 | 44 | float yn = xn; // 処理を行わず,そのまま出力する |
MikamiUitOpen | 0:230fb37e5561 | 45 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 46 | myDac_.Write(yn); // DAC へ出力する |
MikamiUitOpen | 0:230fb37e5561 | 47 | } |
MikamiUitOpen | 0:230fb37e5561 | 48 | } |
MikamiUitOpen | 0:230fb37e5561 | 49 | #endif // POLLING_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 50 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 51 | |
MikamiUitOpen | 0:230fb37e5561 | 52 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 53 | // ポーリングで入力の切り替えを行う場合 |
MikamiUitOpen | 0:230fb37e5561 | 54 | |
MikamiUitOpen | 0:230fb37e5561 | 55 | #ifdef POLLING_SW_CHANNEL |
MikamiUitOpen | 2:7706df2df05a | 56 | #include "DAC_MCP4922.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 57 | |
MikamiUitOpen | 2:7706df2df05a | 58 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 2:7706df2df05a | 59 | ADC_BuiltIn adc_(A0, FS_, A1); // for AD using channel A0 and A1 |
MikamiUitOpen | 2:7706df2df05a | 60 | DAC_MCP4921 myDac_; // for DA |
MikamiUitOpen | 0:230fb37e5561 | 61 | |
MikamiUitOpen | 0:230fb37e5561 | 62 | DigitalIn sw_(D2, PullDown); |
MikamiUitOpen | 0:230fb37e5561 | 63 | |
MikamiUitOpen | 0:230fb37e5561 | 64 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 65 | { |
MikamiUitOpen | 0:230fb37e5561 | 66 | printf("\r\nPOLLING_SW_CHANNEL\r\n"); |
MikamiUitOpen | 0:230fb37e5561 | 67 | myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:230fb37e5561 | 68 | |
MikamiUitOpen | 0:230fb37e5561 | 69 | int swBefore = 0; |
MikamiUitOpen | 0:230fb37e5561 | 70 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 71 | { |
MikamiUitOpen | 1:a5792f051953 | 72 | float xn = adc_.Read(); // Read from A0 or A1 |
MikamiUitOpen | 0:230fb37e5561 | 73 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 74 | float yn = xn; |
MikamiUitOpen | 0:230fb37e5561 | 75 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 76 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:230fb37e5561 | 77 | |
MikamiUitOpen | 0:230fb37e5561 | 78 | int swNow = sw_.read(); |
MikamiUitOpen | 0:230fb37e5561 | 79 | if (swNow != swBefore) |
MikamiUitOpen | 0:230fb37e5561 | 80 | { |
MikamiUitOpen | 0:230fb37e5561 | 81 | if (swNow == 0) adc_.Select1stChannel(); // line input |
MikamiUitOpen | 0:230fb37e5561 | 82 | else adc_.Select2ndChannel(); // mic. input |
MikamiUitOpen | 0:230fb37e5561 | 83 | swBefore = swNow; |
MikamiUitOpen | 0:230fb37e5561 | 84 | } |
MikamiUitOpen | 0:230fb37e5561 | 85 | } |
MikamiUitOpen | 0:230fb37e5561 | 86 | } |
MikamiUitOpen | 0:230fb37e5561 | 87 | #endif // POLLING_SW_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 88 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 89 | |
MikamiUitOpen | 0:230fb37e5561 | 90 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 91 | // ADC の EOC 割り込みを使う場合,入力:1チャンネル |
MikamiUitOpen | 0:230fb37e5561 | 92 | |
MikamiUitOpen | 0:230fb37e5561 | 93 | #ifdef INTERRUPT_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 94 | #include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt |
MikamiUitOpen | 0:230fb37e5561 | 95 | #include "DAC_MCP4922.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 96 | |
MikamiUitOpen | 2:7706df2df05a | 97 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 2:7706df2df05a | 98 | ADC_Intr adc_(A0, FS_); // for AD |
MikamiUitOpen | 2:7706df2df05a | 99 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:230fb37e5561 | 100 | |
MikamiUitOpen | 0:230fb37e5561 | 101 | // Interrupt service routine for ADC |
MikamiUitOpen | 0:230fb37e5561 | 102 | void AdcIsr() |
MikamiUitOpen | 0:230fb37e5561 | 103 | { |
MikamiUitOpen | 0:230fb37e5561 | 104 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:230fb37e5561 | 105 | myDac_.Write(xn); // to DAC |
MikamiUitOpen | 0:230fb37e5561 | 106 | } |
MikamiUitOpen | 0:230fb37e5561 | 107 | |
MikamiUitOpen | 0:230fb37e5561 | 108 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 109 | { |
MikamiUitOpen | 0:230fb37e5561 | 110 | printf("\r\nINTERRUPT_1_CHANNEL\r\n"); |
MikamiUitOpen | 0:230fb37e5561 | 111 | |
MikamiUitOpen | 0:230fb37e5561 | 112 | myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:230fb37e5561 | 113 | |
MikamiUitOpen | 0:230fb37e5561 | 114 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:230fb37e5561 | 115 | |
MikamiUitOpen | 0:230fb37e5561 | 116 | while (true) {} |
MikamiUitOpen | 0:230fb37e5561 | 117 | } |
MikamiUitOpen | 0:230fb37e5561 | 118 | #endif // INTERRUPT_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 119 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 120 | |
MikamiUitOpen | 0:230fb37e5561 | 121 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 122 | // ADC の EOC 割り込みを使う場合で,2チャンネル使う場合 |
MikamiUitOpen | 0:230fb37e5561 | 123 | |
MikamiUitOpen | 0:230fb37e5561 | 124 | #ifdef INTERRUPT_2_CHANNELS |
MikamiUitOpen | 0:230fb37e5561 | 125 | #include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt |
MikamiUitOpen | 0:230fb37e5561 | 126 | #include "DAC_MCP4922.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 127 | |
MikamiUitOpen | 0:230fb37e5561 | 128 | // A0: Line input |
MikamiUitOpen | 0:230fb37e5561 | 129 | // A1: Not used |
MikamiUitOpen | 0:230fb37e5561 | 130 | // A2: VR (controls volume of output signal) |
MikamiUitOpen | 2:7706df2df05a | 131 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 2:7706df2df05a | 132 | ADC_Intr adc_(A0, FS_, A1, A2); // for AD |
MikamiUitOpen | 0:230fb37e5561 | 133 | DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A, D11, D13, D10, D12); // for DA |
MikamiUitOpen | 0:230fb37e5561 | 134 | |
MikamiUitOpen | 0:230fb37e5561 | 135 | uint16_t a2_ = 0; |
MikamiUitOpen | 0:230fb37e5561 | 136 | uint16_t a21_ = 0; |
MikamiUitOpen | 0:230fb37e5561 | 137 | float vol_ = 1.0f; |
MikamiUitOpen | 0:230fb37e5561 | 138 | |
MikamiUitOpen | 0:230fb37e5561 | 139 | // Interrupt service routine for ADC |
MikamiUitOpen | 0:230fb37e5561 | 140 | void AdcIsr() |
MikamiUitOpen | 0:230fb37e5561 | 141 | { |
MikamiUitOpen | 0:230fb37e5561 | 142 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:230fb37e5561 | 143 | |
MikamiUitOpen | 0:230fb37e5561 | 144 | adc_.Select3rdChannel(); // Enable A2 |
MikamiUitOpen | 2:7706df2df05a | 145 | adc_.SoftStart(); // ADC start for A2 input |
MikamiUitOpen | 0:230fb37e5561 | 146 | |
MikamiUitOpen | 0:230fb37e5561 | 147 | float yn = vol_*xn; // Volume control by VR |
MikamiUitOpen | 0:230fb37e5561 | 148 | myDac_.Write(yn); // to DAC |
MikamiUitOpen | 0:230fb37e5561 | 149 | |
MikamiUitOpen | 0:230fb37e5561 | 150 | // Read value which controls volume |
MikamiUitOpen | 0:230fb37e5561 | 151 | a2_ = adc_.ReadWait_u16(); |
MikamiUitOpen | 0:230fb37e5561 | 152 | |
MikamiUitOpen | 0:230fb37e5561 | 153 | adc_.Select1stChannel(); // Enable A0 |
MikamiUitOpen | 0:230fb37e5561 | 154 | adc_.ClearPending_EnableIRQ(); // Clear pending interrupt |
MikamiUitOpen | 0:230fb37e5561 | 155 | } |
MikamiUitOpen | 0:230fb37e5561 | 156 | |
MikamiUitOpen | 0:230fb37e5561 | 157 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 158 | { |
MikamiUitOpen | 0:230fb37e5561 | 159 | printf("\r\nINTERRUPT_2_CHANNELS\r\n"); |
MikamiUitOpen | 0:230fb37e5561 | 160 | myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:230fb37e5561 | 161 | |
MikamiUitOpen | 0:230fb37e5561 | 162 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:230fb37e5561 | 163 | |
MikamiUitOpen | 0:230fb37e5561 | 164 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 165 | { |
MikamiUitOpen | 0:230fb37e5561 | 166 | if (abs(a2_ - a21_) > 10) |
MikamiUitOpen | 0:230fb37e5561 | 167 | { |
MikamiUitOpen | 0:230fb37e5561 | 168 | vol_ = ((float)a2_)/4095.0f; |
MikamiUitOpen | 0:230fb37e5561 | 169 | a21_ = a2_; |
MikamiUitOpen | 0:230fb37e5561 | 170 | } |
MikamiUitOpen | 0:230fb37e5561 | 171 | wait(0.1f); |
MikamiUitOpen | 0:230fb37e5561 | 172 | } |
MikamiUitOpen | 0:230fb37e5561 | 173 | } |
MikamiUitOpen | 0:230fb37e5561 | 174 | #endif // INTERRUPT_2_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 175 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 176 | |
MikamiUitOpen | 0:230fb37e5561 | 177 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 178 | // DAC を2チャンネル出力で使う場合, 入力:ポーリング,1チャンネル |
MikamiUitOpen | 0:230fb37e5561 | 179 | |
MikamiUitOpen | 0:230fb37e5561 | 180 | #ifdef POLLING_DA_2_CHANNELS |
MikamiUitOpen | 0:230fb37e5561 | 181 | #include "DAC_MCP4922Dual.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 182 | |
MikamiUitOpen | 2:7706df2df05a | 183 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 2:7706df2df05a | 184 | ADC_BuiltIn adc_(A0, FS_); // for AD |
MikamiUitOpen | 2:7706df2df05a | 185 | DAC_MCP4922Dual myDac_; // for DA of 2 channels |
MikamiUitOpen | 0:230fb37e5561 | 186 | |
MikamiUitOpen | 0:230fb37e5561 | 187 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 188 | { |
MikamiUitOpen | 0:230fb37e5561 | 189 | printf("\r\nPOLLING_DA_2_CHANNELS\r\n"); |
MikamiUitOpen | 0:230fb37e5561 | 190 | |
MikamiUitOpen | 0:230fb37e5561 | 191 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 192 | { |
MikamiUitOpen | 0:230fb37e5561 | 193 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:230fb37e5561 | 194 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 195 | float yn = xn; |
MikamiUitOpen | 0:230fb37e5561 | 196 | //----------------------------------------------- |
MikamiUitOpen | 2:7706df2df05a | 197 | myDac_.Write(yn, -yn); // Write to DAC |
MikamiUitOpen | 0:230fb37e5561 | 198 | } |
MikamiUitOpen | 0:230fb37e5561 | 199 | } |
MikamiUitOpen | 0:230fb37e5561 | 200 | #endif // POLLING_DA_2_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 201 | //------------------------------------------------------------------------------ |