stm32f103的内部温度测量代码
时间: 2023-09-20 21:10:51 浏览: 100
以下是基于STM32F103芯片的内部温度传感器测量代码示例:
首先,在CubeMX中启用内部温度传感器,并将其分配给一个ADC通道。假设我们将其分配给ADC1通道16:
![image1](https://img-blog.csdnimg.cn/20210916180935246.png)
然后在代码中初始化ADC1并启用DMA传输:
```c
/* ADC1 init function */
void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_16;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* Start ADC conversion on regular group with transfer by DMA */
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&ADC_val, 1) != HAL_OK)
{
Error_Handler();
}
}
```
在main函数中,我们还需要初始化时钟和DMA,并启用中断:
```c
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start_IT(&hadc1); // Enable ADC Interrupt
printf("Internal Temperature Measurement Example\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
```
最后,在ADC中断服务程序中读取ADC值并将其转换为温度值:
```c
/**
* @brief This function handles ADC1 global interrupt.
* @param None
* @retval None
*/
void ADC1_IRQHandler(void)
{
/* USER CODE BEGIN ADC1_IRQn 0 */
/* USER CODE END ADC1_IRQn 0 */
HAL_ADC_IRQHandler(&hadc1);
/* USER CODE BEGIN ADC1_IRQn 1 */
/* USER CODE END ADC1_IRQn 1 */
}
/**
* @brief Conversion complete callback in non blocking mode
* @param hadc: ADC handle
* @retval None
*/
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
/* Get ADC value */
uint16_t adc_val = ADC_val;
/* Convert ADC value to temperature in C */
float temp_in_c = ((float)adc_val * 3.3 / 4096 - 0.76) / 0.0025 + 25;
printf("Temperature: %0.2fC\n", temp_in_c);
/* Start next ADC conversion */
HAL_ADC_Start_IT(&hadc1);
}
/**
* @brief ADC error callback in non blocking mode
* @param hadc: ADC handle
* @retval None
*/
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
{
/* User can add their own implementation to report the ADC error */
Error_Handler();
}
```
上述代码将ADC值转换为温度值的公式为:
```
Temperature (C) = (ADC_value * Vref / 4096 - V25) / Avg_Slope + 25
```
其中,Vref为ADC参考电压(一般为3.3V),V25是芯片在25℃时的传感器输出电压(一般为0.76V),Avg_Slope是传感器输出电压随温度变化的平均斜率(一般为2.5mV/℃)。具体数值可以参考芯片数据手册。
阅读全文