Measure the ambient temperature around the chip. The output voltage of the temperature sensor is proportional to the temperature. The obtained temperature is measured by ADC.
Inside the chip, the temperature sensor is connected to the ch16 of adc1. If you do not use a sensor, you can set it to the powerdown mode to save power.
Main features:
Temperature measurement range:-45 ~ + 125 degrees Celsius
Temperature Measurement Accuracy: ± 1. 5 degrees Celsius
Procedure:
1. Select adc1 ch16 as the input of the ADC.
2. Set the sampling time
3. Set the tsvrefe bit of adc1 register adc_cr to wake up the temperature sensor from the powerdown mode.
4. Start ADC Conversion
5. Read the conversion value and convert it to the voltage vsense, vsense = Value × 3300/4096 (MV)
6. Calculate the temperature.
Temperature Calculation formula: temp = (vsense-V25)/avg_slope) + 25
V25 is the value of vsense at 25 degrees, and avg_slope is the temperature conversion rate. The two values can be found in Datasheet.
V25 = 0.76 v avg_slope = 2.5 mV/°C
Temp = (vsense-760)/2500 + 25;
【CodeImplementation]
First, wake up the temperature sensor:
Adc_tempsensorvrefintcmd (enable );
Move the previousProgramTo 16 channels:
Adc_regularchannelconfig (adc1, adc_channel_16, 4, adc_sampletime_144cycles );
Read the converted value and use the above formula to calculate it:
AA = adcvalue [I]-760;
Printf ("Current temperature: % F degree Celsius \ r \ n", (aa)/2.5 + 25 );
Printf ("% d \ r \ n", adcvalue [I]);
Pay attention to the data type here. adcvalue defines uint16_t and is the unsigned number.
The temperature can be read here, but there is still a problem: the temperature is not accurate. The temperature I read at night is 68 degrees. I don't know if the internal temperature is high after the film has been working for a long time or my calculation is wrong.
However, the temperature does change with the temperature. When the board is powered off, the temperature will decrease, but it is still inaccurate.
Technorati flag: 440f4, ADC, Temperature Sensor