![](/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@12:4dcb3d21332c, 2014-12-31 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Dec 31 10:45:10 2014 +0000
- Revision:
- 12:4dcb3d21332c
- Parent:
- 10:cec66d437a6e
13
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 | 10:cec66d437a6e | 3 | // Analog Input : A0, A3 (A1: not used) |
MikamiUitOpen | 10:cec66d437a6e | 4 | // A3 is used to cotrol volume of output signal |
MikamiUitOpen | 0:494ae18d3b30 | 5 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 1:43a38eaed0fc | 6 | // A2: used for control volume of output |
MikamiUitOpen | 10:cec66d437a6e | 7 | // 2014/12/19, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:494ae18d3b30 | 8 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:494ae18d3b30 | 9 | |
MikamiUitOpen | 0:494ae18d3b30 | 10 | #include "ADC_Interrupt.hpp" // for ADC using interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:494ae18d3b30 | 12 | |
MikamiUitOpen | 0:494ae18d3b30 | 13 | using namespace Mikami; |
MikamiUitOpen | 0:494ae18d3b30 | 14 | |
MikamiUitOpen | 2:8cbfd28344db | 15 | const int FS_ = 10000; // Sampling frequency: 10 kHz |
MikamiUitOpen | 4:063b26cc467a | 16 | ADC_Intr adc_(A0, FS_, A1, A2); // for AD |
MikamiUitOpen | 4:063b26cc467a | 17 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:494ae18d3b30 | 18 | |
MikamiUitOpen | 0:494ae18d3b30 | 19 | uint16_t a2_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 20 | uint16_t a21_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 21 | float vol_ = 1.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 22 | |
MikamiUitOpen | 0:494ae18d3b30 | 23 | // Interrupt service routine for ADC |
MikamiUitOpen | 0:494ae18d3b30 | 24 | void AdcIsr() |
MikamiUitOpen | 0:494ae18d3b30 | 25 | { |
MikamiUitOpen | 0:494ae18d3b30 | 26 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:494ae18d3b30 | 27 | |
MikamiUitOpen | 0:494ae18d3b30 | 28 | adc_.Select3rdChannel(); // Enable A2 |
MikamiUitOpen | 0:494ae18d3b30 | 29 | adc_.SoftStart(); // ADC start for A2 input |
MikamiUitOpen | 0:494ae18d3b30 | 30 | |
MikamiUitOpen | 0:494ae18d3b30 | 31 | float yn = vol_*xn; // Volume control by VR |
MikamiUitOpen | 0:494ae18d3b30 | 32 | myDac_.Write(yn); // to DAC |
MikamiUitOpen | 0:494ae18d3b30 | 33 | |
MikamiUitOpen | 0:494ae18d3b30 | 34 | // Read value which controls volume |
MikamiUitOpen | 0:494ae18d3b30 | 35 | a2_ = adc_.ReadWait_u16(); |
MikamiUitOpen | 0:494ae18d3b30 | 36 | |
MikamiUitOpen | 0:494ae18d3b30 | 37 | adc_.Select1stChannel(); // Enable A0 |
MikamiUitOpen | 0:494ae18d3b30 | 38 | adc_.ClearPending_EnableIRQ(); // Clear pending interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 39 | } |
MikamiUitOpen | 0:494ae18d3b30 | 40 | |
MikamiUitOpen | 0:494ae18d3b30 | 41 | int main() |
MikamiUitOpen | 0:494ae18d3b30 | 42 | { |
MikamiUitOpen | 10:cec66d437a6e | 43 | myDac_.ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:494ae18d3b30 | 44 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 45 | |
MikamiUitOpen | 0:494ae18d3b30 | 46 | while (true) |
MikamiUitOpen | 0:494ae18d3b30 | 47 | { |
MikamiUitOpen | 0:494ae18d3b30 | 48 | if (abs(a2_ - a21_) > 10) |
MikamiUitOpen | 0:494ae18d3b30 | 49 | { |
MikamiUitOpen | 0:494ae18d3b30 | 50 | vol_ = ((float)a2_)/4095.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 51 | a21_ = a2_; |
MikamiUitOpen | 0:494ae18d3b30 | 52 | } |
MikamiUitOpen | 0:494ae18d3b30 | 53 | wait(0.1f); |
MikamiUitOpen | 0:494ae18d3b30 | 54 | } |
MikamiUitOpen | 0:494ae18d3b30 | 55 | } |