1.1K
/* Includes ------------------------------------------------------------------*/ #include "main.h" #include"stdio.h" #include<string.h> /* Private variables ---------------------------------------------------------*/ ADC_HandleTypeDef hadc1; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ int val = 0; // Variable to store the value read float TempC; // Temperature value in Celsius degree float TempF; // Temperature value in Fahrenheit degree float gain = (3.3/(4096-1)) ; // gain = STM23_Vref/(2^12-1) STM32 ADC 12bit resolution float vref = 2.5; // datasheet : Reference value 2.5V for adjustment float Output_Voltage= 0.005; // datasheet : 5 mV/°C for J type and K type char buffer[100]; /* 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); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ if(HAL_ADC_Start(&hadc1)!=HAL_OK) { Error_Handler(); } /* USER CODE END 2 */ while (1) { while(HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY)!=HAL_OK) { Error_Handler(); } val = HAL_ADC_GetValue(&hadc1); // Read the input from ADC TempC = (( (float)(val) * gain)-vref)/ Output_Voltage; // -ref Convert ADC value to Celsius degree TempF = (TempC * 1.8) + 32.0; sprintf(buffer, "Temperature: %.2f Degrees Celsius, %.2f Degrees Fahrenheit\r\n", TempC,TempF); HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY); HAL_Delay(500); } }