不韋 呂 / 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.cpp Source File

ADC_BuiltIn.cpp

00001 //------------------------------------------------------
00002 // Class for ADC using TIM2 trigger
00003 //      To get bit definition for register in
00004 //      peripheral, see "stm32f401xe.h"
00005 //
00006 // 2015/04/19, Copyright (c) 2015 MIKAMI, Naoki
00007 //------------------------------------------------------
00008 
00009 #include "ADC_BuiltIn.hpp"
00010 
00011 namespace Mikami
00012 {
00013     ADC_BuiltIn::ADC_BuiltIn(PinName pin1, int frequency,
00014                              PinName pin2, PinName pin3)
00015         : adc_(pin1), myAdc_(ADC1)
00016     {
00017         myAdc_->CR2 = ADC_EXTERNALTRIGCONVEDGE_RISING   // External Trigger on the rising edge
00018                     | ADC_EXTERNALTRIGCONV_T2_TRGO      // Use Timer2 TRGO event
00019                     | ADC_CR2_ADON;                     // Enable ADC
00020 
00021         ch1_ = GetChannelNumber(pin1);
00022         if (pin2 != NC)
00023         {
00024             adc2_ = new AnalogIn(pin2);
00025             ch2_ = GetChannelNumber(pin2);
00026         }
00027         if (pin3 != NC)
00028         {
00029             adc3_ = new AnalogIn(pin3);
00030             ch3_ = GetChannelNumber(pin3);
00031         }
00032         SetTim2(frequency);
00033         Select1stChannel();
00034     }
00035 
00036     // Extract channel number
00037     uint8_t ADC_BuiltIn::GetChannelNumber(PinName pin)
00038     {
00039         uint8_t ch = 0;
00040         if ((pin & 0x30) == 0x00) ch = pin;
00041         if ((pin & 0x30) == 0x10) ch = (pin & 0x01) + 8;
00042         if ((pin & 0x30) == 0x20) ch = (pin & 0x07) + 10;
00043         return ch;
00044     }
00045 
00046     void ADC_BuiltIn::SetTim2(int frequency)
00047     {
00048         __TIM2_CLK_ENABLE();    // Supply clock, See "stm32f4xx_hal_rcc.h"
00049         
00050         SystemCoreClockUpdate();    // Update core clock (for F411RE)
00051                                     // See system_stm32f4xx.h
00052         TIM_TypeDef* const myTim = TIM2;
00053 
00054         myTim->CR2 = TIM_CR2_MMS_1; // Update event: as trigger out
00055         myTim->ARR = SystemCoreClock/frequency - 1; // Auto-reload
00056         myTim->PSC = 0;             // Prescaler
00057         myTim->CR1 = TIM_CR1_CEN;   // Enable TIM2
00058     }
00059 }