不韋 呂 / UIT_ADDA

Dependents:   UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Fri Dec 19 06:01:15 2014 +0000
Parent:
12:479d18a09e87
Child:
14:6c60601c1834
Commit message:
14

Changed in this revision

ADC_Base.cpp Show annotated file Show diff for this revision Revisions of this file
ADC_Base.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/ADC_Base.cpp	Mon Dec 01 07:13:33 2014 +0000
+++ b/ADC_Base.cpp	Fri Dec 19 06:01:15 2014 +0000
@@ -3,7 +3,7 @@
 //      To get bit definition for register in
 //      peripheral, see "stm32f401xe.h"
 //
-// 2014/11/12, Copyright (c) 2014 MIKAMI, Naoki
+// 2014/12/19, Copyright (c) 2014 MIKAMI, Naoki
 //------------------------------------------------------
 
 #include "ADC_Base.hpp"
@@ -12,26 +12,44 @@
 {
     ADC_Base::ADC_Base(PinName pin1, int frequency,
                        PinName pin2, PinName pin3)
-        : adc_(pin1), PIN1_(pin1), PIN2_(pin2),
-          PIN3_(pin3), myAdc_(ADC1)
+        : adc_(pin1), /*PIN1_(pin1), PIN2_(pin2),
+          PIN3_(pin3), */myAdc_(ADC1)
     {
         myAdc_->CR2 = ADC_EXTERNALTRIGCONVEDGE_RISING   // External Trigger on the rising edge
                     | ADC_EXTERNALTRIGCONV_T2_TRGO      // Use Timer2 TRGO event
                     | ADC_CR2_ADON;                     // Enable ADC
 
-        if (pin2 != NC) adc2_ = new AnalogIn(pin2);
-        if (pin3 != NC) adc3_ = new AnalogIn(pin3);
+        ch1_ = GetChannel(pin1);
+        if (pin2 != NC)
+        {
+            adc2_ = new AnalogIn(pin2);
+            ch2_ = GetChannel(pin2);
+        }
+        if (pin3 != NC)
+        {
+            adc3_ = new AnalogIn(pin3);
+            ch3_ = GetChannel(pin3);
+        }
         SetTim2(frequency);
         Select1stChannel();
     }
 
+    // Extract channel number
+    uint8_t ADC_Base::GetChannel(PinName pin)
+    {
+        uint8_t ch = 0;
+        if ((pin & 0x30) == 0x00) ch = pin;
+        if ((pin & 0x30) == 0x10) ch = (pin & 0x01) + 8;
+        if ((pin & 0x30) == 0x20) ch = (pin & 0x07) + 10;
+        return ch;
+    }
+
     void ADC_Base::SetTim2(int frequency)
     {
         __TIM2_CLK_ENABLE();    // Supply clock, See "stm32f4xx_hal_rcc.h"
         
         SystemCoreClockUpdate();    // Update core clock (for F411RE)
                                     // See system_stm32f4xx.h
-
         TIM_TypeDef* myTim = TIM2;
 
         myTim->CR2 = TIM_CR2_MMS_1; // Update event: as trigger out
@@ -41,5 +59,3 @@
     }
 }
 
-
-
--- a/ADC_Base.hpp	Mon Dec 01 07:13:33 2014 +0000
+++ b/ADC_Base.hpp	Fri Dec 19 06:01:15 2014 +0000
@@ -1,7 +1,7 @@
 //------------------------------------------------------
 // Class for ADC using TIM2 trigger -- Header
 //
-// 2014/11/12, Copyright (c) 2014 MIKAMI, Naoki
+// 2014/12/19, Copyright (c) 2014 MIKAMI, Naoki
 //------------------------------------------------------
 
 #ifndef ADC_BASE_HPP
@@ -21,13 +21,16 @@
         // initailized by regular excutable statements
         AnalogIn* adc2_;
         AnalogIn* adc3_;
-        
-        // Pins for Analog input
-        const PinName PIN1_, PIN2_, PIN3_;
+
+        // 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 GetChannel(PinName);
+
         // for inhibition of copy constructor
         ADC_Base(const ADC_Base&);
         // for inhibition of substitute operator
@@ -44,6 +47,7 @@
         { while((myAdc_->SR & ADC_SR_EOC) == RESET); }
 
     public:
+
         // Constructor
         //      pin1:       Pin Name for input as A0, A1, etc.
         //      frequency:  Sampling frequency
@@ -67,9 +71,9 @@
         }
 
         // Select channel
-        void Select1stChannel() { myAdc_->SQR3 = PIN1_; }
-        void Select2ndChannel() { myAdc_->SQR3 = PIN2_; }
-        void Select3rdChannel() { myAdc_->SQR3 = PIN3_; }
+        void Select1stChannel() { myAdc_->SQR3 = ch1_; }
+        void Select2ndChannel() { myAdc_->SQR3 = ch2_; }
+        void Select3rdChannel() { myAdc_->SQR3 = ch3_; }
         
         // Software start
         virtual void SoftStart()
@@ -79,7 +83,3 @@
     };
 }
 #endif  // ADC_BASE_HPP
-
-
-
-