Nucleo-F446RE 内蔵の AD/DA を使うためのライブラリ.DA からの出力は,標本化周波数の4倍のレートで行う.  このライブラリを登録した際のプログラム: Demo_F446_AD_DA_Multirate. Library for built-in ADC and DAC in Nucleo-F446RE. Sampling rate for DAC is four times of that for ADC.

Dependencies:   Array_Matrix

Dependents:   F446_UpSampling_GraphicEqualizer F446_UpSampling_ReverbSystem F446_UpSampling_FrqShifter_Weaver Demo_F446_AD_DA_Multirate ... more

F446_DAC.hpp

Committer:
MikamiUitOpen
Date:
2020-02-09
Revision:
9:75bc15678d1b
Parent:
8:9429fb179c38

File content as of revision 9:75bc15678d1b:

//--------------------------------------------------------
//  Class for buit-in single DAC on STM32F446 ---- Header
//
//  STM32F446 内蔵の DAC 用のクラス(ヘッダ)
//      選択可能な入力端子:
//          A2  (PA_4): ---- デフォルト
//          D13 (PA_5): このポートはマイコンボードの LED もドライブするので
//                       このポートは使わない方がよい
//
//  2018/11/28, Copyright (c) 2018 MIKAMI, Naoki
//--------------------------------------------------------

#include "mbed.h"

#ifndef STM32F446xx
#error Not NUCLEO-F446RE.
#endif

#ifndef F446_DAC_SINGLE_HPP
#define F446_DAC_SINGLE_HPP

namespace Mikami
{
    class DacF446
    {
    public:
        // Constructor
        explicit DacF446(PinName pin = A2);

        virtual ~DacF446() {}

        // -1.0f <= data <= 1.0f
        void Write(float data) { WriteDac(ToUint16(data)); }

        // 0 <= data <= 4095
        void Write(uint16_t data) { WriteDac(__USAT(data, BIT_WIDTH_)); }

    private:
        void (DacF446::*fpWriteDac)(uint16_t);

        static const int BIT_WIDTH_ = 12;
        AnalogOut da_;

        // DAC の片方のチェンネルへ出力する
        void WriteDac1(uint16_t val) { DAC->DHR12R1 = val; }    // CH1 へ
        void WriteDac2(uint16_t val) { DAC->DHR12R2 = val; }    // CH2 へ
        
        void WriteDac(uint16_t val) { (this->*fpWriteDac)(val); }

        // 飽和処理を行い uint16_t 型のデータを戻り値とする
        uint16_t ToUint16(float val)
        {   return __USAT((val + 1.0f)*2048.0f, BIT_WIDTH_); }

        // for inhibition of copy constructor
        DacF446(const DacF446&);
        // for inhibition of substitute operator
        DacF446& operator=(const DacF446&);     
    };
}
#endif  // F446_DAC_SINGLE_HPP