2021-10-14 03:15 PM
My objective is to utilize ADC peripheral for reading analog signals and converting them to digital. I am using the polling method for reading ADC values. When setting up the chip configuration, I am currently using the Nucleo-H743ZI with default settings. After enabling ADC1, I enable continuous conversion mode, end of conversion mode to end of sequence selection and lastly, enable regular conversions. I would also like to send that data to a serial monitor to log data, therefore I will be using USART3. USART3 was already initialized by default settings and I'll be using default settings. Save and generate code.
The polling method requires 3-4 steps:
(+++) Activate the ADC peripheral and start conversions using function HAL_ADC_Start()
(+++) Wait for ADC conversion completion using function HAL_ADC_PollForConversion()
(+++) Retrieve conversion results using function HAL_ADC_GetValue()
(+++) Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop()
After opening main.c , I create a few variables and lines of code.
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t MSG[35] = {'\0'};
uint32_t X = 0;
uint16_t adc_value = 0;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ETH_Init();
MX_USART3_UART_Init();
MX_USB_OTG_FS_PCD_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_TIM2_Init();
while (1)
{
// Start ADC Conversion
HAL_ADC_Start(&hadc1);
// Poll ADC1 Perihperal & TimeOut = 1mSec
HAL_ADC_PollForConversion(&hadc1, 1);
// Read The ADC Conversion Result
adc_value = (uint8_t)HAL_ADC_GetValue(&hadc1);
sprintf((char*)MSG, "Digital Value = %lu \r\n", adc_value);
HAL_UART_Transmit(&huart3, (uint8_t*)MSG, sizeof(MSG), 100);
HAL_Delay(500);
My question is why am I reading zero? I have this circuit set up, only difference is instead of A0 I have to locate PA0 (ADC1) which can be easily found. So why is this happening?
2021-10-14 03:46 PM
You're not allowed to put 5V into PA0 in analog mode. Use 3.3V as the upper rail for your potentiometer. Probably didn't damage the chip, but you could have.
> adc_value = (uint8_t)HAL_ADC_GetValue(&hadc1);
You're casting the 16-bit result to an 8-bit variables, so you're losing the 8 most significant bits. Probably not the issue at hand, but something that will bite you once it's otherwise working.
Monitor the HAL return values to make sure it's not timing out or encountering an error.
2021-10-14 03:53 PM
Thank you for responding, I had forgot to mention that I am using 3.3V and yes PA0 is only 3.3V tolerant. I changed adc_value = (uint16_t)HAL_ADC_GetValue(&hadc1);, however I am still reading zero. How do I monitor return values? I have looked at the variables window and I see that adc_value is 0 which agrees with the serial monitor.
2023-01-21 11:10 AM
I also getting same problem .
please tell me if you got the solution.
thanks.
2023-01-21 11:32 PM
ADC IP changes quite a lot between STM32 families.
Make sure the GPIO are configured as analog inputs by watching the HW registers in debug mode, stopping the code few seconds after it starts running.
As for monitoring its values, the simplest way if the debug live watch found on most IDEs.
Otherwise, (needs more time to set it up), through an HC-05 RS232 to Bluetooth SPP cheap dongle, the Android Bluetooth Electronics App turns your smartphone into a handy remote control display with widgets. Code on the STM32 is pretty small.
You may check one implementation here on STM32C0