×
单片机 > 单片机程序设计 > 详情

STM32 之 内部温度传感器

发布时间:2020-06-16 发布时间:
|
和ADC的设置基本相同,算是ADC的扩展应用。

只是在计算方法上有变化,在adc的初始化程序里面做一些变动就可以了。

包含函数:

(1)Main

 
C语言: Codee#14690

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 实验平台 : ST 官方三合一套件 
+ 硬件     : STM32F103C8T6
+ 开发平台 : IAR For ARM 5.40
+ 仿真器   : J-Link
+ 日期     : 2010-10-28
+ 频率     :HSE = 8MHz ,主频 = 72MHz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"
#include "stdio.h"
/*******************************************************************************
                             == 变量声明 == 
*******************************************************************************/
float ADC_Value,Tem;
unsigned char a= 0;
unsigned char b= 0;
unsigned char c= 0;
unsigned char d= 0;

/*******************************************************************************
                             == Main 函数 == 
*******************************************************************************/
void main(void)

//---- 初始化 ------------------------------------------------------
RCC_Configuration();    //配置系统时钟 
NVIC_Configuration();   //配置 NVIC 和 Vector Table 
SysTick_Config();       //配置SysTick的精确延时

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


//---- 任务开始 ---------------------------------------------------- 
LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; // 初始化让灯全灭

Uart1_PutString("===== douzi STM32 @ Temperature =====\r\n",39);

while (1)
{                                                      // 这里只选采集的10个数据中的一个,应该做一些算法进行滤波才好
         ADC_Value = (float)(sys_analog[5])*330/409600; // 计算公式datasheet上可以找到,但是我没找到。
         Tem = (1.42 - ADC_Value)*1000/4.35 + 25;
       
         Tem = Tem*100 ;                    // ADC是12位的,这里数据类型转换有问题
         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(" C\n",3);
          
          Delay_Ms(1000);     
}
}

(2)在ADC初始化函数里要变动些内容,关于温度传感器的

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

/* Resets ADC1 */ 
ADC_DeInit(ADC1);

ADC_InitStructure_ADC1.ADC_Mode = ADC_Mode_Independent;                        // 配置ADC1 工作在独立模式
ADC_InitStructure_ADC1.ADC_ScanConvMode = ENABLE;                              // 配置ADC1 模数转换工作在扫描模式(多通道模式)
ADC_InitStructure_ADC1.ADC_ContinuousConvMode = ENABLE;                        // 配置ADC1 模数转换工作在连续模式
ADC_InitStructure_ADC1.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;       // 配置ADC1 模数转换有软件方式启动而非中断方式
ADC_InitStructure_ADC1.ADC_DataAlign = ADC_DataAlign_Right;                    // 配置ADC1 模数转换数据对齐方式为右对齐
ADC_InitStructure_ADC1.ADC_NbrOfChannel = 1;                                   // 配置ADC1 模数转换的通道数目 为 1个通道
ADC_Init(ADC1, &ADC_InitStructure_ADC1);                                       // 配置ADC1 初始化
   //常规转换序列1:通道10
ADC_RegularChannelConfig(ADC1, ADC_Channel_16,1, ADC_SampleTime_239Cycles5);   // 配置为 ADC1,通道1,1个通道,采样时间为239.5个周期,周期越长采集的信号越准越稳
                                                                                // 对应的管教所对应的ADC通道时对应的,一定不要搞错!
   /* Enable the temperature sensor and vref internal channel */ 
ADC_TempSensorVrefintCmd(ENABLE);                          // 使能温度传感器内部参考电压通道

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

    /* Enable ADC1 reset calibaration register */   
    ADC_ResetCalibration(ADC1);                                // 重置ADC1的校准寄存器
    /* Check the end of ADC1 reset calibration register */
    while(ADC_GetResetCalibrationStatus(ADC1));                // 检测是否重置完毕            

    /* Start ADC1 calibaration */
    ADC_StartCalibration(ADC1);                                // 开始校准 ADC1
    /* Check the end of ADC1 calibration */
    while(ADC_GetCalibrationStatus(ADC1));                     // 检测是否校准完毕
       
    /* Start ADC1 Software Conversion */ 
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);                    // 软件使能ADC1的转换
}


关键字:STM32  内部温度传感器

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
基于AT91M42800A的LED显示系统设计