2022-01-23 02:32 AM
Hi all,
I'm new to the STM32 MCU familiy, so I've got trouble already doing basic stuff like reading the analog input value of the potentiometer. My main problem is, that I don't understand how multiple measurements can be made on one single ADC. I've watched several Videos about the topic already, but can't get it to wok properly. With some code variants I read bigger numbers that shouldn't be possible with a 12bit ADC (23.000-ish or even bigger), with other codes I dont get anything back at all.
What I did so far:
The datasheet states that the potentiometer is connected to pin PC3, so I added this pin in CubeMX as a ADC1_IN9 pin. In the ADC1 Mode and Config tab, I didn't change any settings, as I'm not sure if its needed. Only thing I've read in one of STM's MC docs is that I'm not allowed to use the injected conversion mode as it will interfere with the motor controls.
In the while loop I added these commands:
HAL_ADC_Start(&hadc1);
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_9;
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;
HAL_ADC_ConfigChannel(&hadc1, &sConfig)
HAL_ADC_PollForConversion(&hadc1, 20)
Value = HAL_ADC_GetValue(&hadc1);
What am I missing?
Best regards, Jordan
Solved! Go to Solution.
2022-02-01 03:36 AM
Hello @Jordan Güllübag ,
Thanks for your feedback and welcome to the community,
Since your using CubeMX to configure your MCU then the ADC config is already generated in MX_ADC1_init(void) in main.c file.
So you don't have to add the following code in the while loop as it is already done.
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_9;
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;
HAL_ADC_ConfigChannel(&hadc1, &sConfig)
There are some ADC examples available under C:\Users\<Your_User_Name>\STM32Cube\Repository\STM32Cube_FW_XX_Vx.x.x\Projects. Some of these examples don't come with an .ioc file but they are helpful.
You can also find some examples under Example selector in CubeMX, some of which are Compatible with STM32CubeMX.
I hope this helps :)
If your issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)
Thanks for your contribution and do not hesitate to raise any issue/ feedback.
Sara.
2022-02-01 03:36 AM
Hello @Jordan Güllübag ,
Thanks for your feedback and welcome to the community,
Since your using CubeMX to configure your MCU then the ADC config is already generated in MX_ADC1_init(void) in main.c file.
So you don't have to add the following code in the while loop as it is already done.
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_9;
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;
HAL_ADC_ConfigChannel(&hadc1, &sConfig)
There are some ADC examples available under C:\Users\<Your_User_Name>\STM32Cube\Repository\STM32Cube_FW_XX_Vx.x.x\Projects. Some of these examples don't come with an .ioc file but they are helpful.
You can also find some examples under Example selector in CubeMX, some of which are Compatible with STM32CubeMX.
I hope this helps :)
If your issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)
Thanks for your contribution and do not hesitate to raise any issue/ feedback.
Sara.