帮我写一个stm32 使用PT100测温,使用查表法测温,测温范围-120 到80℃,使用hal库
时间: 2024-03-27 09:39:52 浏览: 59
好的,以下是一个使用STM32和PT100传感器测温的程序,使用查表法计算温度,测温范围为-120℃到80℃,使用HAL库:
```c
#include "main.h"
#include <math.h>
#define TABLE_SIZE 201
#define V_REF 3.3f
#define R_REF 100.0f
#define R0 100.0f
#define A 3.9083e-3f
#define B -5.775e-7f
float temperature_table[TABLE_SIZE] = {-120.0f, -119.0f, -118.0f, /* ... */ , 78.0f, 79.0f, 80.0f}; // 温度表格
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
int get_adc_value(void)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
return HAL_ADC_GetValue(&hadc1);
}
float get_resistance(int adc_value)
{
float v = (float)adc_value * V_REF / 4096.0f;
return (v * R_REF) / (V_REF - v);
}
float get_temperature(float resistance)
{
float t = 0.0f;
float r = resistance / R0;
t = (-A + sqrtf(A * A - 4 * B * (1 - r))) / (2 * B);
return t;
}
float lookup_temperature(float resistance)
{
int i = 0;
for(i = 0; i < TABLE_SIZE; i++)
{
if(resistance > (float)i)
{
break;
}
}
if(i == 0)
{
return temperature_table[0];
}
else if(i >= TABLE_SIZE)
{
return temperature_table[TABLE_SIZE - 1];
}
else
{
float x1 = (float)(i - 1);
float x2 = (float)i;
float y1 = temperature_table[i - 1];
float y2 = temperature_table[i];
float k = (y2 - y1) / (x2 - x1);
float b = y1 - k * x1;
return k * resistance + b;
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_ADC1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
while (1)
{
int adc_value = get_adc_value();
float resistance = get_resistance(adc_value);
float temperature = lookup_temperature(resistance);
// do something with temperature
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 180;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
```
这个程序使用了STM32的ADC模块来读取PT100传感器的电阻值,并使用查表法将电阻值转换为温度。注意,在程序中需要设置正确的ADC通道和引脚配置,以及正确的温度表格。此外,程序中还包含了时钟配置函数,以确保系统时钟正确工作。
阅读全文