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

编程入门 行业动态 更新时间:2024-10-14 14:14:35

基于stm32f429,利用内置温度传感器和ADC<a href=https://www.elefans.com/category/jswz/34/1753653.html style=外设,测量芯片工作温度"/>

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

说明

基于stm32f429,利用内置温度传感器和ADC外设,测量芯片工作温度,再通过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  ;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外设,测量芯片工作温度

本文发布于:2024-03-23 16:51:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1740539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:外设   工作温度   测量   芯片   温度传感器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!