Reading temperature sensor built into STM32F103RB chips

Dependencies:   mbed

Revision:
4:f5c604b5eceb
Parent:
2:bd6594739176
Child:
5:27071c2166bb
--- a/main.cpp	Mon Mar 28 16:03:11 2016 +0000
+++ b/main.cpp	Tue Mar 29 16:51:33 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Reading built-in temperature sensor of STM32F103RB chip on NUCLEO-F103RB board
+ * Reading built-in temperature sensor of STM32F103RB chip (on a NUCLEO-F103RB board)
  */
 
 #include "mbed.h"
@@ -9,15 +9,17 @@
  * 5.3.19 Temperature sensor characteristics 
  * Table 50. TS characteristics, Page 80
  */
-const float         AVG_SLOPE   = 4.3E-03;
-const float         V25         = 1.43;
-const float         ACD_TO_VOLT = 3.3 / 4096;
+const float         AVG_SLOPE   = 4.3E-03;      // slope (gradient) of temperature line function  [V/°C]
+const float         V25         = 1.43;         // sensor's voltage at 25°C [V]
+const float         ADC_TO_VOLT = 3.3 / 4096;   // conversion coefficient of digital value to voltage [V] 
+                                                // when using 3.3V ref. voltage at 12-bit resolution (2^12 = 4096)
 
 Serial              pc(USBTX, USBRX);
 DigitalOut          led(LED1);
-ADC_HandleTypeDef   hadc1;
-uint16_t            adcValue;
-float               temp;
+ADC_HandleTypeDef   hadc1;                      // ADC handle
+uint16_t            adcValue;                   // digital value of sensor
+float               vSense;                     // sensor's output voltage [V]
+float               temp;                       // sensor's temperature [°C]
 
 /* ADC1 init function */
 void MX_ADC1_Init(void) {
@@ -43,20 +45,22 @@
 }
 
 int main() {
-    MX_ADC1_Init();
-    while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK);
+    MX_ADC1_Init();                                                 // initialize AD convertor
+    while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK);           // calibrate AD convertor
 
     while(1) {        
+        HAL_ADC_Start(&hadc1);                                      // start analog to digital conversion
+        while(HAL_ADC_PollForConversion(&hadc1, 1000000) != HAL_OK);// wait for completing the conversion
+        adcValue = HAL_ADC_GetValue(&hadc1);                        // read sensor's digital value
+        vSense = adcValue * ADC_TO_VOLT;                            // convert sensor's digital value to voltage [V]
         /*
          * STM32F103xx Reference Manual:
          * 11.10 Temperature sensor
          * Reading the temperature, Page 235
+         * Temperature (in °C) = {(V25 - Vsense) / Avg_Slope} + 25
          */
-        HAL_ADC_Start(&hadc1);
-        while(HAL_ADC_PollForConversion(&hadc1, 1000000) != HAL_OK);
-        adcValue = HAL_ADC_GetValue(&hadc1);
-        temp = (V25 - adcValue * ACD_TO_VOLT) / AVG_SLOPE + 25.0f;
-        pc.printf("temp = %3.1f%cC\n", temp, 176);
+        temp = (V25 - vSense) / AVG_SLOPE + 25.0f;                  // convert sensor's output voltage to temperature [°C]
+        pc.printf("temp = %3.1f%cC\n", temp, 176);                  // display chip's temperature [°C]
         led = !led;
         wait_ms(1000);
    }