cancel
Showing results for 
Search instead for 
Did you mean: 

How ADC Works Without GPIO Pins ?

Kiran kumar
Associate II

Hi All,

I am working on ADC with multiple channels Interrupt method using 

NUCLEO -L4R5ZI- P(STM32L4R5ZITxP). ADC working for all channels irrespective of GPIO Pins.

for example, I Commented GPIOA( Port A and its clock) highlighted in bold color even though the ADC channels of Port A works.32 MHZ clock selected for ADC here. 

Can Someone please help me , how to work with ADC only by selecting GPIO Pins.

7 REPLIES 7
Kiran kumar
Associate II

#include "main.h"

ADC_HandleTypeDef hadc1;

 uint32_t ADC_RESULTS[16] = {0};

 uint32_t i;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_ADC1_Init(void)

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_ADC1_Init();

__HAL_ADC_ENABLE_IT(&hadc1,ADC_IT_EOS);

HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);

while (1)

{

 HAL_ADC_Start_IT(&hadc1);

 }

}

static void MX_ADC1_Init(void)

{

ADC_ChannelConfTypeDef sConfig = {0};

 hadc1.Instance = ADC1;

 hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

 hadc1.Init.Resolution = ADC_RESOLUTION_12B;

 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;

 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

 hadc1.Init.LowPowerAutoWait = DISABLE;

 hadc1.Init.ContinuousConvMode = DISABLE;

 hadc1.Init.NbrOfConversion = 16;

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

 }

sConfig.Channel = ADC_CHANNEL_1;

 sConfig.Rank = ADC_REGULAR_RANK_1;

 sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;

 sConfig.SingleDiff = ADC_SINGLE_ENDED;

 sConfig.OffsetNumber = ADC_OFFSET_NONE;

 sConfig.Offset = 0;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_2;

 sConfig.Rank = ADC_REGULAR_RANK_2;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_3;

 sConfig.Rank = ADC_REGULAR_RANK_3;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_4;

 sConfig.Rank = ADC_REGULAR_RANK_4;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_5;

 sConfig.Rank = ADC_REGULAR_RANK_5;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_6;

sConfig.Rank = ADC_REGULAR_RANK_6;

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

{

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_7;

sConfig.Rank = ADC_REGULAR_RANK_7;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_8;

 sConfig.Rank = ADC_REGULAR_RANK_8;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_9;

 sConfig.Rank = ADC_REGULAR_RANK_9;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_10;

 sConfig.Rank = ADC_REGULAR_RANK_10;

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

 {

 Error_Handler();

}

 /** Configure Regular Channel

 */

 sConfig.Channel = ADC_CHANNEL_11;

 sConfig.Rank = ADC_REGULAR_RANK_11;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_12;

 sConfig.Rank = ADC_REGULAR_RANK_12;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_13;

 sConfig.Rank = ADC_REGULAR_RANK_13;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_14;

 sConfig.Rank = ADC_REGULAR_RANK_14;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_15;

 sConfig.Rank = ADC_REGULAR_RANK_15;

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

 {

 Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_16;

 sConfig.Rank = ADC_REGULAR_RANK_16;

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

 {

 Error_Handler();

 }

}

static void MX_GPIO_Init(void)

{

 /* GPIO Ports Clock Enable *

 __HAL_RCC_GPIOC_CLK_ENABLE();

 // __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)

{

if(__HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_EOC))

{

ADC_RESULTS[i] = HAL_ADC_GetValue(&hadc1);

i++;}

if(__HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_EOS))

{i=0;

}

}

static void MX_GPIO_Init(void)

{

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 // __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hadc->Instance==ADC1)

 {

 __HAL_RCC_ADC_CLK_ENABLE();

 __HAL_RCC_GPIOC_CLK_ENABLE();

 // __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOB_CLK_ENABLE();

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;    

// GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;

// GPIO_InitStruct.Pull = GPIO_NOPULL;

// HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 /* ADC1 interrupt Init */

 HAL_NVIC_SetPriority(ADC1_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(ADC1_IRQn);

 }

}

Thank you 

TDK
Guru

> How ADC Works Without GPIO Pins ?

The ADC will still function if nothing is connected to its input, but the result will not be useful. It would be similar to trying to convert a voltage on a floating pin.

> how to work with ADC only by selecting GPIO Pins.

I'm not sure what you're asking here. What's wrong with the original uncommented code?

If you feel a post has answered your question, please click "Accept as Solution".
Kiran kumar
Associate II

> How ADC Works Without GPIO Pins ?

The ADC will still function if nothing is connected to its input, but the result will not be useful. It would be similar to trying to convert a voltage on a floating pin.

I do verified by connecting the Potentiometer to ADC Channels and it works for all the channels even though GPIO pins and its clock enabled or disabled.

How to configure ADC Channels should work only by selecting GPIO Pins and its clock ?

TDK
Guru

> How to configure ADC Channels should work only by selecting GPIO Pins and its clock ?

Unfortunately, I still don't understand your question.

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

The clocks are predominantly there for the configuration/control of the synchronous digital electronics.

>>How to configure ADC Channels should work only by selecting GPIO Pins and its clock ?

The pin/channel associativity is described in the data sheet. I don't know you have this level of control. You'll have to manage the capture/use of the data in your software.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Kiran kumar
Associate II

ADC_CHANNEL_X ( X -> 1,2,34.........16) all are working without the initialization of GPIO pins. 

We are getting the digital values for every 0.8mV ( 3.3/2^12). vref is 3.3 and 12 bit resolution. we verified it by connecting Potentiometer to the ADC_CHANNEL_2 .

0693W00000GW8kjQAD.png 

Is the ADC_CHANNEL_X ( X -> 1,2,34.........16) enabling the GPIO pins internally ?

What is the default mode of ADC Pins ?

TDK
Guru

The default mode of pins is given in the reference manual under the relevant GPIO register settings.

For this chip and other newer chip families, most pins are in analog mode after reset. That would explain what you're seeing.

0693W00000GWTVuQAP.png

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