![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
main.cpp@0:dfdf46225b14, 2016-03-27 (annotated)
- Committer:
- hudakz
- Date:
- Sun Mar 27 21:46:25 2016 +0000
- Revision:
- 0:dfdf46225b14
- Child:
- 1:09a5e83ea18f
Initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:dfdf46225b14 | 1 | // Reading internal temperature sensor of STM32F103RB chip |
hudakz | 0:dfdf46225b14 | 2 | |
hudakz | 0:dfdf46225b14 | 3 | #include "mbed.h" |
hudakz | 0:dfdf46225b14 | 4 | |
hudakz | 0:dfdf46225b14 | 5 | // See STM32F103x datasheet, chapter 5.3.19 "Temperature sensor characteristics", Table 50. "TS characteristics" |
hudakz | 0:dfdf46225b14 | 6 | const float AVG_SLOPE = 4.3E-03; |
hudakz | 0:dfdf46225b14 | 7 | const float V25 = 1.43; |
hudakz | 0:dfdf46225b14 | 8 | const float ACD_TO_VOLT = 3.3 / 4096; |
hudakz | 0:dfdf46225b14 | 9 | |
hudakz | 0:dfdf46225b14 | 10 | Serial pc(USBTX, USBRX); |
hudakz | 0:dfdf46225b14 | 11 | DigitalOut myled(LED1); |
hudakz | 0:dfdf46225b14 | 12 | ADC_HandleTypeDef hadc1; |
hudakz | 0:dfdf46225b14 | 13 | float temp; |
hudakz | 0:dfdf46225b14 | 14 | uint16_t adcValue; |
hudakz | 0:dfdf46225b14 | 15 | |
hudakz | 0:dfdf46225b14 | 16 | /* ADC1 init function */ |
hudakz | 0:dfdf46225b14 | 17 | extern "C" |
hudakz | 0:dfdf46225b14 | 18 | void MX_ADC1_Init(void) { |
hudakz | 0:dfdf46225b14 | 19 | ADC_ChannelConfTypeDef sConfig; |
hudakz | 0:dfdf46225b14 | 20 | |
hudakz | 0:dfdf46225b14 | 21 | /**Common config |
hudakz | 0:dfdf46225b14 | 22 | */ |
hudakz | 0:dfdf46225b14 | 23 | hadc1.Instance = ADC1; |
hudakz | 0:dfdf46225b14 | 24 | hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; |
hudakz | 0:dfdf46225b14 | 25 | hadc1.Init.ContinuousConvMode = DISABLE; |
hudakz | 0:dfdf46225b14 | 26 | hadc1.Init.DiscontinuousConvMode = DISABLE; |
hudakz | 0:dfdf46225b14 | 27 | hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
hudakz | 0:dfdf46225b14 | 28 | hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
hudakz | 0:dfdf46225b14 | 29 | hadc1.Init.NbrOfConversion = 1; |
hudakz | 0:dfdf46225b14 | 30 | HAL_ADC_Init(&hadc1); |
hudakz | 0:dfdf46225b14 | 31 | |
hudakz | 0:dfdf46225b14 | 32 | /**Configure Regular Channel |
hudakz | 0:dfdf46225b14 | 33 | */ |
hudakz | 0:dfdf46225b14 | 34 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
hudakz | 0:dfdf46225b14 | 35 | sConfig.Rank = 1; |
hudakz | 0:dfdf46225b14 | 36 | sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; |
hudakz | 0:dfdf46225b14 | 37 | HAL_ADC_ConfigChannel(&hadc1, &sConfig); |
hudakz | 0:dfdf46225b14 | 38 | } |
hudakz | 0:dfdf46225b14 | 39 | |
hudakz | 0:dfdf46225b14 | 40 | int main() { |
hudakz | 0:dfdf46225b14 | 41 | MX_ADC1_Init(); |
hudakz | 0:dfdf46225b14 | 42 | while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK); |
hudakz | 0:dfdf46225b14 | 43 | |
hudakz | 0:dfdf46225b14 | 44 | while(1) { |
hudakz | 0:dfdf46225b14 | 45 | myled = !myled; |
hudakz | 0:dfdf46225b14 | 46 | HAL_ADC_Start(&hadc1); |
hudakz | 0:dfdf46225b14 | 47 | while(HAL_ADC_PollForConversion(&hadc1, 0) != HAL_OK); |
hudakz | 0:dfdf46225b14 | 48 | adcValue = HAL_ADC_GetValue(&hadc1); |
hudakz | 0:dfdf46225b14 | 49 | temp = (V25 - adcValue * ACD_TO_VOLT) / AVG_SLOPE + 25.0f; |
hudakz | 0:dfdf46225b14 | 50 | pc.printf("temp = %3.1f%cC\n", temp, 176); |
hudakz | 0:dfdf46225b14 | 51 | wait_ms(1000); |
hudakz | 0:dfdf46225b14 | 52 | } |
hudakz | 0:dfdf46225b14 | 53 | } |