NUCLEO- STM32F401RE板上的ADC在输入信号的10kHz以上无法正常工作

时间:2019-06-05 05:24:37

标签: stm32 adc nucleo

Plot of 60-80kHz using continuous conversion- DMA Edit 1:Image of 10kHz to 50kHz input signal using continuous conversion 我正在NUCLEO- STM32F401RE板上使用ADC。我已经使用中断对ADC进行了编程,该中断将在数组中存储大约10,000个值,并使用.ini文件读取它们。该代码不能在输入信号的10kHz以上工作。 ADC具有12位分辨率和3个采样周期; ADC时钟频率为21Khz。下面是我的代码和1-200Khz正弦波输入频率范围的值图。输入电压为1.3V。我想念什么?

#include "main.h"
#include "stm32f4xx_hal.h" 
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);

uint32_t adc_data[10000];
uint32_t i=0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{   if (i<10000) {
adc_data[i]= HAL_ADC_GetValue(&hadc1);
    i++;
HAL_ADC_Start_IT(&hadc1);
}
    else {
    HAL_ADC_Stop_IT(&hadc1);
}
}

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
HAL_ADC_Start_IT(&hadc1);

while (1)
  {

  }

}


void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /**Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 16;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                          |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

static void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};

  /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of     conversion) 
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and              its sample time. 
   */
   sConfig.Channel = ADC_CHANNEL_0;
   sConfig.Rank = 1;
   sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
   {
     Error_Handler();
   }
 }

 static void MX_GPIO_Init(void)
 { 
   /* GPIO Ports Clock Enable */
   __HAL_RCC_GPIOA_CLK_ENABLE();

 }

 void Error_Handler(void)
 {

 }

 #ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{ 
}
#endif /* USE_FULL_ASSERT */

plot of values for input frequency range of 1- 200Khz

2 个答案:

答案 0 :(得分:1)

我假设21 kHz是一个错字,并且ADC在APB2 / 4 = 21 MHz上运行。采样时间为3个周期,分辨率为12位的转换时间为12个周期,因此ADC的采样频率为= 1.4 MHz 21/(12+3))。当核心以84 MHz运行时,每60个核心周期采样一次。

60个周期几乎不足以开始执行中断处理程序,同时考虑了闪存等待状态。

查看您所附的图,它看起来像实际采样频率是300 kHz (在10 kHz信号的整个周期中有30个采样),即3.33 µs或25200时钟周期。考虑到ridiculous amount of overhead in HAL,这似乎很现实。

您的代码进行一次转换,并在完成时触发中断,然后读取并存储该值,然后指示ADC开始下一次转换,并引入了可变延迟,该延迟取决于几个难以预测的因素

使用连续转换

ADC可以在完成最后一个覆盖后或由计时器触发后立即重新启动覆盖(或序列,如果存在一个),请参见EXTSELEXTEN的说明参考手册中控制寄存器的{,SCANCONT位。每次转换后,它甚至可以触发DMA传输,因此您可以设置DMA通道以将读数存储到缓冲区中。这样即使在最高采样频率下也可以保证均匀的采样间隔。

答案 1 :(得分:0)

请关注Nyquist Frequency

简而言之: ADC频率为21kHz时,您可以以7kHz采样输入,这意味着您的 最高可能的信号频率为〜3.5kHz。