cancel
Showing results for 
Search instead for 
Did you mean: 

Get data from ADC

PhilippeN
Associate

Hello,

My aim is to get data from two ADC channels in order to extrat data into a file. I want then to analyze data with Matlab.

For now I achieve to see the two signals in my debugger but I can't manage to extract them from there.

I am using a Nucleo F411RE demoboard with TrueStudio. The initial code have been generated with CubeMX.

The signals are squared with a 37kHz frequency, so that I want to acquire with a minimum sampling frequency of 300kHz.

I wanted to use fopen, write, fclose to put data into a file but it seems not to work. I don't know why, I can't find the file if it is created or not.

Here is my main.c code

******************************************************************************
  //* @file           : main.c
  //* @brief          : Main program body
  ******************************************************************************
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
 
 
/* Private variables ---------------------------------------------------------*/
uint8_t channel=1;
uint32_t adc[2], buffer[2];
uint16_t tab1[20000];
uint16_t tab2[20000];
uint32_t n1=0;
uint32_t n2=0;
float vsense=3300/4095;
 
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
 
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
 
void SystemClock_Config(void);
 
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
 
	adc[channel]=buffer[0];
	if (channel == (2-1) ){
		tab2[n2]=adc[channel];
		n2++;
		channel = 0;
	}
	else{
		tab1[n1]=adc[channel];
		n1++;
		channel++;
	}
}
 
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_DMA_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
 
  HAL_ADC_Start_DMA(&hadc1, buffer, 1);
  HAL_ADC_Start_IT(&hadc1);
 
 
 
  /*
  FILE *pFile;
  pFile= fopen("test.txt", "w");
  fprintf(pFile, "Hello file");
  if (pFile != NULL)
  {
	  fclose(pFile);
  }
  else
  {
	  printf("Could not open the file. \n");
  }
	*/
 
 
  while (1)
  {
	  HAL_ADC_Start_IT(&hadc1);
	  HAL_Delay(1);
  }
}
 
void SystemClock_Config(void)
{
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
    /**Configure the main internal regulator output voltage 
    */
  __HAL_RCC_PWR_CLK_ENABLE();
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 200;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    /**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_3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 

2 REPLIES 2

Choose only 1 from

  1. HAL_ADC_Start_DMA(&hadc1, buffer, 1);
  2. HAL_ADC_Start_IT(&hadc1);

PhilippeN
Associate

Yes, it's working.

Actually I put the data into two tabs tab1 and tab2. I want to extract them to a file .txt or .bin but the functions fopen, fclose don't semm to work with TrueSudio.

Another thing, I would like to know the speed of putting data into tabs. I know that my acquisition speed from the ADC is about 1,6 Msps but after this I don't know anything.

Thanks,

Philippe