![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Prototype program of AD and DA using classes in UIT_ADDA. This program uses interrupt of ADC. Second channnel input is used to control volume of output for ST Nucleo F401RE. UIT_ADDA のクラスを使った AD および DA のためのプログラムの雛形.ADC の割り込みを使うバージョン.2番目のチャンネルの ADC の入力は,出力信号の大きさをコントロールするために使う.ST Nucleo F401 用.
Dependencies: UIT_ACM1602NI UIT_ADDA mbed
main.cpp@8:6afde3ece70c, 2014-11-20 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Nov 20 09:01:13 2014 +0000
- Revision:
- 8:6afde3ece70c
- Parent:
- 5:fd3e47335bfa
- Child:
- 10:cec66d437a6e
9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:494ae18d3b30 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 1:43a38eaed0fc | 2 | // 割り込みを使って AD DA を行う場合の雛形2(入力:2 チャンネル) |
MikamiUitOpen | 0:494ae18d3b30 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:494ae18d3b30 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 1:43a38eaed0fc | 5 | // A2: used for control volume of output |
MikamiUitOpen | 8:6afde3ece70c | 6 | // 2014/11/20, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:494ae18d3b30 | 7 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:494ae18d3b30 | 8 | |
MikamiUitOpen | 0:494ae18d3b30 | 9 | #include "ADC_Interrupt.hpp" // for ADC using interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 10 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:494ae18d3b30 | 11 | |
MikamiUitOpen | 0:494ae18d3b30 | 12 | using namespace Mikami; |
MikamiUitOpen | 0:494ae18d3b30 | 13 | |
MikamiUitOpen | 2:8cbfd28344db | 14 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 4:063b26cc467a | 15 | ADC_Intr adc_(A0, FS_, A1, A2); // for AD |
MikamiUitOpen | 4:063b26cc467a | 16 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:494ae18d3b30 | 17 | |
MikamiUitOpen | 0:494ae18d3b30 | 18 | uint16_t a2_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 19 | uint16_t a21_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 20 | float vol_ = 1.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 21 | |
MikamiUitOpen | 0:494ae18d3b30 | 22 | // Interrupt service routine for ADC |
MikamiUitOpen | 0:494ae18d3b30 | 23 | void AdcIsr() |
MikamiUitOpen | 0:494ae18d3b30 | 24 | { |
MikamiUitOpen | 0:494ae18d3b30 | 25 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:494ae18d3b30 | 26 | |
MikamiUitOpen | 0:494ae18d3b30 | 27 | adc_.Select3rdChannel(); // Enable A2 |
MikamiUitOpen | 0:494ae18d3b30 | 28 | adc_.SoftStart(); // ADC start for A2 input |
MikamiUitOpen | 0:494ae18d3b30 | 29 | |
MikamiUitOpen | 0:494ae18d3b30 | 30 | float yn = vol_*xn; // Volume control by VR |
MikamiUitOpen | 0:494ae18d3b30 | 31 | myDac_.Write(yn); // to DAC |
MikamiUitOpen | 0:494ae18d3b30 | 32 | |
MikamiUitOpen | 0:494ae18d3b30 | 33 | // Read value which controls volume |
MikamiUitOpen | 0:494ae18d3b30 | 34 | a2_ = adc_.ReadWait_u16(); |
MikamiUitOpen | 0:494ae18d3b30 | 35 | |
MikamiUitOpen | 0:494ae18d3b30 | 36 | adc_.Select1stChannel(); // Enable A0 |
MikamiUitOpen | 0:494ae18d3b30 | 37 | adc_.ClearPending_EnableIRQ(); // Clear pending interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 38 | } |
MikamiUitOpen | 0:494ae18d3b30 | 39 | |
MikamiUitOpen | 0:494ae18d3b30 | 40 | int main() |
MikamiUitOpen | 0:494ae18d3b30 | 41 | { |
MikamiUitOpen | 5:fd3e47335bfa | 42 | myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:494ae18d3b30 | 43 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 44 | |
MikamiUitOpen | 0:494ae18d3b30 | 45 | while (true) |
MikamiUitOpen | 0:494ae18d3b30 | 46 | { |
MikamiUitOpen | 0:494ae18d3b30 | 47 | if (abs(a2_ - a21_) > 10) |
MikamiUitOpen | 0:494ae18d3b30 | 48 | { |
MikamiUitOpen | 0:494ae18d3b30 | 49 | vol_ = ((float)a2_)/4095.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 50 | a21_ = a2_; |
MikamiUitOpen | 0:494ae18d3b30 | 51 | } |
MikamiUitOpen | 0:494ae18d3b30 | 52 | wait(0.1f); |
MikamiUitOpen | 0:494ae18d3b30 | 53 | } |
MikamiUitOpen | 0:494ae18d3b30 | 54 | } |