锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

基于stm32f429,利用内置温度传感器和ADC外设,测量芯片工作温度

时间:2022-10-19 03:30:00 gd温湿度传感器igt202传感器

说明

基于stm32f使用内置温度传感器和429ADC外设,测量芯片的工作温度,然后通过DMA外设将ADC将外设数据传输到内存中, 使用串口打印输出。(串口部分驱动代码省略)

通过加热器测试,工作温度从24度上升到37度。

Flowchart

  1. 配置好ADC1外设。
  2. 配置好DMA
  3. 打开温度传感器
  4. 根据官方数据手册,使用以下公式打印到串口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  ;  DMAInitStructspan class="token punctuation">.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.0025f


extern __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);
	}

		

}
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章