cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to transmit continuous ADC data via UART to PuTTY

sherbeen98
Associate

Hallo!

I am trying to calculate and display the temperature measurement of 2 NTC thermistor's using STM32F103RBT6. I have done the ADC conversion and hardware connections correct. I was able to get continuous values with a delay of 1 second before using input commands. Now, however the measurement and display of output in PuTTY is not continuous. I programmed the mcu to start temperature measurement when the input is '1' and stop it when input is '2'. But I have to press '1' each time to obtain a measurement , even though I used the same HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY) . Is it a problem in my code or can we not continously transmit data in this case?

 

3 REPLIES 3
TDK
Guru

> Is it a problem in my code or can we not continously transmit data in this case?

The hardware is able to transmit continuously, so it must be a code issue. Debug your code, hit "pause" when you think it should be sending but isn't, and figure out where the code is at and correct it.

If you feel a post has answered your question, please click "Accept as Solution".

This is how my while loop looks currently, I thought it should be continuously transmitting data with this, but it isn't.

 

while (1)
  {
    /* USER CODE END WHILE */
// User giving input
uint8_t input;
HAL_UART_Receive(&huart2, &input, sizeof(input), HAL_MAX_DELAY);  //receiving the command
 
//if (input=='1')
if (input=='1'&& system==0)
{
           system=1;   //start measurement
HAL_UART_Transmit(&huart2, (uint8_t*)"Temperature measurement started.\r\n", 35, HAL_MAX_DELAY);
}
//else if(input=='2')
else if(input=='2' && system==1)
{
           system=0;  //stop measurement
HAL_UART_Transmit(&huart2, (uint8_t*)"Temperature measurement stopped.\r\n", 35, HAL_MAX_DELAY);
 
//Turning off LED when measurement stops
HAL_GPIO_WritePin(GPIOA, GREEN_LED, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RED_LED, GPIO_PIN_RESET);
}
 
//if(input=='1')
if(system==1)
     {
             float V_ref= 3.3;
             char buffer[100];
         // ADC Value from Channel 0 (First NTC)
            ADC_Select_CH0();
            HAL_ADC_Start(&hadc1);
            HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
 
            adc_value1 = HAL_ADC_GetValue(&hadc1);  //Read ADC from Channel 0
            HAL_ADC_Stop(&hadc1);
 
            float voltage1= (adc_value1 / 4095.0)* V_ref;    // Convert ADC value to voltage
 
            resistance1 = (refResistance1*voltage1)/(V_ref - voltage1); //Voltage Divider Equation
            temperature_ntc1 = findTemp1(resistance1);   // Calculate temperature from resistance
 
        // ADC Value from Channel 1 (Second NTC)
            ADC_Select_CH1();
            HAL_ADC_Start(&hadc1);
            HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
            adc_value2 = HAL_ADC_GetValue(&hadc1); //Read ADC from Channel 1
            HAL_ADC_Stop(&hadc1);
 
             float voltage2= (adc_value2 / 4095.0)* V_ref;
             resistance2 = (refResistance2*voltage2)/(V_ref- voltage2);
             temperature_ntc2 = findTemp2(resistance2);
        // Check temperature differences and control the LED's
       if (fabs(temperature_ntc1 - temperature_ntc2)> 1.0)
       {
            //Temperature difference btw 2 NTC'S greater than 1 degree, RED LED should blink
            HAL_GPIO_WritePin(GPIOA, RED_LED, GPIO_PIN_SET);
            HAL_GPIO_WritePin(GPIOA, GREEN_LED, GPIO_PIN_RESET);
 
       }
       else
       {
            HAL_GPIO_WritePin(GPIOA, GREEN_LED, GPIO_PIN_SET);
            HAL_GPIO_WritePin(GPIOA, RED_LED, GPIO_PIN_RESET);
       }
 
           // Transmit the temperature values
           sprintf(buffer, "Temperature_NTC1: %.2f °C \r\n", temperature_ntc1);
           HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
           sprintf(buffer, "Temperature_NTC2: %.2f °C \r\n\r\n", temperature_ntc2);
           HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
 
       // Updating the older temperature measurements
        oldtemp1 = temperature_ntc1;
        oldtemp2= temperature_ntc2;
     }
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

TDK
Guru

You're calling HAL_UART_Receive with HAL_MAX_DELAY every loop. The system is waiting for a character to come in. If you don't want it to wait, use a timeout here and handle HAL_TIMEOUT appropriately.

If you feel a post has answered your question, please click "Accept as Solution".