2022-11-22 10:11 PM
I'm new to the stm32MCU. I am using a Nucleo-U575zi-Q board. I am trying to read ADC values by setting the built-in DAC. I have check my DAC out and it is working fine. My ADC reading is giving me garbage values. I have set HAL_PWREx_EnableVddA() also.
My ADC config
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_14B;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 3;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
uint32_t ADC_Value[20]={0};
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100000);
ADC_Value[0] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
Solved! Go to Solution.
2022-11-23 07:14 AM
Hello ABhat.12 (Community Member)
Here attached your project with some rework.
I change the DAC configuration to have DAC_CHIPCONNECT_BOTH (IO and Internal)
ADC1 configuration added channel for ADC1_IN9 (PA4 DAC_OUT1) and ADC1_IN10 (PA5 DAC_OUT2) and increase Sampling Time.
I also changed ADC1 resolution as 12bit (same as DAC to compare both) but you can set it back to 14bit.
Do DAC Self calibration for both channels.
Finally, changed your main loop to synchronize ADC1 sequence.
You should see in Live Expression:
adcResultsArray[0] = PC0 ADC1_IN1 (below floating result)
adcResultsArray[1] = PC1 ADC1_IN2 (below floating result)
adcResultsArray[2] = PA3 ADC1_IN8 (below floating result)
adcResultsArray[3] = PA4 ADC1_IN9 ~2239 (DAC_OUT[4]
adcResultsArray[4] = PA5 ADC1_IN10 ~3110 (DAC_OUT[5]
Let me know if it helps.
BR
Romain,
In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-11-22 11:59 PM
Hello ABhat.12 (Community Member)
Can you share the ADC1 Channel configuration ?
In order to convert DAC_OUT1, you need also to configure ADC_CHANNEL_9 using HAL_ADC_ConfigChannel(&hadc1, &sConfig)
Best regards
Romain
In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-11-23 01:22 AM
ADC Config
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_14B;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 3;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_5CYCLE;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
// DAC Config
uint32_t DAC_OUT[6] = {0, 1241, 2482, 3723,2234,3103};
if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DAC_OUT[4]) != HAL_OK)
{
Error_Handler();
}
if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, DAC_OUT[5]) != HAL_OK)
{
Error_Handler();
}
if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
}
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100000);
ADC_Value[0] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100000);
ADC_Value[1] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100000);
ADC_Value[2] = HAL_ADC_GetValue(&hadc1);
2022-11-23 02:06 AM
2022-11-23 07:14 AM
Hello ABhat.12 (Community Member)
Here attached your project with some rework.
I change the DAC configuration to have DAC_CHIPCONNECT_BOTH (IO and Internal)
ADC1 configuration added channel for ADC1_IN9 (PA4 DAC_OUT1) and ADC1_IN10 (PA5 DAC_OUT2) and increase Sampling Time.
I also changed ADC1 resolution as 12bit (same as DAC to compare both) but you can set it back to 14bit.
Do DAC Self calibration for both channels.
Finally, changed your main loop to synchronize ADC1 sequence.
You should see in Live Expression:
adcResultsArray[0] = PC0 ADC1_IN1 (below floating result)
adcResultsArray[1] = PC1 ADC1_IN2 (below floating result)
adcResultsArray[2] = PA3 ADC1_IN8 (below floating result)
adcResultsArray[3] = PA4 ADC1_IN9 ~2239 (DAC_OUT[4]
adcResultsArray[4] = PA5 ADC1_IN10 ~3110 (DAC_OUT[5]
Let me know if it helps.
BR
Romain,
In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-11-23 10:08 PM
@RomainR.
Hello RomainR
I am able to get the desired output on channel 9 and 10, which are internally connected. But when I connect the DAC out pin to channel 1, 2, or 8 using a cable, I am still getting garbage values.
2022-11-23 11:09 PM
Hello RomainR
I was connecting the board's front headers A1, A2, and A8 thinking that those were channels 1, 2, and 8. Now I found out from board user Manuel that those are connected to different channels. I have changed the settings according to the user manual, and that fixed the problem.
Thank you
2022-11-24 12:01 AM
Hello ABhat.12 (Community Member)
That's a good new.
BR,
Romain,
In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.