cancel
Showing results for 
Search instead for 
Did you mean: 

ADC not working at all - only output is "0"

RuanSanchez
Associate II

Hello,

I am trying to continuously read a voltage from a single channel by using the onboard ADC of the STM32L562ZET6 on a custom PCB. 

All the relevant analog voltages of the ADC have expected values, i.e. VREF+ =  3V, AVDD = 3V VREF- = 0V, AVSS = 0V. Also, the applied voltage signal (DC signal between 500mV and 2.5V is measured through the signal path on the PCB and does arrive at the respective Pin on the actual uC. So the Hardware is likely to work as expected.)

The following code uses HAL-functions to read from the channel and pass it through the DMA Handler. (I have tried many other ways, by using single conversion and polling, by using the interrupt and the callback functions...)


Still, the only output I get on the terminal is the value 0. 

The exact identical code runs on the STM32L433RC Nucleo board without any issues, outputting the correct voltage that I apply to the channel!

 

I guess there must be some fundamental mistake with my code, but I fail to see it.. Does anybody see what might need some changing?

The next option would be to discard any HAL functions and do it the proper way, I am just curious to know why this code runs on the one system and fails miserably on the other...

 

Thank you in advance!

 

Ruan!

 

 

uint8_t conv_completed = 0;

int16_t ADC_BUFFER[1];

char msg[64];

 

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_LPUART1_UART_Init();

MX_ICACHE_Init();

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

 

HAL_ADC_Start_DMA(&hadc1, (uint32_t*) ADC_BUFFER, 1);

 

while(!conv_completed); // Wait for conversion to complete

 

conv_completed = 0; // Reset the conversion complete flag

 

// Debug: Output raw buffer values

sprintf(msg, "ADC Channel 1: %hu \r\n", ADC_BUFFER[0]);

HAL_UART_Transmit(&hlpuart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);

 

HAL_Delay(1000); // Delay between reads

 

}

/* USER CODE END 3 */

}

 

 

/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 

/** Configure the main internal regulator output voltage

*/

if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

{

Error_Handler();

}

 

/** Initializes the RCC Oscillators according to the specified parameters

* in the RCC_OscInitTypeDef structure.

*/

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 = 1;

RCC_OscInitStruct.PLL.PLLN = 10;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;

RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

 

/** Initializes the CPU, AHB and APB buses 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_DIV8;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV8;

 

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)

{

Error_Handler();

}

}

 

static void MX_ADC1_Init(void)

{

ADC_MultiModeTypeDef multimode = {0};

ADC_ChannelConfTypeDef sConfig = {0};

 

/** Common config

*/

hadc1.Instance = ADC1;

hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

hadc1.Init.Resolution = ADC_RESOLUTION_12B;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;

hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;

hadc1.Init.LowPowerAutoWait = DISABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.NbrOfConversion = 1;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.DMAContinuousRequests = DISABLE;

hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;

hadc1.Init.OversamplingMode = DISABLE;

if (HAL_ADC_Init(&hadc1) != HAL_OK)

{

Error_Handler();

}

 

/** Configure the ADC multi-mode

*/

multimode.Mode = ADC_MODE_INDEPENDENT;

if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)

{

Error_Handler();

}

 

/** Configure Regular Channel

*/

sConfig.Channel = ADC_CHANNEL_1;

sConfig.Rank = ADC_REGULAR_RANK_1;

sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;

sConfig.SingleDiff = ADC_SINGLE_ENDED;

sConfig.OffsetNumber = ADC_OFFSET_NONE;

sConfig.Offset = 0;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

{

Error_Handler();

}

}

 

static void MX_ICACHE_Init(void)

{

/** Enable instruction cache in 1-way (direct mapped cache)

*/

if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)

{

Error_Handler();

}

if (HAL_ICACHE_Enable() != HAL_OK)

{

Error_Handler();

}

}

 

static void MX_LPUART1_UART_Init(void)

{

hlpuart1.Instance = LPUART1;

hlpuart1.Init.BaudRate = 115200;

hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;

hlpuart1.Init.StopBits = UART_STOPBITS_1;

hlpuart1.Init.Parity = UART_PARITY_NONE;

hlpuart1.Init.Mode = UART_MODE_TX_RX;

hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;

hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;

if (HAL_UART_Init(&hlpuart1) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)

{

Error_Handler();

}

}

 

static void MX_DMA_Init(void)

{

 

/* DMA controller clock enable */

__HAL_RCC_DMAMUX1_CLK_ENABLE();

__HAL_RCC_DMA1_CLK_ENABLE();

 

/* DMA interrupt init */

/* DMA1_Channel1_IRQn interrupt configuration */

HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

 

}

 

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

 

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

 

/*Configure GPIO pin : ADC_CHANNEL1_Pin */

GPIO_InitStruct.Pin = ADC_CHANNEL1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(ADC_CHANNEL1_GPIO_Port, &GPIO_InitStruct);

 

}

 

/* USER CODE BEGIN 4 */

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){

 

conv_completed = 1;

}

 

 

1 REPLY 1
raptorhal2
Lead

Missing:

/* Run the ADC calibration in single-ended mode */
if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK)
{
/* Calibration Error */
Error_Handler();
}

Compare the code with the L5 HAL library <Examples/ADC/ADC_MultiChannelSingleConversion>