![](/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@0:494ae18d3b30, 2014-10-20 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Oct 20 07:48:40 2014 +0000
- Revision:
- 0:494ae18d3b30
- Child:
- 1:43a38eaed0fc
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:494ae18d3b30 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:494ae18d3b30 | 2 | // 割り込みを使って AD DA を行う場合の雛形2 |
MikamiUitOpen | 0:494ae18d3b30 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:494ae18d3b30 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 0:494ae18d3b30 | 5 | // 2014/10/20, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:494ae18d3b30 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:494ae18d3b30 | 7 | |
MikamiUitOpen | 0:494ae18d3b30 | 8 | #include "mbed.h" |
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 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:494ae18d3b30 | 13 | |
MikamiUitOpen | 0:494ae18d3b30 | 14 | using namespace Mikami; |
MikamiUitOpen | 0:494ae18d3b30 | 15 | |
MikamiUitOpen | 0:494ae18d3b30 | 16 | const int FS_ = 10000; // Sampling frequency: 24 kHz |
MikamiUitOpen | 0:494ae18d3b30 | 17 | ADC_Intr adc_(A0, FS_, A1, A2); // for AD |
MikamiUitOpen | 0:494ae18d3b30 | 18 | DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA |
MikamiUitOpen | 0:494ae18d3b30 | 19 | |
MikamiUitOpen | 0:494ae18d3b30 | 20 | uint16_t a2_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 21 | uint16_t a21_ = 0; |
MikamiUitOpen | 0:494ae18d3b30 | 22 | float vol_ = 1.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 23 | |
MikamiUitOpen | 0:494ae18d3b30 | 24 | // Interrupt service routine for ADC |
MikamiUitOpen | 0:494ae18d3b30 | 25 | void AdcIsr() |
MikamiUitOpen | 0:494ae18d3b30 | 26 | { |
MikamiUitOpen | 0:494ae18d3b30 | 27 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:494ae18d3b30 | 28 | |
MikamiUitOpen | 0:494ae18d3b30 | 29 | adc_.Select3rdChannel(); // Enable A2 |
MikamiUitOpen | 0:494ae18d3b30 | 30 | adc_.SoftStart(); // ADC start for A2 input |
MikamiUitOpen | 0:494ae18d3b30 | 31 | |
MikamiUitOpen | 0:494ae18d3b30 | 32 | float yn = vol_*xn; // Volume control by VR |
MikamiUitOpen | 0:494ae18d3b30 | 33 | myDac_.Write(yn); // to DAC |
MikamiUitOpen | 0:494ae18d3b30 | 34 | |
MikamiUitOpen | 0:494ae18d3b30 | 35 | // Read value which controls volume |
MikamiUitOpen | 0:494ae18d3b30 | 36 | a2_ = adc_.ReadWait_u16(); |
MikamiUitOpen | 0:494ae18d3b30 | 37 | |
MikamiUitOpen | 0:494ae18d3b30 | 38 | adc_.Select1stChannel(); // Enable A0 |
MikamiUitOpen | 0:494ae18d3b30 | 39 | adc_.ClearPending_EnableIRQ(); // Clear pending interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 40 | } |
MikamiUitOpen | 0:494ae18d3b30 | 41 | |
MikamiUitOpen | 0:494ae18d3b30 | 42 | int main() |
MikamiUitOpen | 0:494ae18d3b30 | 43 | { |
MikamiUitOpen | 0:494ae18d3b30 | 44 | ScfClockTim3(420000); // cutoff frequency: 4.2 kHz |
MikamiUitOpen | 0:494ae18d3b30 | 45 | adc_.SetIntrVec(AdcIsr); // Assign ISR for ADC interrupt |
MikamiUitOpen | 0:494ae18d3b30 | 46 | |
MikamiUitOpen | 0:494ae18d3b30 | 47 | while (true) |
MikamiUitOpen | 0:494ae18d3b30 | 48 | { |
MikamiUitOpen | 0:494ae18d3b30 | 49 | if (abs(a2_ - a21_) > 10) |
MikamiUitOpen | 0:494ae18d3b30 | 50 | { |
MikamiUitOpen | 0:494ae18d3b30 | 51 | vol_ = ((float)a2_)/4095.0f; |
MikamiUitOpen | 0:494ae18d3b30 | 52 | a21_ = a2_; |
MikamiUitOpen | 0:494ae18d3b30 | 53 | } |
MikamiUitOpen | 0:494ae18d3b30 | 54 | wait(0.1f); |
MikamiUitOpen | 0:494ae18d3b30 | 55 | } |
MikamiUitOpen | 0:494ae18d3b30 | 56 | } |