1、STM32内部温度传感器计算
STM32内置一个温度传感器,通过 ADC_IN16这个通道可以读出温度传感器的电压。其中给出了一个计算公式:
Temperature (in ℃) = {(V25- Vsense) / Avg_Slope} + 25
(1). 公式中的 Vsense 就是在 ADC_IN16读到的数值。单位是V。
(2). Avg_Slope 就是温度与 ADC 数值转换的斜率。 最小=4.0 典型=4.3 最大=4.6 单位是 mV/℃
(3). V25 最小=1.34V 典型=1.43V 最大=1.52V
2、STM32内部温度传感器使用
(1). STM32内部温度传感器与 ADC 的通道16相连,与 ADC 配合
使用实现温度测量;
(2). 测量范围–40~125℃,精度±1.5℃。
(3). 温度传感器产生一个随温度线性变化的电压,转换范围在
2V < VDDA < 3.6V之间。
3、程序例程:
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:main.c 4 * 文件功能:主函数文件 5 * 6 ***************************************************************************************************************************************/ 7 8 #include "pbdata.h"//调用自定义公共函数库 9 10 11 /********************************************************************************* 12 * 13 * 初始化操作 14 * 15 *********************************************************************************/ 16 void RCC_Configuration(void);//系统时钟初始化函数声明 17 void GPIO_Configuration(void);//GPIO初始化函数声明 18 void NVIC_Configuration(void);//中断优先级配置函数声明 19 void USART_Configuration(void);//串口配置函数声明 20 void ADC_Configuration(void);//ADC配置函数声明 21 22 23 24 /******************************************************************************** 25 * 26 * 函数名称:main(void) 27 * 函数功能:主函数 28 * 函数说明:不能用void定义主函数 29 * 30 ********************************************************************************/ 31 int main(void)//void不能void定义主函数 32 { 33 34 u32 ad=0;//取50次转换平均值 35 u8 i=0;//循环50次 36 37 RCC_Configuration(); //系统时钟初始化 38 39 GPIO_Configuration();//端口初始化 40 41 USART_Configuration();//串口配置 42 43 NVIC_Configuration(); 44 45 ADC_Configuration();//ADC配置 46 47 48 while(1) 49 { 50 ad=0; 51 for(i=0;i<50;i++) 52 { 53 ADC_SoftwareStartConvCmd(ADC1,ENABLE);//启动AD转换 54 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//等待转换完成 55 ad+= ADC_GetConversionValue(ADC1); 56 } 57 58 ad=ad/50; 59 printf("temp=%.3f\n",(1.43-3.3/4095*ad)/0.0043+25); 60 61 delay_ms(3000); 62 63 } 64 65 } 66 67 68 69 70 /******************************************************************************** 71 * 72 * 函数名称:RCC_Configuration(void) 73 * 函数功能:系统时钟高初始化函数 74 * 75 ********************************************************************************/ 76 void RCC_Configuration(void)//系统时钟高初始化函数 77 { 78 79 SystemInit();//系统初始化 80 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 81 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 82 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 83 84 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//adc对应GPIO时钟使能 85 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//ADC1时钟使能 86 RCC_ADCCLKConfig(RCC_PCLK2_Div6);//ADC频率不超过14M,所以对它分频,72/6=12 87 88 } 89 90 91 92 /******************************************************************************* 93 * 94 * 函数名称:GPIO_Configuration(void) 95 * 函数功能:GPIO初始化函数 96 * 97 ********************************************************************************/ 98 99 void GPIO_Configuration(void)//GPIO初始化函数 100 { 101 102 103 /*串口引脚配置*/ 104 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 105 106 /*输出引脚配置*/ 107 /*结构体变量赋值*/ 108 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 109 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 110 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 111 /*对应的GPIO初始化*/ 112 GPIO_Init(GPIOA,&GPIO_InitStructure); 113 114 115 /*输入引脚配置*/ 116 /*结构体变量赋值*/ 117 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 118 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 119 /*对应的GPIO初始化*/ 120 GPIO_Init(GPIOA,&GPIO_InitStructure); 121 122 /*ADC输入引脚配置*/ 123 /*结构体变量赋值*/ 124 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;////引脚配置adc 125 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//模拟输入 126 /*对应的GPIO初始化*/ 127 GPIO_Init(GPIOC,&GPIO_InitStructure); 128 129 130 131 132 } 133 134 135 /******************************************************************************** 136 * 137 * 函数名称:NVIC_Configuration(void) 138 * 函数功能:配置中断优先级 139 * 140 ********************************************************************************/ 141 142 void NVIC_Configuration(void) 143 { 144 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 145 146 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 147 148 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 149 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 150 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 151 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 152 153 NVIC_Init(&NVIC_InitStructure);//初始化 154 } 155 156 157 /********************************************************************************* 158 * 159 * 函数名称:USART_Configuration(void) 160 * 函数功能:串口配置函数 161 * 162 *********************************************************************************/ 163 void USART_Configuration(void) 164 { 165 /*定义串口配置结构体变量*/ 166 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 167 168 169 /*结构体变量赋值*/ 170 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 171 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 172 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 173 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 174 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 175 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 176 177 178 /*发送串口配置初始化*/ 179 USART_Init(USART1, &USART_InitStructure); 180 181 182 /*打开串口接收中断*/ 183 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 184 185 186 /*打开串口*/ 187 USART_Cmd(USART1,ENABLE);//串口使能,打开 188 189 /*清空中断标志位*/ 190 USART_ClearFlag(USART1,USART_FLAG_TC); 191 } 192 193 194 195 /********************************************************************************* 196 * 197 * 函数名称:ADC_Configuration(void) 198 * 函数功能:串口配置函数 199 * 200 *********************************************************************************/ 201 void ADC_Configuration(void) 202 { 203 204 /*定义一个ADC配置结构体变量*/ 205 ADC_InitTypeDef ADC_InitStructure; 206 207 208 /*对结构体变量赋值*/ 209 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式 210 ADC_InitStructure.ADC_ScanConvMode = DISABLE;//单通道模式 211 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//单次采集 212 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件触发 213 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对其 214 ADC_InitStructure.ADC_NbrOfChannel = 1;//转换的通道,只取1个通道 215 216 /*ADC初始化*/ 217 ADC_Init(ADC1, &ADC_InitStructure); 218 219 220 /*规则组*/ 221 ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1,ADC_SampleTime_239Cycles5); //,选择ADC1,温度传感器只能通道16,通道数1,采样时间239.5 222 223 224 /*使能温度传感器*/ 225 ADC_TempSensorVrefintCmd(ENABLE); 226 227 228 /*使能ADC*/ 229 ADC_Cmd(ADC1, ENABLE); 230 231 232 /*校准ADC*/ 233 ADC_ResetCalibration(ADC1);//对应ADC复位 234 while(ADC_GetResetCalibrationStatus(ADC1));//获取状态,等待复位完成 235 ADC_StartCalibration(ADC1);//开始指定ADC校准状态 236 while(ADC_GetCalibrationStatus(ADC1));//获取校准状态,等待完成 237 238 239 /*启动ADC*/ 240 ADC_SoftwareStartConvCmd(ADC1,ENABLE); 241 242 243 } 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
1 /**************************************************************************************************************** 2 * 3 * 文件名称:pbdata.c 4 * 文件功能:自定义函数或者全局变量的初始化 5 * 6 ****************************************************************************************************************/ 7 8 /*头文件声明*/ 9 #include "pbdata.h" 10 11 12 13 14 /******************************************************************************************** 15 * 16 * 自定义全局变量 17 * 18 ********************************************************************************************/ 19 u8 dt=0; 20 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 32 33 /************************************************** 34 * 35 * 函数名称:delay_us(u32 nus) 36 * 函数功能:微秒延时函数 37 * 输入参数:输入值为延时us 38 * 39 ***************************************************/ 40 void delay_us(u32 nus) 41 { 42 u32 temp; 43 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 44 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 45 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 46 47 do 48 { 49 temp=SysTick->CTRL;//标志位,等到一直减到0 50 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 51 52 SysTick->CTRL=0x00; //关闭计数器 53 SysTick->VAL =0X00; //清空计数器 54 } 55 56 57 58 59 60 61 62 /*************************************************** 63 * 64 * 函数名称:delay_ms(u16 nms) 65 * 函数功能:毫秒级延时 66 * 输入参数:输入值位延时ms 67 * 68 ****************************************************/ 69 void delay_ms(u16 nms) 70 { 71 u32 temp; 72 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 73 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 74 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 75 76 do 77 { 78 temp=SysTick->CTRL;//标志位,等到一直减到0 79 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 80 81 SysTick->CTRL=0x00; //关闭计数器 82 SysTick->VAL =0X00; //清空计数器 83 } 84 85 86 87 /**************************************************** 88 * 89 * 重定义printf函数部分 90 * 91 ****************************************************/ 92 int fputc(int ch,FILE *F) 93 { 94 95 USART_SendData(USART1,(u8)ch); 96 97 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 98 99 return ch; 100 }
1 /*pbdata.h*/ 2 /*************************************************************************************************** 3 * 4 * 文件名称:pbdata.h 5 * 文件功能:自定义的函数和全局变量的声明头文件 6 * 7 ***************************************************************************************************/ 8 9 #ifndef _pbdata_H 10 #define _pbdata_H 11 12 13 14 15 16 /******************************************************************** 17 * 18 * 调用的头文件放在这里 19 * 20 ********************************************************************/ 21 #include "stm32f10x.h" 22 23 24 #include "stm32f10x_rcc.h" 25 #include "stm32f10x_gpio.h" 26 #include "misc.h" 27 #include "stm32f10x_adc.h" 28 #include "stm32f10x_bkp.h" 29 #include "stm32f10x_can.h" 30 #include "stm32f10x_cec.h" 31 #include "stm32f10x_dac.h" 32 #include "stm32f10x_dbgmcu.h" 33 #include "stm32f10x_dma.h" 34 #include "stm32f10x_exti.h" 35 #include "stm32f10x_flash.h" 36 #include "stm32f10x_fsmc.h" 37 #include "stm32f10x_i2c.h" 38 #include "stm32f10x_iwdg.h" 39 #include "stm32f10x_pwr.h" 40 #include "stm32f10x_rtc.h" 41 #include "stm32f10x_sdio.h" 42 #include "stm32f10x_spi.h" 43 #include "stm32f10x_tim.h" 44 #include "stm32f10x_usart.h" 45 #include "stm32f10x_wwdg.h" 46 47 48 #include "stdio.h" 49 50 51 52 53 /******************************************************************** 54 * 55 * 自定义全局变量声明 56 * 57 ********************************************************************/ 58 extern u8 dt; 59 60 61 62 63 64 /******************************************************************** 65 * 66 * 自定义全函数声明 67 * 68 ********************************************************************/ 69 void delay(u32 nCount); 70 void delay_us(u32 nus); 71 void delay_ms(u16 nms); 72 int fputc(int ch,FILE *F); 73 74 75 76 #endif
4、工程下载:
http://download.csdn.net/detail/a1181803348/8765423