本文主要是介绍基于stm32f429,利用内置温度传感器和ADC外设,测量芯片工作温度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说明
基于stm32f429,利用内置温度传感器和ADC外设,测量芯片工作温度,再通过DMA外设将ADC外设数据传到内存中, 利用串口打印输出。(串口部分驱动代码省略)
通过暖风机测试,工作温度从24度左右上升至37度。
Flowchart
- 配置好ADC1外设。
- 配置好DMA
- 开启温度传感器
- 利用如下公式打印到串口,根据官方数据手册V25 = 0.76 V, Avg_Slope =0.0025 V/C 因为是内置设备无需配置任何的GPIO
ADC相关头文件
#ifndef __BSP_ADC_H
#define __BSP_ADC_H#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_adc.h"#define Temp_sensor_channel ADC_Channel_18
#define ADC1_Channel DMA_Channel_0#define ADC1_DR_ADDR ((uint32_t)ADC1+0x4C)void Temp_Sensor_ADC_Init(void);
ADC相关配置源文件
#include "bsp_adc.h"__IO uint16_t ADC_ConvertedValue;void Temp_Sensor_ADC_Config(void)
{/*初始化DMA, ADC, ADC_Common结构体*/ADC_InitTypeDef ADC_InitStruct;DMA_InitTypeDef DMAInitStruct;ADC_CommonInitTypeDef ADC_CommonInitStruct;/**************配置DMA************/RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);/*DMA配置如下*/DMAInitStruct.DMA_BufferSize = 1;DMAInitStruct.DMA_Channel = DMA_Channel_0;DMAInitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;DMAInitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable ;DMAInitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full ;DMAInitStruct.DMA_Memory0BaseAddr = (uint32_t)&(ADC_ConvertedValue);DMAInitStruct.DMA_MemoryBurst =DMA_MemoryBurst_Single ;DMAInitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ;DMAInitStruct.DMA_MemoryInc =DMA_MemoryInc_Disable ;DMAInitStruct.DMA_Mode = DMA_Mode_Circular ;DMAInitStruct.DMA_PeripheralBaseAddr = ADC1_DR_ADDR;DMAInitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single ;DMAInitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ;DMAInitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMAInitStruct.DMA_Priority =DMA_Priority_High;DMA_Init(DMA2_Stream0, &DMAInitStruct);DMA_Cmd(DMA2_Stream0, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);/*****************ADC_Common配置如下***********************/ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div4;ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;ADC_CommonInit(&ADC_CommonInitStruct);ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStruct.ADC_ExternalTrigConv =ADC_ExternalTrigConv_T1_CC1 ;ADC_InitStruct.ADC_ExternalTrigConvEdge =ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_NbrOfConversion = 1;ADC_InitStruct.ADC_ScanConvMode = DISABLE;ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;ADC_Init(ADC1, &ADC_InitStruct);ADC_RegularChannelConfig(ADC1,Temp_sensor_channel,1,ADC_SampleTime_56Cycles );ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);ADC_DMACmd(ADC1, ENABLE);ADC_Cmd(ADC1, ENABLE);ADC_TempSensorVrefintCmd(ENABLE); ADC_SoftwareStartConv(ADC1);}void Temp_Sensor_ADC_Init(void){Temp_Sensor_ADC_Config();
}
Main函数
#include "stm32f4xx.h"
#include "bsp_usart.h"
#include "bsp_adc.h"#define V25 0.76f
#define Avg_Slope 0.0025fextern __IO uint16_t ADC_ConvertedValue;
static void Delay(__IO uint32_t nCount) //简单的延时函数
{for(; nCount != 0; nCount--);
}int main(void){Usart_Config();Temp_Sensor_ADC_Init();while(1){float data =(float) (ADC_ConvertedValue)/4096* (float)3.3;float temp = (data-V25)/Avg_Slope+25;printf("\r\n The current temp = %f C \r\n",temp); Delay(0xFFFFFF);}}
这篇关于基于stm32f429,利用内置温度传感器和ADC外设,测量芯片工作温度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!