reading internal temperature sensor of arch max and storing data

Committer:
tifo
Date:
Thu Sep 14 16:44:10 2017 +0000
Revision:
3:e98dbcd4f4e3
Parent:
0:62f36500cd41
another version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tifo 0:62f36500cd41 1 #include "mbed.h"
tifo 0:62f36500cd41 2 #include "TempBase.h"
tifo 0:62f36500cd41 3
tifo 0:62f36500cd41 4
tifo 0:62f36500cd41 5 TempBase::TempBase()
tifo 0:62f36500cd41 6 {
tifo 0:62f36500cd41 7 ambientTemp = 25.0f;
tifo 0:62f36500cd41 8 v25 = 760.0f;
tifo 0:62f36500cd41 9 avgSlope = 2.5f;
tifo 0:62f36500cd41 10 vRef = 3000.0f;
tifo 0:62f36500cd41 11 }
tifo 0:62f36500cd41 12
tifo 0:62f36500cd41 13
tifo 0:62f36500cd41 14 TempBase::TempBase(float correction)
tifo 0:62f36500cd41 15 {
tifo 0:62f36500cd41 16 ambientTemp = 25.0f + correction;
tifo 0:62f36500cd41 17 v25 = 760.0f;
tifo 0:62f36500cd41 18 avgSlope = 2.5f;
tifo 0:62f36500cd41 19 vRef = 3000.0f;
tifo 0:62f36500cd41 20 }
tifo 0:62f36500cd41 21
tifo 0:62f36500cd41 22 // ADC init function, call it 1 time during setup
tifo 0:62f36500cd41 23 void TempBase::Init()
tifo 0:62f36500cd41 24 {
tifo 0:62f36500cd41 25 hadc1.Instance = ADC1;
tifo 0:62f36500cd41 26 hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
tifo 0:62f36500cd41 27 hadc1.Init.Resolution = ADC_RESOLUTION12b;
tifo 0:62f36500cd41 28 hadc1.Init.ScanConvMode = DISABLE;
tifo 0:62f36500cd41 29 hadc1.Init.ContinuousConvMode = DISABLE;
tifo 0:62f36500cd41 30 hadc1.Init.DiscontinuousConvMode = DISABLE;
tifo 0:62f36500cd41 31 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
tifo 0:62f36500cd41 32 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
tifo 0:62f36500cd41 33 hadc1.Init.NbrOfConversion = 1;
tifo 0:62f36500cd41 34 hadc1.Init.DMAContinuousRequests = DISABLE;
tifo 0:62f36500cd41 35 hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
tifo 0:62f36500cd41 36 HAL_ADC_Init(&hadc1);
tifo 0:62f36500cd41 37
tifo 0:62f36500cd41 38 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
tifo 0:62f36500cd41 39 sConfig.Rank = 1;
tifo 0:62f36500cd41 40 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
tifo 0:62f36500cd41 41
tifo 0:62f36500cd41 42 HAL_ADC_ConfigChannel(&hadc1, &sConfig);
tifo 0:62f36500cd41 43 }
tifo 0:62f36500cd41 44
tifo 0:62f36500cd41 45 // returns raw adc value from sensor
tifo 0:62f36500cd41 46 int TempBase::GetRawTemp()
tifo 0:62f36500cd41 47 {
tifo 0:62f36500cd41 48 HAL_ADC_Start(&hadc1); // Start conversion
tifo 0:62f36500cd41 49
tifo 0:62f36500cd41 50 // Wait end of conversion and get value
tifo 0:62f36500cd41 51 if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)
tifo 0:62f36500cd41 52 return HAL_ADC_GetValue(&hadc1);
tifo 0:62f36500cd41 53 else return -1;
tifo 0:62f36500cd41 54 }
tifo 0:62f36500cd41 55
tifo 0:62f36500cd41 56 // takes raw value and convert it to temp
tifo 0:62f36500cd41 57 float TempBase::ConvertTemp(int value)
tifo 0:62f36500cd41 58 {
tifo 0:62f36500cd41 59 return (((value * vRef / 0xFFF) - v25) / avgSlope) + ambientTemp; // formula is from manual
tifo 0:62f36500cd41 60 }