Aleksandr Koptevtsov / arch_temp_stack
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TempBase.cpp Source File

TempBase.cpp

00001 #include "mbed.h"
00002 #include "TempBase.h"
00003 
00004 
00005 TempBase::TempBase()
00006 {
00007     ambientTemp = 25.0f;
00008     v25         = 760.0f;
00009     avgSlope    = 2.5f;
00010     vRef        = 3000.0f;
00011 }
00012 
00013 
00014 TempBase::TempBase(float correction)
00015 {
00016     ambientTemp = 25.0f + correction;
00017     v25         = 760.0f;
00018     avgSlope    = 2.5f;
00019     vRef        = 3000.0f;
00020 }
00021 
00022 // ADC init function, call it 1 time during setup
00023 void TempBase::Init()
00024 {
00025     hadc1.Instance = ADC1;
00026     hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
00027     hadc1.Init.Resolution = ADC_RESOLUTION12b;
00028     hadc1.Init.ScanConvMode = DISABLE;
00029     hadc1.Init.ContinuousConvMode = DISABLE;
00030     hadc1.Init.DiscontinuousConvMode = DISABLE;
00031     hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
00032     hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
00033     hadc1.Init.NbrOfConversion = 1;
00034     hadc1.Init.DMAContinuousRequests = DISABLE;
00035     hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
00036     HAL_ADC_Init(&hadc1);   
00037         
00038     sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; 
00039     sConfig.Rank = 1;
00040     sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;  
00041         
00042     HAL_ADC_ConfigChannel(&hadc1, &sConfig);
00043 }
00044     
00045 // returns raw adc value from sensor    
00046 int TempBase::GetRawTemp()
00047 {
00048     HAL_ADC_Start(&hadc1); // Start conversion
00049             
00050     // Wait end of conversion and get value
00051     if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) 
00052         return HAL_ADC_GetValue(&hadc1);
00053     else return -1;
00054 }
00055     
00056 // takes raw value and convert it to temp    
00057 float TempBase::ConvertTemp(int value)
00058 {
00059     return (((value * vRef / 0xFFF) - v25) / avgSlope) + ambientTemp;   // formula is from manual
00060 }