不韋 呂 / UITDSP_ADDA

Dependents:   UITDSP_ADDA_Example UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADC_BuiltIn.hpp Source File

ADC_BuiltIn.hpp

00001 //------------------------------------------------------
00002 // Class for ADC using TIM2 trigger -- Header
00003 //
00004 // 2015/04/19, Copyright (c) 2015 MIKAMI, Naoki
00005 //------------------------------------------------------
00006 
00007 #ifndef ADC_BUILTIN_HPP
00008 #define ADC_BUILTIN_HPP
00009 
00010 #include "mbed.h"
00011 
00012 namespace Mikami
00013 {
00014     class ADC_BuiltIn
00015     {
00016     private:
00017         // Following object of AnalogIn class will be
00018         // initialized by menber initializer
00019         AnalogIn adc_;
00020         // Following two objects of AnalogIn class will be
00021         // initailized by regular executable statements
00022         AnalogIn* adc2_;
00023         AnalogIn* adc3_;
00024 
00025         // Channel of ADC1
00026         uint8_t ch1_, ch2_, ch3_;
00027 
00028         // Set timer to generate sampling pulse for ADC
00029         void SetTim2(int frequency);
00030         
00031         // Exctract channel number
00032         uint8_t GetChannelNumber(PinName);
00033 
00034         // for inhibition of copy constructor
00035         ADC_BuiltIn(const ADC_BuiltIn&);
00036         // for inhibition of substitute operator
00037         ADC_BuiltIn& operator=(const ADC_BuiltIn&);
00038 
00039     protected:
00040         // for normalize   
00041         static const float AMP_ = 1.0f/2048.0f;
00042 
00043         ADC_TypeDef* const myAdc_;
00044 
00045         // Wait until completion of AD conversion
00046         void WaitDone()
00047         { while((myAdc_->SR & ADC_SR_EOC) == RESET); }
00048 
00049     public:
00050 
00051         // Constructor
00052         //      pin1:       Pin Name for input as A0, A1, etc.
00053         //      frequency:  Sampling frequency
00054         //      pin2:       If use 2nd channel, set this parameter
00055         //      pin3:       If use 3rd channel, set this parameter
00056         ADC_BuiltIn(PinName pin1, int frequency,
00057                  PinName pin2 = NC, PinName pin3 = NC);  
00058         
00059         // Read ADC with waiting, range: [0, 0x0FFF]
00060         virtual uint16_t Read_u16()
00061         {
00062             WaitDone();
00063             return myAdc_->DR;   
00064         }
00065 
00066         // Read ADC with waiting, range: [-1.0f, 1.0f]
00067         virtual float Read()
00068         {
00069             WaitDone();
00070             return AMP_*((int16_t)myAdc_->DR - 2048);
00071         }
00072 
00073         // Select channel
00074         void Select1stChannel() { myAdc_->SQR3 = ch1_; }
00075         void Select2ndChannel() { myAdc_->SQR3 = ch2_; }
00076         void Select3rdChannel() { myAdc_->SQR3 = ch3_; }
00077     };
00078 }
00079 #endif  // ADC_BUILTIN_HPP