Unable to read continuous values from NTC thermistors on PuTTY after giving commands

88 Views Asked by At
while (1)
{

// 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 \*/
}

I am trying to calculate and read values from two NTC thermistors and currently sending it via UART to PuTTY console. I am using a STM32 nucleo board. I wanted to input command '1' for continuous reading and displaying of values in PuTTY and '2' to stop measurement and reading. My code worked good and was transmitting values continuously before giving this input ==1 and 2 command. But after this PuTTY only displays temperature values when '1' is pressed which means I need to keep pressing '1' to display the values , there is no continuous transmission. Is this a problem in my code or continuous transmission is not possible when there is a flag condition? I want to assure that my hardware connection and configuration of pins are correct because the value is transmitted continuously without the input condition.

This is how my while loop looks like in the STM32 IDE. The output now displays 'Temperature measurement started and keeps displaying values of Temperature_NTC1 and Temperature_NTC2 only when I keep pressing '1' each time. I am not getting continous temperatures without typing in '1'.What I want is the temperature values to be dsiplayed continuously when I type '1' once and stop when I type in '2'.

`

2

There are 2 best solutions below

0
pmacfarlane On BEST ANSWER

HAL_UART_Receive(..., HAL_MAX_DELAY); will block forever until a character is entered.

The easiest solution would be to use a smaller timeout value, and check the return value from the function. Something like:

while (1)
{
    uint8_t input;

    if (HAL_UART_Receive(&huart2, &input, sizeof(input), 10) == HAL_OK)
    {
        // Process input
    }
    else
    {
        // Error or timeout - no input to process
    }

    // The rest of your code that reads and displays a value
}

This uses a 10 millisecond timeout. If a character has been entered, you process it, otherwise you continue on with your loop.

0
Geisterfahrer On

I would implement it using the interrupt function:

HAL_UART_Receive_IT();

The interrupt function is invoked each time your microcontroller (MCU) receives data via UART. In the callback function associated with the UART, you can implement the necessary code to manage UART operations.

Do not forget to implement the UART Callback function in the source code stm32fXxx_it.c, this file is responsible for the Interrupt Service Routines.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  HAL_UART_Receive_IT(huart, pData, Size);
}

By utilizing this approach, you can ensure that your system won't become unresponsive while waiting for data to arrive from the UART interface. This allows your MCU to efficiently handle incoming data without blocking the execution.

You can get more information here: https://wiki.st.com/stm32mcu/wiki/Getting_started_with_UART#UART_with_Interrupt