Cortex_m3_stm32 Embedded Learning Note (17): Internal temperature Sensor (ADC acquisition)

Source: Internet
Author: User

The STM32 has an internal temperature sensor that can be used to measure the CPU and ambient temperature (TA). The temperature sensor is connected internally to the Adcx_in16 input channel, which converts the voltage from the sensor output to a digital value. The recommended sampling time for temperature sensor analog inputs is 17.1μs. The STM32 's internal temperature sensor supports a temperature range of:-40~125度 with a precision of around ±1.5℃ (The actual effect is not very well)。
The effect is really not very well. The big winter test is 20 degrees.

The use of the STM32 internal temperature sensor is simple, as long as the internal ADC is set up and the internal channel is activated, it is not much worse. With regard to the ADC settings, we have introduced the previous chapter in detail, and we will not say much more. Let's take a look at 2 places related to temperature sensor settings.


First place, we want to use the STM32 internal temperature sensor, you must first activate the ADC's internal channel, here second place, stm32 The internal temperature sensor is fixedly connected on channel 16 of the ADC , so we set t (℃) ={( V25-vsense)/avg_slope}+25
v25= Vsense value at 25 degrees ( typical: 1.43 ).
avg_slope= the average slope of the temperature and Vsense curve (in Mv/℃ or Uv/℃) ( typical: 4.3mv/℃ ).
using the above formula, we can easily calculate the temperature of the current temperature sensor.


The ADC in the previous section reads the value of the external channel, The internal temperature sensor is equivalent to connecting the channel port to the internal temperature sensor .

Configuring the ADC is roughly similar to the previous section, except that you do not have to initialize the external IO, but add a statement that turns on the internal temperature sensor. Temperate.c

#include "temperate.h" void T_adc_init (void) {adc_inittypedef adc_ist; Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_adc1,enable)//72M/6=12,ADC maximum time cannot exceed 14mrcc_adcclkconfig (RCC_PCLK2_DIV6);//Set ADC divider factor 6adc_deinit ( ADC1);//reset ADC1 to set all the registers of the peripheral ADC1 to the default value adc_ist. Adc_mode= ADC_MODE_INDEPENDENT;//ADC Standalone mode adc_ist. adc_scanconvmode=disable;//single channel mode adc_ist. adc_continuousconvmode=disable;//single Conversion Mode//conversion is triggered by the software rather than by the external trigger adc_ist. Adc_externaltrigconv=adc_externaltrigconv_none; Adc_ist. ADC_DATAALIGN=ADC_DATAALIGN_RIGHT;//ADC Data Right-aligns the adc_ist. adc_nbrofchannel=1;//number of ADC channels with sequential rules conversion adc_init (adc1,&adc_ist);  Adc_tempsensorvrefintcmd (ENABLE); Turn on the internal temperature sensor Adc_cmd (adc1,enable);//enable the specified adc1adc_resetcalibration (ADC1);//Turn on reset calibration while (adc_ Getresetcalibrationstatus (ADC1));//wait for the reset calibration to end adc_startcalibration (ADC1);//Turn on AD calibration while (Adc_getcalibrationstatus ( ADC1))///wait for calibration to end}//get ADC value//ch: Channel value 0~3u16 T_GET_ADC (U8 ch) {//Set the rule group channel of the specified ADC set their conversion order and sampling time Adc_regularchannelconfig ( ADC1,CH,1,ADC_SAMPLETIME_239CYCLES5); Adc_softwarestArtconvcmd (ADC1, enable);//enable the ADC1 software conversion function to be specified while (! Adc_getflagstatus (ADC1,ADC_FLAG_EOC));//wait for the conversion to end return Adc_getconversionvalue (ADC1);//Returns the result of the most recent ADC1 rule group}u16 T_get _temp (void) {U16 temp_val=0;u8 i;for (i=0;i<10;i++) {TEMP_VAL+=T_GET_ADC (adc_channel_16);d Elay_ms (5);} return TEMP_VAL/10;} U16 T_get_adc_average (U8 ch,u8 times) {u32 tem_val=0;u8 i;for (i=0;i<times;i++) {TEM_VAL+=T_GET_ADC (ch);d Elay_ms (5) ;} return tem_val/times;}
Temperate.h
#ifndef _temp_h#define _temp_h#include "sys.h" #include "delay.h" #define ADC_CH_TEMP  adc_channel_16//temperature sensor channel void T_adc_init (void); U16 T_GET_ADC (U8 ch); U16 t_get_temp (void); U16 t_get_adc_average (U8 ch,u8 times); #endif

The main function is modified on the basis of the previous section:

#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" #include "temperate.h" void init ()     {Delay_init (); The delay function initializes the uart_init (9600);  The serial port is initialized to 9600led_init (); Initialize the hardware interface with the LED connection lcd_init (); T_adc_init (); point_color=red;//set the font to Red lcd_showstring (60,40,200,24,24, "ADC Test ^-^"); Lcd_showstring (60,70,200,16,16, "Medium difficulty"); Lcd_showstring (60,90,200,16,16, "2015/1/25"); Lcd_showstring (60,110,200,16,16, "By--mr yh");//Display message point_color=blue;//set font to Blue lcd_showstring (60,130,200,16,16,      "Temp_val:");      Lcd_showstring (60,150,200,16,16, "temp_vol:0.000v");  Lcd_showstring (60,170,200,16,16, "temperate:00.00c"); }int Main (void) {U16 adcnum;float tem,temperate;init (); while (1) {adcnum=t_get_adc_average (adc_ch_temp,10); Lcd_showxnum (132,130,adcnum,4,16,0);//Displays the value of the ADC tem= (float) adcnum* (3.3/4096); temperate=tem;adcnum=tem; Lcd_showxnum (132,150,adcnum,1,16,0);//display voltage value of the whole number of tem-= (U8) tem; Lcd_showxnum (148,150, (u32) (tem*1000), 3,16,0x80);//display voltage value of the decimal place temperate= (1.43-temperate)/0.0043+25;//Calculate the current temperature value Lcd_showxnum (140,170, (U8) temperate,2,16,0); Display temperature integer part temperate-= (u8) temperate; Lcd_showxnum (164,170,temperate*100,2,16,0x80);//display temperature of the small part of the led0=! Led0;delay_ms (250);}}



Cortex_m3_stm32 Embedded Learning Note (17): Internal temperature Sensor (ADC acquisition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.