Added one channel mode
Dependents: CW_Decoder_using_FFT_on_F446
Fork of F446_AD_DA by
Diff: F446_ADC.cpp
- Revision:
- 4:03e91e464ce5
- Parent:
- 2:29f9831ce719
diff -r d1da91aec62f -r 03e91e464ce5 F446_ADC.cpp --- a/F446_ADC.cpp Mon Jan 30 08:12:44 2017 +0000 +++ b/F446_ADC.cpp Sun Feb 05 07:59:14 2017 +0000 @@ -9,6 +9,14 @@ // // 2016/11/12, Copyright (c) 2016 MIKAMI, Naoki //---------------------------------------------------------- +/* + * Modified by Kenji Arai + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * + * Started: Feburary 1st, 2017 + * Revised: Feburary 1st, 2017 + */ #include "F446_ADC.hpp" @@ -89,4 +97,73 @@ myTim->PSC = psc; // Prescaler myTim->CR1 = TIM_CR1_CEN; // Enable TIM8 } + +//------------------------------------------------------------------------------ + + AdcSingle::AdcSingle(int frequency) + { + // ADC input is assigned PA0 + __HAL_RCC_GPIOA_CLK_ENABLE(); + GPIO_InitTypeDef gpioInit; + gpioInit.Pin = GPIO_PIN_0; + gpioInit.Mode = GPIO_MODE_ANALOG; + gpioInit.Pull = GPIO_NOPULL; + gpioInit.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + HAL_GPIO_Init(GPIOA, &gpioInit); + + // ADC2 (port:PA0) + __HAL_RCC_ADC2_CLK_ENABLE(); + ADC2->CR1 = 0x0; + ADC2->CR2 = ADC_EXTERNALTRIGCONVEDGE_RISING // Ext. trigger + rising eggde + | ADC_EXTERNALTRIGCONV_T8_TRGO // Ext. trigger Timer8 TRGO event + | ADC_CR2_ADON; // ADC を有効にする + ADC2->SQR3 = 0x0; // CH0 を使う + + // ADC 共通の設定 + ADC->CCR = 0x0; // 念のため + + // AD 変換器の外部トリガに使うタイマ (TIM8) の設定 + SetTim8(frequency); + } + + void AdcSingle::Read(float &ad) + { + WaitDone(); + ad = ToFloat(ADC2->DR); + } + + void AdcSingle::Read(uint16_t &ad) + { + WaitDone(); + ad = ADC2->DR; + } + + void AdcSingle::SetTim8(int frequency) + { + __HAL_RCC_TIM8_CLK_ENABLE(); // Supply clock. See "stm32f4xx_hal_rcc.h" + SystemCoreClockUpdate(); // Update core clock. See "system_stm32f4xx.h" + TIM_TypeDef* const myTim = TIM8; + + myTim->CR2 = TIM_CR2_MMS_1; // Update event: as trigger out + + uint32_t psc = 0; + uint16_t mul = 1; + uint32_t arr; + while (true) + { + arr = SystemCoreClock/(mul*frequency); + if (arr <= 65536) break; + psc++; + mul++; + if (psc > 65535) + { + fprintf(stderr, "Sampling frequency: too low.\r\n"); + while (true) {} + } + } + myTim->ARR = arr - 1; // Auto-reload + myTim->PSC = psc; // Prescaler + myTim->CR1 = TIM_CR1_CEN; // Enable TIM8 + } } +