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

Committer:
MikamiUitOpen
Date:
Fri Feb 20 05:54:49 2015 +0000
Revision:
0:230fb37e5561
Child:
1:a5792f051953
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:230fb37e5561 1 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 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 0:230fb37e5561 9 // 2015/02/20, Copyright (c) 2015 MIKAMI, Naoki
MikamiUitOpen 0:230fb37e5561 10 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 11
MikamiUitOpen 0:230fb37e5561 12 #include "InternalADC.hpp" // かならず必要
MikamiUitOpen 0:230fb37e5561 13
MikamiUitOpen 0:230fb37e5561 14 // 以下の "#define" 文を一つだけ有効にし,他はコメントとすること
MikamiUitOpen 0:230fb37e5561 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 0:230fb37e5561 19 //#define POLLING_DA_2_CHANNELS
MikamiUitOpen 0:230fb37e5561 20
MikamiUitOpen 0:230fb37e5561 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 0:230fb37e5561 27 #include "DAC_MCP4921.hpp"
MikamiUitOpen 0:230fb37e5561 28
MikamiUitOpen 0:230fb37e5561 29 const int TS_ = 100; // 標本化間隔: 0.1 [ms]
MikamiUitOpen 0:230fb37e5561 30 InternalADC adc_(A0); // A0 からの入力を使うように設定
MikamiUitOpen 0:230fb37e5561 31 DAC_MCP4921 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 0:230fb37e5561 36
MikamiUitOpen 0:230fb37e5561 37 adc_.SetSamplingPeriod(TS_); // 標本化間隔の設定
MikamiUitOpen 0:230fb37e5561 38 myDac_.ScfClockTim3(420000); // 出力の LPF の遮断周波数を 4.2 kHz に設定
MikamiUitOpen 0:230fb37e5561 39
MikamiUitOpen 0:230fb37e5561 40 while (true)
MikamiUitOpen 0:230fb37e5561 41 {
MikamiUitOpen 0:230fb37e5561 42 float xn = adc_.Read(); // A0 からの入力信号を読み込む
MikamiUitOpen 0:230fb37e5561 43 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 44 // この部分に信号処理のプログラムを記述する
MikamiUitOpen 0:230fb37e5561 45
MikamiUitOpen 0:230fb37e5561 46 float yn = xn; // 処理を行わず,そのまま出力する
MikamiUitOpen 0:230fb37e5561 47 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 48 myDac_.Write(yn); // DAC へ出力する
MikamiUitOpen 0:230fb37e5561 49 }
MikamiUitOpen 0:230fb37e5561 50 }
MikamiUitOpen 0:230fb37e5561 51 #endif // POLLING_1_CHANNEL
MikamiUitOpen 0:230fb37e5561 52 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 53
MikamiUitOpen 0:230fb37e5561 54 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 55 // ポーリングで入力の切り替えを行う場合
MikamiUitOpen 0:230fb37e5561 56
MikamiUitOpen 0:230fb37e5561 57 #ifdef POLLING_SW_CHANNEL
MikamiUitOpen 0:230fb37e5561 58 #include "DAC_MCP4921.hpp"
MikamiUitOpen 0:230fb37e5561 59
MikamiUitOpen 0:230fb37e5561 60 const int TS_ = 100; // Sampling period: 0.1 ms
MikamiUitOpen 0:230fb37e5561 61 InternalADC adc_(A0, A1); // for AD
MikamiUitOpen 0:230fb37e5561 62 DAC_MCP4921 myDac_; // for DA
MikamiUitOpen 0:230fb37e5561 63
MikamiUitOpen 0:230fb37e5561 64 DigitalIn sw_(D2, PullDown);
MikamiUitOpen 0:230fb37e5561 65
MikamiUitOpen 0:230fb37e5561 66 int main()
MikamiUitOpen 0:230fb37e5561 67 {
MikamiUitOpen 0:230fb37e5561 68 printf("\r\nPOLLING_SW_CHANNEL\r\n");
MikamiUitOpen 0:230fb37e5561 69
MikamiUitOpen 0:230fb37e5561 70 adc_.SetSamplingPeriod(TS_);
MikamiUitOpen 0:230fb37e5561 71 myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz
MikamiUitOpen 0:230fb37e5561 72
MikamiUitOpen 0:230fb37e5561 73 int swBefore = 0;
MikamiUitOpen 0:230fb37e5561 74 while (true)
MikamiUitOpen 0:230fb37e5561 75 {
MikamiUitOpen 0:230fb37e5561 76 float xn = adc_.Read(); // Read from A0
MikamiUitOpen 0:230fb37e5561 77 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 78 float yn = xn;
MikamiUitOpen 0:230fb37e5561 79 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 80 myDac_.Write(yn); // Write to DAC
MikamiUitOpen 0:230fb37e5561 81
MikamiUitOpen 0:230fb37e5561 82 int swNow = sw_.read();
MikamiUitOpen 0:230fb37e5561 83 if (swNow != swBefore)
MikamiUitOpen 0:230fb37e5561 84 {
MikamiUitOpen 0:230fb37e5561 85 if (swNow == 0) adc_.Select1stChannel(); // line input
MikamiUitOpen 0:230fb37e5561 86 else adc_.Select2ndChannel(); // mic. input
MikamiUitOpen 0:230fb37e5561 87 swBefore = swNow;
MikamiUitOpen 0:230fb37e5561 88 }
MikamiUitOpen 0:230fb37e5561 89 }
MikamiUitOpen 0:230fb37e5561 90 }
MikamiUitOpen 0:230fb37e5561 91 #endif // POLLING_SW_CHANNEL
MikamiUitOpen 0:230fb37e5561 92 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 93
MikamiUitOpen 0:230fb37e5561 94 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 95 // ADC の EOC 割り込みを使う場合,入力:1チャンネル
MikamiUitOpen 0:230fb37e5561 96
MikamiUitOpen 0:230fb37e5561 97 #ifdef INTERRUPT_1_CHANNEL
MikamiUitOpen 0:230fb37e5561 98 #include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt
MikamiUitOpen 0:230fb37e5561 99 #include "DAC_MCP4922.hpp"
MikamiUitOpen 0:230fb37e5561 100
MikamiUitOpen 0:230fb37e5561 101 const int TS_ = 100; // Sampling period: 0.1 ms
MikamiUitOpen 0:230fb37e5561 102 ADC_Intr adc_(A0); // for AD
MikamiUitOpen 0:230fb37e5561 103 DAC_MCP4922 myDac_; // for DA
MikamiUitOpen 0:230fb37e5561 104
MikamiUitOpen 0:230fb37e5561 105 // Interrupt service routine for ADC
MikamiUitOpen 0:230fb37e5561 106 void AdcIsr()
MikamiUitOpen 0:230fb37e5561 107 {
MikamiUitOpen 0:230fb37e5561 108 float xn = adc_.Read(); // Read from A0
MikamiUitOpen 0:230fb37e5561 109 myDac_.Write(xn); // to DAC
MikamiUitOpen 0:230fb37e5561 110 }
MikamiUitOpen 0:230fb37e5561 111
MikamiUitOpen 0:230fb37e5561 112 int main()
MikamiUitOpen 0:230fb37e5561 113 {
MikamiUitOpen 0:230fb37e5561 114 printf("\r\nINTERRUPT_1_CHANNEL\r\n");
MikamiUitOpen 0:230fb37e5561 115
MikamiUitOpen 0:230fb37e5561 116 adc_.SetSamplingPeriod(TS_);
MikamiUitOpen 0:230fb37e5561 117 myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz
MikamiUitOpen 0:230fb37e5561 118
MikamiUitOpen 0:230fb37e5561 119 adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt
MikamiUitOpen 0:230fb37e5561 120
MikamiUitOpen 0:230fb37e5561 121 while (true) {}
MikamiUitOpen 0:230fb37e5561 122 }
MikamiUitOpen 0:230fb37e5561 123 #endif // INTERRUPT_1_CHANNEL
MikamiUitOpen 0:230fb37e5561 124 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 125
MikamiUitOpen 0:230fb37e5561 126 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 127 // ADC の EOC 割り込みを使う場合で,2チャンネル使う場合
MikamiUitOpen 0:230fb37e5561 128
MikamiUitOpen 0:230fb37e5561 129 #ifdef INTERRUPT_2_CHANNELS
MikamiUitOpen 0:230fb37e5561 130 #include "ADC_Interrupt.hpp" // for ADC not using ADC interrupt
MikamiUitOpen 0:230fb37e5561 131 #include "DAC_MCP4922.hpp"
MikamiUitOpen 0:230fb37e5561 132
MikamiUitOpen 0:230fb37e5561 133 // A0: Line input
MikamiUitOpen 0:230fb37e5561 134 // A1: Not used
MikamiUitOpen 0:230fb37e5561 135 // A2: VR (controls volume of output signal)
MikamiUitOpen 0:230fb37e5561 136 const int TS_ = 100; // Sampling period: 0.1 ms
MikamiUitOpen 0:230fb37e5561 137 ADC_Intr adc_(A0, A1, A2); // for AD
MikamiUitOpen 0:230fb37e5561 138 DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A, D11, D13, D10, D12); // for DA
MikamiUitOpen 0:230fb37e5561 139
MikamiUitOpen 0:230fb37e5561 140 uint16_t a2_ = 0;
MikamiUitOpen 0:230fb37e5561 141 uint16_t a21_ = 0;
MikamiUitOpen 0:230fb37e5561 142 float vol_ = 1.0f;
MikamiUitOpen 0:230fb37e5561 143
MikamiUitOpen 0:230fb37e5561 144 // Interrupt service routine for ADC
MikamiUitOpen 0:230fb37e5561 145 void AdcIsr()
MikamiUitOpen 0:230fb37e5561 146 {
MikamiUitOpen 0:230fb37e5561 147 float xn = adc_.Read(); // Read from A0
MikamiUitOpen 0:230fb37e5561 148
MikamiUitOpen 0:230fb37e5561 149 adc_.Select3rdChannel(); // Enable A2
MikamiUitOpen 0:230fb37e5561 150 adc_.SoftStartDisableIRQ(); // ADC start for A2 input
MikamiUitOpen 0:230fb37e5561 151
MikamiUitOpen 0:230fb37e5561 152 float yn = vol_*xn; // Volume control by VR
MikamiUitOpen 0:230fb37e5561 153 myDac_.Write(yn); // to DAC
MikamiUitOpen 0:230fb37e5561 154
MikamiUitOpen 0:230fb37e5561 155 // Read value which controls volume
MikamiUitOpen 0:230fb37e5561 156 a2_ = adc_.ReadWait_u16();
MikamiUitOpen 0:230fb37e5561 157
MikamiUitOpen 0:230fb37e5561 158 adc_.Select1stChannel(); // Enable A0
MikamiUitOpen 0:230fb37e5561 159 adc_.ClearPending_EnableIRQ(); // Clear pending interrupt
MikamiUitOpen 0:230fb37e5561 160 }
MikamiUitOpen 0:230fb37e5561 161
MikamiUitOpen 0:230fb37e5561 162 int main()
MikamiUitOpen 0:230fb37e5561 163 {
MikamiUitOpen 0:230fb37e5561 164 printf("\r\nINTERRUPT_2_CHANNELS\r\n");
MikamiUitOpen 0:230fb37e5561 165
MikamiUitOpen 0:230fb37e5561 166 adc_.SetSamplingPeriod(TS_);
MikamiUitOpen 0:230fb37e5561 167 myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz
MikamiUitOpen 0:230fb37e5561 168
MikamiUitOpen 0:230fb37e5561 169 adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt
MikamiUitOpen 0:230fb37e5561 170
MikamiUitOpen 0:230fb37e5561 171 while (true)
MikamiUitOpen 0:230fb37e5561 172 {
MikamiUitOpen 0:230fb37e5561 173 if (abs(a2_ - a21_) > 10)
MikamiUitOpen 0:230fb37e5561 174 {
MikamiUitOpen 0:230fb37e5561 175 vol_ = ((float)a2_)/4095.0f;
MikamiUitOpen 0:230fb37e5561 176 a21_ = a2_;
MikamiUitOpen 0:230fb37e5561 177 }
MikamiUitOpen 0:230fb37e5561 178 wait(0.1f);
MikamiUitOpen 0:230fb37e5561 179 }
MikamiUitOpen 0:230fb37e5561 180 }
MikamiUitOpen 0:230fb37e5561 181 #endif // INTERRUPT_2_CHANNEL
MikamiUitOpen 0:230fb37e5561 182 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 183
MikamiUitOpen 0:230fb37e5561 184 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 185 // DAC を2チャンネル出力で使う場合, 入力:ポーリング,1チャンネル
MikamiUitOpen 0:230fb37e5561 186
MikamiUitOpen 0:230fb37e5561 187 #ifdef POLLING_DA_2_CHANNELS
MikamiUitOpen 0:230fb37e5561 188 #include "DAC_MCP4922Dual.hpp"
MikamiUitOpen 0:230fb37e5561 189
MikamiUitOpen 0:230fb37e5561 190 InternalADC adc_(A0); // for AD
MikamiUitOpen 0:230fb37e5561 191 DAC_MCP4922Dual myDac_; // for DA
MikamiUitOpen 0:230fb37e5561 192
MikamiUitOpen 0:230fb37e5561 193 int main()
MikamiUitOpen 0:230fb37e5561 194 {
MikamiUitOpen 0:230fb37e5561 195 printf("\r\nPOLLING_DA_2_CHANNELS\r\n");
MikamiUitOpen 0:230fb37e5561 196 adc_.SetSamplingPeriod(20); // 20 micro seconds
MikamiUitOpen 0:230fb37e5561 197
MikamiUitOpen 0:230fb37e5561 198 while (true)
MikamiUitOpen 0:230fb37e5561 199 {
MikamiUitOpen 0:230fb37e5561 200 float xn = adc_.Read(); // Read from A0
MikamiUitOpen 0:230fb37e5561 201 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 202 float yn = xn;
MikamiUitOpen 0:230fb37e5561 203 //-----------------------------------------------
MikamiUitOpen 0:230fb37e5561 204 myDac_.Write(yn, yn); // Write to DAC
MikamiUitOpen 0:230fb37e5561 205 }
MikamiUitOpen 0:230fb37e5561 206 }
MikamiUitOpen 0:230fb37e5561 207 #endif // POLLING_DA_2_CHANNEL
MikamiUitOpen 0:230fb37e5561 208 //------------------------------------------------------------------------------
MikamiUitOpen 0:230fb37e5561 209