ttt2/my_src/thermometer.c

55 lines
1.0 KiB
C
Raw Permalink Normal View History

2017-06-13 13:26:06 +02:00
/*
* thermometer.c
*
* Created on: Jun 13, 2017
* Author: wn
*/
#include "stm32f1xx_hal.h"
#include "thermometer.h"
#include <PontCoopScheduler.h>
extern ADC_HandleTypeDef hadc1;
#define NUM_OF_CONV 10
uint16_t adcBuf[NUM_OF_CONV];
volatile uint32_t value;
const float R_REF = 1000.0;
const uint16_t N_MAX = 4095;
const float PT1000_R0 = 1000.0;
const float PT1000_Coeff = 3.85e-3;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
HAL_ADC_Stop_DMA(hadc);
uint32_t sum = 0;
for (uint8_t i = 0; i < NUM_OF_CONV; i++) {
sum += adcBuf[i];
}
uint32_t avg = sum / NUM_OF_CONV;
value = avg;
}
void thermometerStartCycle(void *handle) {
HAL_ADC_Start_DMA(&hadc1, ((uint32_t*)adcBuf), NUM_OF_CONV);
}
void thermometerInit() {
HAL_ADCEx_Calibration_Start(&hadc1);
schAdd(thermometerStartCycle, NULL, 0, 100);
}
uint32_t thermometerGetValue() {
float r = R_REF / ((((float)N_MAX) / ((float)value)) - 1.0);
float t = ((r / PT1000_R0) - 1) / PT1000_Coeff;
uint32_t tn = (t < 0) ? 0 : (uint16_t)t;
return tn;
}