672
/* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include <stdio.h> #include <math.h> #include <string.h> /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define NUMSAMPLES 5 //Number of ADC samples to average for better measurement accuracy #define SERIESRESISTOR 10000 //Value of the series resistor in ohms #define THERMISTORNOMINAL 10000 //Nominal resistance of the thermistor at 25°C in ohms #define TEMPERATURENOMINAL 25 //Nominal temperature for the thermistor resistance in degrees Celsius #define BCOEFFICIENT 3950 //Beta coefficient of the thermistor, typically between 3000 and 4000 /* USER CODE END PD */ /* Private variables ---------------------------------------------------------*/ ADC_HandleTypeDef hadc1; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ char buffer[50]; //Buffer for UART transmission float samples[NUMSAMPLES]; //Array to store ADC sample values float average = 0; //Variable to store the average of ADC samples float steinhart; //Variable to store the calculated temperature in Celsius uint8_t i; //Loop counter for sampling /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ average = 0; //Clear previous average value to ensure correct calculation for the new set of samples //Collect multiple ADC samples and store them in the array for (i = 0; i < NUMSAMPLES; i++) { HAL_ADC_Start(&hadc1); //Start the ADC conversion HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY); //Wait for the conversion to complete samples[i] = HAL_ADC_GetValue(&hadc1); //Read the ADC conversion result HAL_Delay(10); //Delay between samples to stabilize the ADC readings } //Compute the average of the collected ADC samples for (i = 0; i < NUMSAMPLES; i++) { average += samples[i]; //Sum up all the ADC sample values } average /= NUMSAMPLES; //Calculate the average of the ADC samples sprintf(buffer, "Average analog reading %.2f \r\n", average); // Format the average reading for UART output HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY); //Transmit the average reading via UART //Convert the average ADC reading to thermistor resistance and calculate temperature if (average > 0) { // Ensure the average is not zero to avoid division errors average = 4094 / average - 1; // Convert ADC reading to resistance using 12-bit ADC resolution average = SERIESRESISTOR / average; // Calculate the thermistor resistance sprintf(buffer, "Thermistor resistance %.2f \r\n", average); // Format the resistance value for UART output HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY); // Transmit the resistance value via UART // Apply the Steinhart-Hart equation to convert resistance to temperature steinhart = average / THERMISTORNOMINAL; //Compute the ratio of thermistor resistance to nominal resistance steinhart = log(steinhart); //Compute the natural logarithm of the resistance ratio steinhart /= BCOEFFICIENT; //Divide by the thermistor's Beta coefficient steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); //Add the reciprocal of the nominal temperature in Kelvin steinhart = 1.0 / steinhart; //Compute the reciprocal to get the temperature in Kelvin steinhart -= 273.15; //Convert the temperature from Kelvin to Celsius sprintf(buffer, "Temperature: %.2f *C\r\n", steinhart); // Format the temperature value for UART output HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY); // Transmit the temperature value via UART } else { sprintf(buffer, "Error: Invalid ADC reading\r\n"); // Handle invalid ADC reading (e.g., division by zero) HAL_UART_Transmit(&huart1, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY); // Transmit the error message via UART } HAL_Delay(1000); // Wait for 1 second before repeating the loop } /* USER CODE END 3 */ }