2020-03-31 04:45 PM
/* 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);
}
2020-03-31 07:43 PM
How are you triggering the ADC conversions?
2020-04-01 02:52 AM
2020-04-01 02:53 AM
2020-04-01 04:01 AM
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
2020-04-01 04:49 AM
I am triggering the ADC conversions with the option "Regular Conversion Launched by Software". Is it correct?
Thanks for your help.
2020-04-01 05:19 AM
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.
2020-04-01 05:19 AM
2020-04-01 05:46 AM
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 =)
2020-04-01 10:53 AM
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