Library for build-in ADC and DAC in STM32F446 mounted on Nucleo F446RE. Nucleo F446RE に搭載されている STM32F446 の内蔵 ADC, DAC 用のライブラリ.

Dependents:   Demo_F446_AD_DA F446_MySoundMachine F446_ADF_Nlms F446_Spectrogram

F446_ADC.hpp

Committer:
MikamiUitOpen
Date:
2017-02-21
Revision:
6:bbc26cf86b70
Parent:
4:16667bdb8227

File content as of revision 6:bbc26cf86b70:

//----------------------------------------------------------
//  Simultanuous AD Conversion by polling using
//  ADC2 and ADC3 on STM32F446 ---- Header
//
//  STM32F446 の ADC2, ADC3 を使って同時に AD 変換を開始し,
//  ポーリングによりアナログ信号を入力するクラス(ヘッダ)
//      A0 (PA_0) :  ADC2 CH0, 左
//      A1 (PA_1) :  ADC3 CH1, 右
//  Read(), Write() の引数:
//      第一引数:A0 (左),第二引数:A1 (右)
//
//  2017/02/16, Copyright (c) 2017 MIKAMI, Naoki
//----------------------------------------------------------

#include "mbed.h"

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

#include "F446_DAC.hpp"

#ifndef F446_ADC_DUAL_HPP
#define F446_ADC_DUAL_HPP

namespace Mikami
{
    class AdcDual
    {
    public:
        // Constructor
        //      frequency: 標本化周波数
        explicit AdcDual(int frequency);
        
        virtual ~AdcDual() {}

        // -1.0f <= ad1, ad2 <= 1.0f
        //      ad1: left, ad2: right
        virtual void Read(float &ad1, float &ad2);

        // 0 <= ad1, ad2 <= 4095
        //      ad1: left, ad2: right
        virtual void Read(uint16_t &ad1, uint16_t &ad2);

    protected:
        float ToFloat(uint16_t x)
        {   return AMP_*(x - 2048); }
    
    private:
        static const float AMP_ = 1.0f/2048.0f;
        static const uint32_t EOC23_ = ADC_CSR_EOC2 | ADC_CSR_EOC3;
        
        // AD 変換が完了するまで待つ
        void WaitDone()
        {   while((ADC->CSR & EOC23_) != EOC23_); }

        // AD 変換器の外部トリガに使うタイマ (TIM8) の設定
        void SetTim8(int frequency);

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