STM32F446 の内蔵 ADC, DAC 用のライブラリ.このライブラリを登録した際のプログラム: Demo_DSP_ADDA. Library for build-in ADC and DAC in STM32F446.
Dependents: Demo_DSP_ADDA F446_DSP_FFT_Analyzer TrG_FFT_Analyzer DSP_MultirateLinearphase ... more
DSP_Dac.hpp@0:85348d58f498, 2020-05-21 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu May 21 10:42:04 2020 +0000
- Revision:
- 0:85348d58f498
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:85348d58f498 | 1 | //-------------------------------------------------------------------- |
MikamiUitOpen | 0:85348d58f498 | 2 | // STM32F446 内蔵の DAC 用のクラス(ヘッダ) |
MikamiUitOpen | 0:85348d58f498 | 3 | // 選択可能な入力端子: |
MikamiUitOpen | 0:85348d58f498 | 4 | // A2 (PA_4): ---- デフォルト |
MikamiUitOpen | 0:85348d58f498 | 5 | // D13 (PA_5): このポートはマイコンボードの LED もドライブ |
MikamiUitOpen | 0:85348d58f498 | 6 | // するのでこのポートは使わない方がよい |
MikamiUitOpen | 0:85348d58f498 | 7 | // |
MikamiUitOpen | 0:85348d58f498 | 8 | // 2020/05/21, Copyright (c) 2020 MIKAMI, Naoki |
MikamiUitOpen | 0:85348d58f498 | 9 | //-------------------------------------------------------------------- |
MikamiUitOpen | 0:85348d58f498 | 10 | |
MikamiUitOpen | 0:85348d58f498 | 11 | #include "mbed.h" |
MikamiUitOpen | 0:85348d58f498 | 12 | #include "pinmap.h" // pin_function() で使用 |
MikamiUitOpen | 0:85348d58f498 | 13 | |
MikamiUitOpen | 0:85348d58f498 | 14 | #ifndef DAC_BASE |
MikamiUitOpen | 0:85348d58f498 | 15 | #error DAC not built in. |
MikamiUitOpen | 0:85348d58f498 | 16 | #endif |
MikamiUitOpen | 0:85348d58f498 | 17 | |
MikamiUitOpen | 0:85348d58f498 | 18 | #ifndef DSP_DAC_HPP |
MikamiUitOpen | 0:85348d58f498 | 19 | #define DSP_DAC_HPP |
MikamiUitOpen | 0:85348d58f498 | 20 | |
MikamiUitOpen | 0:85348d58f498 | 21 | namespace Mikami |
MikamiUitOpen | 0:85348d58f498 | 22 | { |
MikamiUitOpen | 0:85348d58f498 | 23 | class DspDac |
MikamiUitOpen | 0:85348d58f498 | 24 | { |
MikamiUitOpen | 0:85348d58f498 | 25 | public: |
MikamiUitOpen | 0:85348d58f498 | 26 | // コンストラクタ |
MikamiUitOpen | 0:85348d58f498 | 27 | explicit DspDac(PinName pin = A2); |
MikamiUitOpen | 0:85348d58f498 | 28 | |
MikamiUitOpen | 0:85348d58f498 | 29 | virtual ~DspDac() {} |
MikamiUitOpen | 0:85348d58f498 | 30 | |
MikamiUitOpen | 0:85348d58f498 | 31 | // -1.0f <= data <= 1.0f |
MikamiUitOpen | 0:85348d58f498 | 32 | void Write(float data) { WriteDac(ToUint16(data)); } |
MikamiUitOpen | 0:85348d58f498 | 33 | |
MikamiUitOpen | 0:85348d58f498 | 34 | // 0 <= data <= 4095 |
MikamiUitOpen | 0:85348d58f498 | 35 | void Write(uint16_t data) { WriteDac(__USAT(data, BIT_WIDTH_)); } |
MikamiUitOpen | 0:85348d58f498 | 36 | |
MikamiUitOpen | 0:85348d58f498 | 37 | private: |
MikamiUitOpen | 0:85348d58f498 | 38 | void (DspDac::*fpWrite_)(uint16_t); |
MikamiUitOpen | 0:85348d58f498 | 39 | |
MikamiUitOpen | 0:85348d58f498 | 40 | static const int BIT_WIDTH_ = 12; |
MikamiUitOpen | 0:85348d58f498 | 41 | DAC_TypeDef* const DAC_; // DA 変換器に対応する構造体のポインタ |
MikamiUitOpen | 0:85348d58f498 | 42 | static bool created_; // このクラスのオブジェクトの複数生成禁止で使用 |
MikamiUitOpen | 0:85348d58f498 | 43 | |
MikamiUitOpen | 0:85348d58f498 | 44 | // DAC の片方のチェンネルへ出力する |
MikamiUitOpen | 0:85348d58f498 | 45 | void WriteDac1(uint16_t val) { DAC->DHR12R1 = val; } // CH1 へ |
MikamiUitOpen | 0:85348d58f498 | 46 | void WriteDac2(uint16_t val) { DAC->DHR12R2 = val; } // CH2 へ |
MikamiUitOpen | 0:85348d58f498 | 47 | |
MikamiUitOpen | 0:85348d58f498 | 48 | void WriteDac(uint16_t val) { (this->*fpWrite_)(val); } |
MikamiUitOpen | 0:85348d58f498 | 49 | |
MikamiUitOpen | 0:85348d58f498 | 50 | // 飽和処理を行い uint16_t 型のデータを戻り値とする |
MikamiUitOpen | 0:85348d58f498 | 51 | uint16_t ToUint16(float val) |
MikamiUitOpen | 0:85348d58f498 | 52 | { return __USAT((val + 1.0f)*2048.0f, BIT_WIDTH_); } |
MikamiUitOpen | 0:85348d58f498 | 53 | |
MikamiUitOpen | 0:85348d58f498 | 54 | // コピー・コンストラクタ,代入演算子の禁止のため |
MikamiUitOpen | 0:85348d58f498 | 55 | DspDac(const DspDac&); |
MikamiUitOpen | 0:85348d58f498 | 56 | DspDac& operator=(const DspDac&); |
MikamiUitOpen | 0:85348d58f498 | 57 | }; |
MikamiUitOpen | 0:85348d58f498 | 58 | } |
MikamiUitOpen | 0:85348d58f498 | 59 | #endif // DSP_DAC_HPP |