Created
August 1, 2020 14:39
-
-
Save jorgeacortes/47f306f71ffee2ca94f1e8e718b8a1f8 to your computer and use it in GitHub Desktop.
Converts ADC value of the internal temperature sensor of STM32 microcontroller to degrees Celsius taking into account calibration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @brief Converts ADC_CHANNEL_TEMPSENSOR ADC value to Temperature in ºC. | |
* @param[in] sample ADC_CHANNEL_TEMPSENSOR value read. | |
* @return temperature in float. | |
* @note This temperature sensor measures junction temperature of the microcontroller. | |
* */ | |
float temp_calc(uint16_t sample) { | |
/* Factory calibrated values. Address for STM32F072x8 and STM32F072xB. Check datasheet for each microcontroller */ | |
const uint16_t ADC_TEMP_3V3_30C = *((uint16_t *)(0x1FFFF7B8)); | |
const float TEMP_30C = 30.0F; | |
const uint16_t ADC_TEMP_3V3_110C = *((uint16_t *)(0x1FFFF7C2)); | |
const float TEMP_110C = 110.0F; | |
const float CALIBRATION_REFERENCE_VOLTAGE = 3.3F; | |
const float REFERENCE_VOLTAGE = 3.3F; // Supply voltage at Vref+ or VDDA. Change accordingly. | |
/* Scaling calibrated values according to our reference voltage */ | |
float kAdcCalValue30 = (float)(ADC_TEMP_3V3_30C) * | |
(REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE); | |
float kAdcCalValue110 = (float)(ADC_TEMP_3V3_110C) * | |
(REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE); | |
/* Calculate temperature*/ | |
float temperature = ((float)(sample)-kAdcCalValue30) / | |
(kAdcCalValue110 - kAdcCalValue30) * (TEMP_110C - TEMP_30C) + | |
TEMP_30C; | |
return temperature; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment