Class library for internal ADC and DAC connected by SPI. ADC is triggered by TIM2. This library support clock generator using TIM3 for switched-capacitor filter to smooth output signal of DAC. This library includes derivative class to support interrupt occured in end of AD conversion. Slave select of SPI for DAC is generated using TIM4. Validated for ST Nucleo F401RE, F411RE. New version. 内蔵 ADC と,SPI 接続の DAC のためのクラスライブラリ.ADC の変換開始トリガは TIM2 で発生.DAC の出力信号を平滑化するためのスイッチトキャパシタフィルタ用のクロックは TIM3 を使用.DAC の SPI 用スレーブ選択信号は TIM4 で発生.ST Nucleo F401RE,F411RE で動作を確認.新バージョン

Dependents:   UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more

ADC_Base.hpp

Committer:
MikamiUitOpen
Date:
2015-02-02
Revision:
21:3731753ebf24
Parent:
15:ab7eb29b69ea

File content as of revision 21:3731753ebf24:

//------------------------------------------------------
// Class for ADC using TIM2 trigger -- Header
//
// 2014/12/31, Copyright (c) 2014 MIKAMI, Naoki
//------------------------------------------------------

#ifndef ADC_BASE_HPP
#define ADC_BASE_HPP

#include "mbed.h"

namespace Mikami
{
    class ADC_Base
    {
    private:
        // Following object of AnalogIn class will be
        // initialized by menber initializer
        AnalogIn adc_;
        // Following two objects of AnalogIn class will be
        // initailized by regular executable statements
        AnalogIn* adc2_;
        AnalogIn* adc3_;

        // Channel of ADC1
        uint8_t ch1_, ch2_, ch3_;

        // Set timer to generate sampling pulse for ADC
        void SetTim2(int frequency);
        
        // Exctract channel number
        uint8_t GetChannelNumber(PinName);

        // for inhibition of copy constructor
        ADC_Base(const ADC_Base&);
        // for inhibition of substitute operator
        ADC_Base& operator=(const ADC_Base&);

    protected:
        // for normalize   
        static const float AMP_ = 1.0f/2048.0f;

        ADC_TypeDef* const myAdc_;

        // Wait until completion of AD conversion
        void WaitDone()
        { while((myAdc_->SR & ADC_SR_EOC) == RESET); }

    public:

        // Constructor
        //      pin1:       Pin Name for input as A0, A1, etc.
        //      frequency:  Sampling frequency
        //      pin2:       If use 2nd channel set this parameter
        //      pin3:       If use 3rd channel set this parameter
        ADC_Base(PinName pin1, int frequency,
                 PinName pin2 = NC, PinName pin3 = NC);  
        
        // Read ADC with waiting, range: [0, 0x0FFF]
        virtual uint16_t Read_u16()
        {
            WaitDone();
            return myAdc_->DR;   
        }

        // Read ADC with waiting, range: [-1.0f, 1.0f]
        virtual float Read()
        {
            WaitDone();
            return AMP_*((int16_t)myAdc_->DR - 2048);
        }

        // Select channel
        void Select1stChannel() { myAdc_->SQR3 = ch1_; }
        void Select2ndChannel() { myAdc_->SQR3 = ch2_; }
        void Select3rdChannel() { myAdc_->SQR3 = ch3_; }
        
        // Software start
        virtual void SoftStart()
        { myAdc_->CR2 |= ADC_CR2_SWSTART; }
        
        uint32_t ReadSQR3() { return myAdc_->SQR3; }
    };
}
#endif  // ADC_BASE_HPP