cancel
Showing results for 
Search instead for 
Did you mean: 

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.

RIzqu.1
Associate II

/* 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);

 }

10 REPLIES 10
TDK
Guru

How are you triggering the ADC conversions?

If you feel a post has answered your question, please click "Accept as Solution".
Imen GH
ST Employee

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

Imen GH
ST Employee

Here attached my file STM32L4xx_hal_msp.c

chaaalyy
Senior II

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

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

Thanks for your help.

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.

Here is my HAL_MSP file

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

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