不韋 呂
/
SDR_FM_Tx
AM中波放送用SDR.CICフィルタのみを使用.CQ出版社「トランジスタ技術」誌,2021年5月号に掲載
F446_DAC_Dual.hpp@0:7a653530c8ce, 2020-08-29 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sat Aug 29 11:26:29 2020 +0000
- Revision:
- 0:7a653530c8ce
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:7a653530c8ce | 1 | //-------------------------------------------------------- |
MikamiUitOpen | 0:7a653530c8ce | 2 | // STM32F446 内蔵の DAC 用のクラス |
MikamiUitOpen | 0:7a653530c8ce | 3 | // A2 (PA_4): 左 |
MikamiUitOpen | 0:7a653530c8ce | 4 | // D13 (PA_5): 右 |
MikamiUitOpen | 0:7a653530c8ce | 5 | // D13 は LD2 (緑) が接続されているため,高い電圧を出力した場合に |
MikamiUitOpen | 0:7a653530c8ce | 6 | // クリップされる.これを防止するためには SB21 を OFF にする |
MikamiUitOpen | 0:7a653530c8ce | 7 | // |
MikamiUitOpen | 0:7a653530c8ce | 8 | // 2020/08/11, Copyright (c) 2020 MIKAMI, Naoki |
MikamiUitOpen | 0:7a653530c8ce | 9 | //-------------------------------------------------------- |
MikamiUitOpen | 0:7a653530c8ce | 10 | |
MikamiUitOpen | 0:7a653530c8ce | 11 | #include "mbed.h" |
MikamiUitOpen | 0:7a653530c8ce | 12 | |
MikamiUitOpen | 0:7a653530c8ce | 13 | #ifndef STM32F446xx |
MikamiUitOpen | 0:7a653530c8ce | 14 | #error Select NUCLEO-F446RE. |
MikamiUitOpen | 0:7a653530c8ce | 15 | #endif |
MikamiUitOpen | 0:7a653530c8ce | 16 | |
MikamiUitOpen | 0:7a653530c8ce | 17 | #ifndef F446_DAC_DUAL_HPP |
MikamiUitOpen | 0:7a653530c8ce | 18 | #define F446_DAC_DUAL_HPP |
MikamiUitOpen | 0:7a653530c8ce | 19 | |
MikamiUitOpen | 0:7a653530c8ce | 20 | namespace Mikami |
MikamiUitOpen | 0:7a653530c8ce | 21 | { |
MikamiUitOpen | 0:7a653530c8ce | 22 | class DacDual |
MikamiUitOpen | 0:7a653530c8ce | 23 | { |
MikamiUitOpen | 0:7a653530c8ce | 24 | public: |
MikamiUitOpen | 0:7a653530c8ce | 25 | // コンストラクタ |
MikamiUitOpen | 0:7a653530c8ce | 26 | DacDual() : da1_(PA_4), da2_(PA_5) |
MikamiUitOpen | 0:7a653530c8ce | 27 | { |
MikamiUitOpen | 0:7a653530c8ce | 28 | DAC->CR = DAC_CR_EN1 | DAC_CR_TEN1 | DAC_CR_TSEL1 | |
MikamiUitOpen | 0:7a653530c8ce | 29 | DAC_CR_EN2 | DAC_CR_TEN2 | DAC_CR_TSEL2; |
MikamiUitOpen | 0:7a653530c8ce | 30 | } |
MikamiUitOpen | 0:7a653530c8ce | 31 | |
MikamiUitOpen | 0:7a653530c8ce | 32 | virtual ~DacDual() {} |
MikamiUitOpen | 0:7a653530c8ce | 33 | |
MikamiUitOpen | 0:7a653530c8ce | 34 | // -1.0f <= data1, data2 <= 1.0f |
MikamiUitOpen | 0:7a653530c8ce | 35 | // data1: 左, data2: 右 |
MikamiUitOpen | 0:7a653530c8ce | 36 | void Write(float data1, float data2) |
MikamiUitOpen | 0:7a653530c8ce | 37 | { WriteDac(ToUint16(data1), ToUint16(data2)); } |
MikamiUitOpen | 0:7a653530c8ce | 38 | |
MikamiUitOpen | 0:7a653530c8ce | 39 | // 0 <= data1, data2 <= 4095 |
MikamiUitOpen | 0:7a653530c8ce | 40 | // data1: 左, data2: 右 |
MikamiUitOpen | 0:7a653530c8ce | 41 | void Write(uint16_t data1, uint16_t data2) |
MikamiUitOpen | 0:7a653530c8ce | 42 | { WriteDac(__USAT(data1, BIT_WIDTH_), |
MikamiUitOpen | 0:7a653530c8ce | 43 | __USAT(data2, BIT_WIDTH_)); } |
MikamiUitOpen | 0:7a653530c8ce | 44 | |
MikamiUitOpen | 0:7a653530c8ce | 45 | private: |
MikamiUitOpen | 0:7a653530c8ce | 46 | static const uint32_t SWTRIG_ = DAC_SWTRIGR_SWTRIG1 |
MikamiUitOpen | 0:7a653530c8ce | 47 | | DAC_SWTRIGR_SWTRIG2; |
MikamiUitOpen | 0:7a653530c8ce | 48 | static const int BIT_WIDTH_ = 12; |
MikamiUitOpen | 0:7a653530c8ce | 49 | AnalogOut da1_, da2_; |
MikamiUitOpen | 0:7a653530c8ce | 50 | |
MikamiUitOpen | 0:7a653530c8ce | 51 | // 両方のチャンネルへ出力する |
MikamiUitOpen | 0:7a653530c8ce | 52 | void WriteDac(uint16_t val1, uint16_t val2) |
MikamiUitOpen | 0:7a653530c8ce | 53 | { |
MikamiUitOpen | 0:7a653530c8ce | 54 | DAC->DHR12RD = __PKHBT(val1, val2, 16); |
MikamiUitOpen | 0:7a653530c8ce | 55 | DAC->SWTRIGR = SWTRIG_; |
MikamiUitOpen | 0:7a653530c8ce | 56 | } |
MikamiUitOpen | 0:7a653530c8ce | 57 | |
MikamiUitOpen | 0:7a653530c8ce | 58 | // 飽和処理を行い uint16_t 型のデータを戻り値とする |
MikamiUitOpen | 0:7a653530c8ce | 59 | uint16_t ToUint16(float val) |
MikamiUitOpen | 0:7a653530c8ce | 60 | { |
MikamiUitOpen | 0:7a653530c8ce | 61 | return __USAT((val + 1.0f)*2047.0f, BIT_WIDTH_); |
MikamiUitOpen | 0:7a653530c8ce | 62 | } |
MikamiUitOpen | 0:7a653530c8ce | 63 | |
MikamiUitOpen | 0:7a653530c8ce | 64 | // コピー・コンストラクタ,代入演算子の禁止のため |
MikamiUitOpen | 0:7a653530c8ce | 65 | DacDual(const DacDual&); |
MikamiUitOpen | 0:7a653530c8ce | 66 | DacDual& operator=(const DacDual&); |
MikamiUitOpen | 0:7a653530c8ce | 67 | }; |
MikamiUitOpen | 0:7a653530c8ce | 68 | } |
MikamiUitOpen | 0:7a653530c8ce | 69 | #endif // F446_DAC_DUAL_HPP |