0

I'm using the MCU STM32F4 and I want to read the ADC inputs using Hal Library and show these on the Terminal. My ADCs inputs are running in continuous convertion mode and the convertion is called by tick of a Timer.

ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;
char buffer[10];
uint32_t adc1, adc2, adc3;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
    if(htim->Instance == TIM2)
    {
        HAL_ADC_Start(&hadc1);

        HAL_ADC_PollForConversion(&hadc1,300);
        adc1 = HAL_ADC_GetValue(&hadc1);
        sprintf(buffer,"A1: %d ",(int)adc1);
        CDC_Transmit_FS((uint8_t*)buffer,9);

        HAL_ADC_PollForConversion(&hadc1,300);
        adc2 = HAL_ADC_GetValue(&hadc1);
        sprintf(buffer,"A2: %d  ",(int)adc2);
        CDC_Transmit_FS((uint8_t*)buffer,5);

        HAL_ADC_Stop(&hadc1);

        if(adc1  > 1000)
        {
            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,1);
        }
        else
        {
            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,0);
        }
    }
}

This code above works if I have a single channel to monitorate. But, if I use more than one channel, the output show only the channel 2 values, like the image shown below.

Terminal Output

I dont know what is happening. Do can you help me?

Felipe Mateus
  • 127
  • 2
  • 11
  • 1
    Are you sure the `9` and `5` hard coded buffer lengths are correct? Would it be better to use the return value from `sprintf` (the buffer length used)? – Weather Vane Oct 21 '19 at 13:03
  • How is `CDC_Transmit_FS` supposed to work? Does it copy your data to an internal buffer/queue or do you have to wait until it has finished sending the data before you can re-use the buffer? – Bodo Oct 21 '19 at 13:03
  • This [previous question](https://stackoverflow.com/questions/40597612/stm32-usb-vcp-virtual-com-port) says you need a delay. – Weather Vane Oct 21 '19 at 13:04
  • 1
    Also your buffer size `10` is very tight. For test puposes you could try forming a single string with both ADC results. – Weather Vane Oct 21 '19 at 13:08
  • The string built for "A2" is longer than 5, at least 7 characters. What happens if you send the correct number of characters? What happens if you use formats ending in `"\r\n"`? – the busybee Oct 21 '19 at 16:40
  • The buffer size is really wrong, but isnt the solution. I changed the size to a correct value, but to work I have to insert all of ADCs values in a single string like says @WeatherVane and now the code works perfectly. I dont understand why this is happening. Is dont the same of show one by one? – Felipe Mateus Oct 21 '19 at 16:54
  • Did you read the [linked question](https://stackoverflow.com/questions/40597612/stm32-usb-vcp-virtual-com-port)? There are several previous questions to be found by googling "stackoverflow c CDC_Transmit_FS". – Weather Vane Oct 21 '19 at 17:08

0 Answers0