I have a code generated by STMCubeMX where I use the portal PA0 like ADC Input. I'm trying to read this input using HAL Library of STM in C and transmit the value to USB port using CDC. See the main, where i try to get the data and show this:
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
static void MX_ADC1_Init(void);
int main(void)
{
uint8_t buffer[8];
HAL_Init();
SystemClock_Config();
MX_USB_DEVICE_Init();
MX_ADC1_Init();
HAL_ADC_Start(&hadc1);
while (1)
{
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_14);
HAL_ADC_PollForConversion(&hadc1, 1000);
sprintf((char*)buffer,"%d\n",(int)HAL_ADC_GetValue(&hadc1));
//HAL_Delay(500);
CDC_Transmit_FS(buffer,8);
}
}
Debugging the code I saw that function "HAL_ADC_PollForConversion(&hadc1, 1000)" never returns "HAL_OK". In the terminal, a single value appears.
UPDATE
Well, for the code works I have to put the "Start" of ADC inside of infinit loop and a call of "Stop" on the final of loop. Ps: The Adc is running on continuous conversion mode.
int main(void)
{
uint8_t buffer[8];
HAL_Init();
SystemClock_Config();
MX_USB_DEVICE_Init();
MX_ADC1_Init();
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_14);
HAL_ADC_PollForConversion(&hadc1, 1000);
sprintf((char*)buffer,"%d\n",(int)HAL_ADC_GetValue(&hadc1));
//HAL_Delay(500);
CDC_Transmit_FS(buffer,8);
HAL_ADC_Stop(&hadc1);
}
}
As an addition to the answer in the comments:
The call to HAL_ADC_PollForConversion(&hadc1, 1000);
explicitly stops the conversion, even if continuous conversion is activated.
If you you want/must wait for each conversion to get finished, you must restart the ADC with HAL_ADC_Start(&hadc1);
after waiting (and reading) for the result.