436 views
in GUI Development by
How to initialize ADC Pins to get Adc values. I have already initialized Adc Pins in stm32f769i_discovery.c file where the pins are initialized for led and other functions. But it is not getting initialized. Can someone help me.

1 Answer

+1 vote
by
Hello,

have you searched through the examples from ST that are provided within the STM32 Cube firmware tree - I think there should be also some examples covering ADC functionality. Otherwise, let me recommend to check the ST developer forum.

Here we are focused on GUI development with Embedded Wizard.

Best regards,

Manfred.
by
Hi Manfred,

For DeviceIntegration example, LED pin is initialized in stm32f769i_discovery.c file, like that i just want to initialize the ADC pins. Actually i did and also i made a function to get adc values also, that function i am calling it in void DeviceIntegrationExample_AdcWorkerThread(); function DeviceDriver.c file. But I am not getting any values in the display. void DeviceIntegrationExample_AdcWorkerThread(); -> this function is called continuosly from the GUI.
by
Hi,

yes, as soon as you get real data from an ADC, you can use that instead of the simulated data that is used in the DeviceIntegration example.

The exact usage of an ADC channel (including the corresponding initialization) was not included into this example, because this topic is completely dependent on the used hardware platform and it is not related to the GUI application or the interface to the device.

For ADC initialization, have you checked the ST examples? Do you get some data from your ADC? As soon as this works, it should be easy to combine that with DeviceIntegration example.

Best regards,

Manfred.
by
Hi Manfred,

I have done the Adc example on STM IDE, it was working fine and i was getting the data also. What and which pin I have configured and initialized on Stm Ide, the same configuration i have copied from there and pasted into stm32f769i_discovery.c file where all the pins are initialized.
by

Hi,

do you get the data within the DeviceIntegrationExample_AdcWorkerThread()? Have you debugged the situation?

Can you post the relevant code snippets?

Best regards,
Manfred.

by

Initialization of ADC Pin in stm32f769i_discovery.c file 

//Initialization of ADC Pin in stm32f769i_discovery.c file

void BSP_Analog_Init()
{
  ADC_ChannelConfTypeDef sConfig = {0};
  ADC_HandleTypeDef hadc1;
  
   /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_6;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */
}

Function to read ADC Pin defined in stm32f769i_discovery.c file 

//Function to read ADC Pin defined in stm32f769i_discovery.c file

uint32_t BSP_AdcValue()
{
	//ADC_HandleTypeDef hadc1;
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1,0);
	uint32_t Analog_data = HAL_ADC_GetValue(&hadc1);
	float voltage = (3.3f * Analog_data/4096.0f);
	HAL_Delay(100);
	return Analog_data;
}

Calling BSP_AdcValue(); function in DeviceDriver.c file to get and update the value to the GUI 

//Calling BSP_AdcValue(); function in DeviceDriver.c file to get and update the value to the GUI

static void AdcWorkerThread( const void* arg )
{
  uint32_t simulatedAdcValue = 0;

  while ( DeviceInitialized )
  {
    /* in order to simplify the example, the ADC value is just simulated - it
       could be read from a real ADC by using corresponding BSP functions... */
    //simulatedAdcValue = ( simulatedAdcValue + 1 ) % 2000;
	simulatedAdcValue = BSP_AdcValue();
	//EwPrint( "Set Temp: %0.2f\n", aValue );
	EwPrint("%d", simulatedAdcValue);
    /*
       Important note: This function is a separate thread/task and not executed
       in the context of the main GUI thread/task. NEVER make a direct function
       call to a method of the driver class or any other generated code
       from an interrupt handler or any other thread/task.
       EwInvoke() or EwInvokeCopy() have to be used to schedule the invocation of
       the desired method in the context of the GUI thread/task.
    */
    EwInvoke( UpdateAdcValueProc, &simulatedAdcValue);

    /* sleep for a certain period... */
    EwBspOsDelay( 1000 );
  }

  /* terminate the worker thread */
  WorkerThread = 0;
  EwBspOsThreadDestroy( EwBspOsThreadGetHandle());
}

To the pin what i have initialized as ADC Pin i.e ADC1_IN6, I am giving 3.3volt to that pin.

I am using STM32F769NI DISCOVERY Board.

by

Within your function BSP_Analog_Init() the handle hadc1 is a local variable - however, the function BSP_AdcValue() is using some global hadc1 handle...

Can you check if this is correct? I assume there is some compiler warning...

Let me know.

Best regards,

Manfred.

 

by
Hi Manfred

Actually I have declared that variable globally, for your convenience I have pasted that inside BSP_Analog_Init(). There are no Warnings while compiling
by
So the code you have posted here is not the code that you are using...

The function BSP_Analog_Init() should be called before the ADC worker thread is created. Maybe this is missing.

Can you step through the initialization of the ADC and the first access to the ADC?

I think there are additional settings necessary (e.g. the PIN configuration). Can you compare your implementation with the working ST example? What is missing?
by

The function BSP_Analog_Init() is called before the AdcWorkerThread() in DeviceIntegrationExample_Init() function when the device is initialized 

//The function BSP_Analog_Init() is called before the AdcWorkerThread() in DeviceIntegrationExample_Init() function when the device is initialized

void DeviceIntegrationExample_Init( void )
{
  /* check for initialization */
  if ( DeviceInitialized )
    return;

  /* configure LED */
  EwBspInOutInitLed();
  
  EwBspInOutInitDigital();
  
  BSP_Analog_Init();

  /* configure interrupt for hardware button */
  EwBspInOutInitButton( HardButtonIsrCallback );

  DeviceInitialized = 1;

#if EW_USE_OPERATING_SYSTEM == 1

  /* create and start the worker thread to process ADC data */
  WorkerThread = EwBspOsThreadCreate( AdcWorkerThread, EW_BSP_OS_THREAD_PRIORITY_NORMAL, 1024, 0 );

#endif
}

Actually ADC_HandleTypeDef hadc1; is commented in the working file, i have pasted working file only, i have removed comment to show that the hadc1 is actually declared but it is declared globally in the working file. 

//Initialization of ADC Pin in stm32f769i_discovery.c file. Actually ADC_HandleTypeDef hadc1; is commented in the working file, i have pasted working file only, i have removed comment to show that the hadc1 is actually declared but it is declared globally.

void BSP_Analog_Init()
{
  ADC_ChannelConfTypeDef sConfig = {0};
  //ADC_HandleTypeDef hadc1;
  
   /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_6;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */
}

 

by
I assume your working example contains some call to HAL_ADC_MspInit() - did you take that also into the device integration example?

Just to be sure that PINs and clocks are configured for the ADC...
by
No, Actually i did not added this function in my device integration example. Can you tell where should i include this HAL_ADC_MspInit() function?.
by
As you have a working example, please make sure to have the same initialization and initialization order when you move the ADC part from the ST example to the GUI example. Then it should work in the same manner....
by
Hi Manfred

HAL_ADC_MspInit(), i called this function before the Adc Initialization. It is working fine. Thanks for suggestion.
by
Hi Manfred

I am printing a received and converted Adc value in board display but it is printing with 6 decimal values. I just want to print only 3 decimal values in the display, Means I am getting 3.4116378 this number and printing as it is but I just need only 3.416 to display. How can i do it?
by

Hello,

it depends on what you want to do....

For example, if you want to display a voltage from 0.000 V .... 9.999 V, you can transfer the data in Millivolt (0.... 9999) as integer and use a Value Display widget.

Btw: For a new topic that is no more related to 'ADC initialization', please open a new question (with detailed description).

Best regards,

Manfred.

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...