![](/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@4:3c6b3d9e5415, 2015-04-19 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Apr 19 09:40:30 2015 +0000
- Revision:
- 4:3c6b3d9e5415
- Parent:
- 3:d7128a3520ac
- Child:
- 5:019c0f09adf3
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:230fb37e5561 | 1 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 4:3c6b3d9e5415 | 2 | // "UITDSP_ADDA" に含まれるライブラリの使用例 |
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 | 4:3c6b3d9e5415 | 9 | // 2015/04/19, 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 | 3:d7128a3520ac | 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 | 3:d7128a3520ac | 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 | 4:3c6b3d9e5415 | 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 | 4:3c6b3d9e5415 | 58 | const int FS_ = 10000; // 標本化周波数: 10 kHz |
MikamiUitOpen | 4:3c6b3d9e5415 | 59 | ADC_BuiltIn adc_(A0, FS_, A1); // ADC の A0, A1 を使うための設定 |
MikamiUitOpen | 4:3c6b3d9e5415 | 60 | DAC_MCP4921 myDac_; // DAC を使うための設定 |
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 | 4:3c6b3d9e5415 | 67 | myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 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 | 4:3c6b3d9e5415 | 72 | float xn = adc_.Read(); // A0 または A1 からの入力信号を読み込む |
MikamiUitOpen | 0:230fb37e5561 | 73 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 74 | float yn = xn; |
MikamiUitOpen | 0:230fb37e5561 | 75 | //----------------------------------------------- |
MikamiUitOpen | 4:3c6b3d9e5415 | 76 | myDac_.Write(yn); // 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 | 4:3c6b3d9e5415 | 81 | if (swNow == 0) adc_.Select1stChannel(); // 入力: ライン |
MikamiUitOpen | 4:3c6b3d9e5415 | 82 | else adc_.Select2ndChannel(); // 入力: マイク |
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 | 4:3c6b3d9e5415 | 94 | #include "ADC_Interrupt.hpp" // ADC 変換終了割り込みを使う場合 |
MikamiUitOpen | 0:230fb37e5561 | 95 | #include "DAC_MCP4922.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 96 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 97 | const int FS_ = 10000; // 標本化周波数: 10 kHz |
MikamiUitOpen | 4:3c6b3d9e5415 | 98 | ADC_Intr adc_(A0, FS_); // ADC の A0 を使うための設定 |
MikamiUitOpen | 4:3c6b3d9e5415 | 99 | DAC_MCP4922 myDac_; // DAC を使うための設定 |
MikamiUitOpen | 0:230fb37e5561 | 100 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 101 | // ADC 変換終了割り込みに対する割り込みサービス・ルーチン |
MikamiUitOpen | 0:230fb37e5561 | 102 | void AdcIsr() |
MikamiUitOpen | 0:230fb37e5561 | 103 | { |
MikamiUitOpen | 4:3c6b3d9e5415 | 104 | float xn = adc_.Read(); // A0 からの入力信号を読み込む |
MikamiUitOpen | 4:3c6b3d9e5415 | 105 | myDac_.Write(xn); // 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 | 4:3c6b3d9e5415 | 112 | myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定する |
MikamiUitOpen | 0:230fb37e5561 | 113 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 114 | // ADC 変換終了割り込みに対する割り込みサービス・ルーチンを割り当てる |
MikamiUitOpen | 0:230fb37e5561 | 115 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:230fb37e5561 | 116 | |
MikamiUitOpen | 0:230fb37e5561 | 117 | while (true) {} |
MikamiUitOpen | 0:230fb37e5561 | 118 | } |
MikamiUitOpen | 0:230fb37e5561 | 119 | #endif // INTERRUPT_1_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 120 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 121 | |
MikamiUitOpen | 0:230fb37e5561 | 122 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 123 | // ADC の EOC 割り込みを使う場合で,2チャンネル使う場合 |
MikamiUitOpen | 4:3c6b3d9e5415 | 124 | // A0 から読み込んだ信号の振幅を,A2 に接続されている半固定抵抗器で, |
MikamiUitOpen | 4:3c6b3d9e5415 | 125 | // コントロールして出力する |
MikamiUitOpen | 0:230fb37e5561 | 126 | |
MikamiUitOpen | 0:230fb37e5561 | 127 | #ifdef INTERRUPT_2_CHANNELS |
MikamiUitOpen | 4:3c6b3d9e5415 | 128 | #include "ADC_Interrupt.hpp" // ADC 変換終了割り込みを使う場合 |
MikamiUitOpen | 0:230fb37e5561 | 129 | #include "DAC_MCP4922.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 130 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 131 | const int FS_ = 10000; // 標本化周波数: 10 kHz |
MikamiUitOpen | 4:3c6b3d9e5415 | 132 | ADC_Intr adc_(A0, FS_, A2); // ADC の A0, A2 を使うための設定 |
MikamiUitOpen | 4:3c6b3d9e5415 | 133 | DAC_MCP4922 myDac_; // DAC を使うための設定 |
MikamiUitOpen | 0:230fb37e5561 | 134 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 135 | uint16_t vr_ = 0; // A2 から読み込んだ現在の値 |
MikamiUitOpen | 4:3c6b3d9e5415 | 136 | uint16_t vr1_ = 0; // A2 から,一つ前に読み込んだ値 |
MikamiUitOpen | 4:3c6b3d9e5415 | 137 | float vol_ = 1.0f; // 出力の振幅の倍率 |
MikamiUitOpen | 0:230fb37e5561 | 138 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 139 | // ADC 変換終了割り込みに対する割り込みサービス・ルーチン |
MikamiUitOpen | 0:230fb37e5561 | 140 | void AdcIsr() |
MikamiUitOpen | 0:230fb37e5561 | 141 | { |
MikamiUitOpen | 4:3c6b3d9e5415 | 142 | float xn = adc_.Read(); // A0 からの入力信号を読み込む |
MikamiUitOpen | 0:230fb37e5561 | 143 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 144 | adc_.Select2ndChannel(); // 入力チャンネルとして A2 を選択する |
MikamiUitOpen | 4:3c6b3d9e5415 | 145 | adc_.SoftStart(); // AD 変換を開始する(ADC 変換終割り込みは禁止する) |
MikamiUitOpen | 0:230fb37e5561 | 146 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 147 | float yn = vol_*xn; // 出力信号の振幅を半固定抵抗器の値でコントロールする |
MikamiUitOpen | 4:3c6b3d9e5415 | 148 | myDac_.Write(yn); // DAC へ出力する |
MikamiUitOpen | 0:230fb37e5561 | 149 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 150 | // 半固定抵抗器の値を A2 から読み込む |
MikamiUitOpen | 4:3c6b3d9e5415 | 151 | vr_ = adc_.ReadWait_u16(); |
MikamiUitOpen | 0:230fb37e5561 | 152 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 153 | adc_.Select1stChannel(); // 入力チャンネルとして A0 を選択する |
MikamiUitOpen | 4:3c6b3d9e5415 | 154 | adc_.ClearPending_EnableIRQ(); // 保留中の割り込みをクリアする |
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 | 4:3c6b3d9e5415 | 160 | myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定する |
MikamiUitOpen | 0:230fb37e5561 | 161 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 162 | // ADC 変換終了割り込みに対する割り込みサービス・ルーチンを割り当てる |
MikamiUitOpen | 4:3c6b3d9e5415 | 163 | adc_.SetIntrVec(AdcIsr); |
MikamiUitOpen | 0:230fb37e5561 | 164 | |
MikamiUitOpen | 0:230fb37e5561 | 165 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 166 | { |
MikamiUitOpen | 4:3c6b3d9e5415 | 167 | if (abs(vr_ - vr1_) > 10) |
MikamiUitOpen | 0:230fb37e5561 | 168 | { |
MikamiUitOpen | 4:3c6b3d9e5415 | 169 | vol_ = ((float)vr_)/4095.0f; |
MikamiUitOpen | 4:3c6b3d9e5415 | 170 | vr1_ = vr_; |
MikamiUitOpen | 0:230fb37e5561 | 171 | } |
MikamiUitOpen | 0:230fb37e5561 | 172 | wait(0.1f); |
MikamiUitOpen | 0:230fb37e5561 | 173 | } |
MikamiUitOpen | 0:230fb37e5561 | 174 | } |
MikamiUitOpen | 0:230fb37e5561 | 175 | #endif // INTERRUPT_2_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 176 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 177 | |
MikamiUitOpen | 0:230fb37e5561 | 178 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:230fb37e5561 | 179 | // DAC を2チャンネル出力で使う場合, 入力:ポーリング,1チャンネル |
MikamiUitOpen | 0:230fb37e5561 | 180 | |
MikamiUitOpen | 0:230fb37e5561 | 181 | #ifdef POLLING_DA_2_CHANNELS |
MikamiUitOpen | 0:230fb37e5561 | 182 | #include "DAC_MCP4922Dual.hpp" |
MikamiUitOpen | 0:230fb37e5561 | 183 | |
MikamiUitOpen | 4:3c6b3d9e5415 | 184 | const int FS_ = 10000; // 標本化周波数: 10 kHz |
MikamiUitOpen | 4:3c6b3d9e5415 | 185 | ADC_BuiltIn adc_(A0, FS_); // ADC の A0 を使うための設定 |
MikamiUitOpen | 4:3c6b3d9e5415 | 186 | DAC_MCP4922Dual myDac_; // DAC の2つのチャンネルを使うための設定 |
MikamiUitOpen | 0:230fb37e5561 | 187 | |
MikamiUitOpen | 0:230fb37e5561 | 188 | int main() |
MikamiUitOpen | 0:230fb37e5561 | 189 | { |
MikamiUitOpen | 0:230fb37e5561 | 190 | printf("\r\nPOLLING_DA_2_CHANNELS\r\n"); |
MikamiUitOpen | 0:230fb37e5561 | 191 | |
MikamiUitOpen | 0:230fb37e5561 | 192 | while (true) |
MikamiUitOpen | 0:230fb37e5561 | 193 | { |
MikamiUitOpen | 4:3c6b3d9e5415 | 194 | float xn = adc_.Read(); // A0 からの入力信号を読み込む |
MikamiUitOpen | 0:230fb37e5561 | 195 | //----------------------------------------------- |
MikamiUitOpen | 0:230fb37e5561 | 196 | float yn = xn; |
MikamiUitOpen | 0:230fb37e5561 | 197 | //----------------------------------------------- |
MikamiUitOpen | 4:3c6b3d9e5415 | 198 | myDac_.Write(yn, -yn); // DAC の2つのチャンネルへ出力する |
MikamiUitOpen | 0:230fb37e5561 | 199 | } |
MikamiUitOpen | 0:230fb37e5561 | 200 | } |
MikamiUitOpen | 0:230fb37e5561 | 201 | #endif // POLLING_DA_2_CHANNEL |
MikamiUitOpen | 0:230fb37e5561 | 202 | //------------------------------------------------------------------------------ |