新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 之 內(nèi)部溫度傳感器

STM32 之 內(nèi)部溫度傳感器

作者: 時間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
和ADC的設(shè)置基本相同,算是ADC的擴(kuò)展應(yīng)用。

只是在計(jì)算方法上有變化,在adc的初始化程序里面做一些變動就可以了。

本文引用地址:http://butianyuan.cn/article/201612/325139.htm

包含函數(shù):

(1)Main

C語言:Codee#14690

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 實(shí)驗(yàn)平臺 : ST 官方三合一套件
+ 硬件 : STM32F103C8T6
+ 開發(fā)平臺 : IAR For ARM 5.40
+ 仿真器 : J-Link
+ 日期 : 2010-10-28
+ 頻率 :HSE = 8MHz ,主頻 = 72MHz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"
#include "stdio.h"
/*******************************************************************************
== 變量聲明 ==
*******************************************************************************/
floatADC_Value,Tem;
unsignedchara=0;
unsignedcharb=0;
unsignedcharc=0;
unsignedchard=0;

/*******************************************************************************
== Main 函數(shù) ==
*******************************************************************************/
voidmain(void)
{
//---- 初始化 ------------------------------------------------------
RCC_Configuration();//配置系統(tǒng)時鐘
NVIC_Configuration();//配置 NVIC 和 Vector Table
SysTick_Config();//配置SysTick的精確延時

GPIO_Configuration();
UART1_Configuration();
AD_Configration();
DMA_Configration();


//---- 任務(wù)開始 ----------------------------------------------------
LED1_HIGH;LED2_HIGH;LED3_HIGH;LED4_HIGH;// 初始化讓燈全滅

Uart1_PutString("===== douzi STM32 @ Temperature =====rn",39);

while(1)
{// 這里只選采集的10個數(shù)據(jù)中的一個,應(yīng)該做一些算法進(jìn)行濾波才好
ADC_Value=(float)(sys_analog[5])*330/409600;// 計(jì)算公式datasheet上可以找到,但是我沒找到。
Tem=(1.42-ADC_Value)*1000/4.35+25;

Tem=Tem*100;// ADC是12位的,這里數(shù)據(jù)類型轉(zhuǎn)換有問題
a=Tem/1000;
b=(Tem-a*1000)/100;
c=(Tem-a*1000-b*100)/10;
d=Tem-a*1000-b*100-c*10;

Uart1_PutChar(a+0);
Uart1_PutChar(b+0);
Uart1_PutString(".",1);
Uart1_PutChar(c+0);
Uart1_PutChar(d+0);
Uart1_PutString(" Cn",3);

Delay_Ms(1000);
}
}

(2)在ADC初始化函數(shù)里要變動些內(nèi)容,關(guān)于溫度傳感器

C語言:Codee#14691
/*******************************************************************************
* Function Name : AD_Configration
* Description : Configures the ADC1
* Input : None
* Output : None
* Return : None
*******************************************************************************/
voidAD_Configration(void)
{
ADC_InitTypeDefADC_InitStructure_ADC1;

/* Resets ADC1 */
ADC_DeInit(ADC1);

ADC_InitStructure_ADC1.ADC_Mode=ADC_Mode_Independent;// 配置ADC1 工作在獨(dú)立模式
ADC_InitStructure_ADC1.ADC_ScanConvMode=ENABLE;// 配置ADC1 模數(shù)轉(zhuǎn)換工作在掃描模式(多通道模式)
ADC_InitStructure_ADC1.ADC_ContinuousConvMode=ENABLE;// 配置ADC1 模數(shù)轉(zhuǎn)換工作在連續(xù)模式
ADC_InitStructure_ADC1.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;// 配置ADC1 模數(shù)轉(zhuǎn)換有軟件方式啟動而非中斷方式
ADC_InitStructure_ADC1.ADC_DataAlign=ADC_DataAlign_Right;// 配置ADC1 模數(shù)轉(zhuǎn)換數(shù)據(jù)對齊方式為右對齊
ADC_InitStructure_ADC1.ADC_NbrOfChannel=1;// 配置ADC1 模數(shù)轉(zhuǎn)換的通道數(shù)目 為 1個通道
ADC_Init(ADC1,&ADC_InitStructure_ADC1);// 配置ADC1 初始化
//常規(guī)轉(zhuǎn)換序列1:通道10
ADC_RegularChannelConfig(ADC1,ADC_Channel_16,1,ADC_SampleTime_239Cycles5);// 配置為 ADC1,通道1,1個通道,采樣時間為239.5個周期,周期越長采集的信號越準(zhǔn)越穩(wěn)
// 對應(yīng)的管教所對應(yīng)的ADC通道時對應(yīng)的,一定不要搞錯!
/* Enable the temperature sensor and vref internal channel */
ADC_TempSensorVrefintCmd(ENABLE);// 使能溫度傳感器內(nèi)部參考電壓通道

ADC_DMACmd(ADC1,ENABLE);// 使能ADC1的DMA請求

ADC_Cmd(ADC1,ENABLE);// 使能ADC1

/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);// 重置ADC1的校準(zhǔn)寄存器
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));// 檢測是否重置完畢

/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);// 開始校準(zhǔn) ADC1
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));// 檢測是否校準(zhǔn)完畢

/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1,ENABLE);// 軟件使能ADC1的轉(zhuǎn)換
}




關(guān)鍵詞: STM32內(nèi)部溫度傳感

評論


技術(shù)專區(qū)

關(guān)閉