thermometer stuff started
This commit is contained in:
54
my_src/thermometer.c
Normal file
54
my_src/thermometer.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
14
my_src/thermometer.h
Normal file
14
my_src/thermometer.h
Normal file
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* thermometer.h
|
||||
*
|
||||
* Created on: Jun 13, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef THERMOMETER_H_
|
||||
#define THERMOMETER_H_
|
||||
|
||||
void thermometerInit();
|
||||
uint32_t thermometerGetValue();
|
||||
|
||||
#endif /* THERMOMETER_H_ */
|
Reference in New Issue
Block a user