Skip to main content
RIzqu.1
Associate
March 31, 2020
Question

STM32L432KC I want to read ADC values using DMA. The problem is that I don't get any value in my array. I would like to do with HAL_ADC_PollForConversion, but doesn't work correctly it seems to read only one channel.

  • March 31, 2020
  • 6 replies
  • 3863 views

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "fatfs.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "fatfs_sd.h"

#include "string.h"

#include "stm32l4xx_hal_adc.h"

/* Private variables ---------------------------------------------------------*/

ADC_HandleTypeDef hadc1;

DMA_HandleTypeDef hdma_adc1;

SPI_HandleTypeDef hspi1;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_DMA_Init(void);

static void MX_ADC1_Init(void);

static void MX_SPI1_Init(void);

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/*Variables for using FATFS.*/

FATFS fs;  //file system

FIL fil; //file

FRESULT fresult; //to store the result

char buffer[1024]; //to store data

UINT br, bw; //file read/write count

FATFS *pfs;

DWORD fre_clust;

uint32_t total, free_space;

/*Variables */

uint16_t vector1[2500]={0};

uint16_t adc_data[2];

uint16_t vector2[2500]={0};

uint8_t i = 0;

uint16_t a = 0;

uint16_t mayor1 = 0;

uint16_t b = 0;

uint16_t mayor2 = 0;

//HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_data, 2);

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)

{

__NOP(); //I am using this function to use a breakpoint, and it never reaches the // //Conversion complete Callback

}

/* USER CODE END 0 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 HAL_ADC_Init(&hadc1);

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_ADC1_Init();

 MX_SPI1_Init();

 MX_FATFS_Init();

 /* USER CODE BEGIN 2 */

 /* Mount SD Card */

   fresult = f_mount(&fs, "", 0);

   /*************** Card capacity details ********************/

     /* Check free space */

     f_getfree("", &fre_clust, &pfs);

     total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);

     free_space = (uint32_t)(fre_clust * pfs->csize * 0.5);

HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_data, 2);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 while (i<50)

 {

//  HAL_ADC_Start(&hadc1);

//  HAL_ADC_PollForConversion(&hadc1, 50);

//  a = HAL_ADC_GetValue(&hadc1);

//  HAL_ADC_PollForConversion(&hadc1, 100);

//  b = HAL_ADC_GetValue(&hadc1);

//  HAL_ADC_Stop(&hadc1);

 vector1[i]= adc_data[0];

 vector2[i]= adc_data[1];

 if(vector1[i] > mayor1) mayor1 = vector1[i];

 if(vector2[i] > mayor2) mayor2 = vector2[i];

 i++;

 HAL_Delay(100);

 }

This topic has been closed for replies.

6 replies

TDK
Super User
April 1, 2020

How are you triggering the ADC conversions?

"If you feel a post has answered your question, please click ""Accept as Solution""."
RIzqu.1
RIzqu.1Author
Associate
April 1, 2020

I am triggering the ADC conversions with the option "Regular Conversion Launched by Software". Is it correct?

Thanks for your help.

Imen GH
ST Employee
April 1, 2020

Hello,

Please find attached the example (when using Nucleo-L476RG) which helps you to solve your problem.

For this example, I already read 2 analog inputs by ADC using DMA:

I used: PC1    ------> ADC1_IN2

            PC2    ------> ADC1_IN3

You will find attached my file main. c

Regards

RIzqu.1
RIzqu.1Author
Associate
April 1, 2020

Excuse me, I have checked both files and I have the same configuration for ADC and DMA, I can't find where my mistake could be. I don't know why my program does not fire the HAL_ADC_ConvCpltCallback function, It seems that DMA doesn't get the values. I am going to attach my main.c and my hal_msp.c

I hope you can help me with this problem.

Thank you very much.

Imen GH
ST Employee
April 1, 2020

Here attached my file STM32L4xx_hal_msp.c

RIzqu.1
RIzqu.1Author
Associate
April 1, 2020

Here is my HAL_MSP file

chaaalyy
Senior II
April 1, 2020

I believe, it´s like TDK already said: The problem lies in triggering the conversion ... Take a look here:

https://www.youtube.com/watch?v=EbWOv_0Lp-U

Just forget about the TGFX stuff and see, how he is handling the ADC :) (also check out the comments, if you get some compiler warnings afterwards ...)

/Charly

RIzqu.1
RIzqu.1Author
Associate
April 1, 2020

Hello!

I am triggering the ADC conversions with the option "Regular Conversion Launched by Software". And I am using the method explained in the next video from the STMicroelectronics youtube channel https://www.youtube.com/watch?v=BS4Wh0qvOkc

He should have the same problem I think. What do you think?

Even though I will take a look to your proposal, thank you very much =)

chaaalyy
Senior II
April 1, 2020

I just took a quick look into your main.c ...

1st thing i´ve seen is the call to "HAL_ADC_Init(&hadc1);" just below HAL_Init();. I think, this can be deleted, because this call is already part of the function MX_ADC1_Init();, which is called a little bit later ...

Further differences i´ve seen are :

hadc1.Init.ClockPrescaler

hadc1.Init.ScanConvMode

hadc1.Init.NbrOfConversion

hadc1.Init.ExternalTrigConv

(surely not complete ... =) )

I´m not too familiar with ADC, i used it just once (like the guy from the video did...) for reading the percentage of a connected potentiometer

Imen GH
ST Employee
April 2, 2020

​Hello @Rubén Izquierdo​ ,

I made a brief comparison between my main.c file and your file. it's true that, we have the same configuration (but the order is not the same). In your main.C file, I confirm that you must remove the HAL_ADC_Init (& hadc1) just after the HAL_Init () ('already HAL_ADC_Init (& hadc1)' you will find it later in the configuration section of the ADC)

I already did the test on my side (as shown in the figure) no problem was detected.

Best regards,

Imen