mikrokontrollersystemer-pro.../prosjekt.X/thermistor-temp.c

53 lines
1.4 KiB
C
Raw Normal View History

2024-03-13 12:07:29 +00:00
#include "themistor-temp.h"
2024-04-16 15:24:54 +00:00
void init_led(){
PORTB.DIRSET = ledpin;
}
2024-05-03 15:16:29 +00:00
// The code is inspired by "Arduino thermistor guide" by valtentina Vogelman, 1 november 2023
// https://www.build-electronic-circuits.com/arduino-thermistor/
2024-04-16 13:56:51 +00:00
float calculate_thermistor_temp(float thermistor_voltage){
2024-04-16 15:24:54 +00:00
float R_thermistor;
2024-03-13 12:07:29 +00:00
float V_1;
float ln;
2024-04-16 15:24:54 +00:00
float T_thermistor;
float V_thermistor;
#define V_TOT 5
2024-03-13 12:07:29 +00:00
// Calculate Voltage over thermistor
2024-04-16 15:24:54 +00:00
V_thermistor = (V_TOT/1024)*thermistor_voltage;
2024-03-13 12:07:29 +00:00
// Voltage accross R_1
2024-04-16 15:24:54 +00:00
V_1 = V_TOT - V_thermistor;
2024-03-13 12:07:29 +00:00
// Calculate Thermistor resistanse
2024-04-16 15:24:54 +00:00
R_thermistor = (V_thermistor)/ (V_1 / R_1);
2024-03-13 12:07:29 +00:00
// Steinhart-Harts formula
2024-04-16 15:24:54 +00:00
ln = log(R_thermistor/R_T0);
T_thermistor = (1/ ((ln/B) + (1/T_0)));
2024-03-13 12:07:29 +00:00
// Temperatur in celcius
2024-04-16 15:24:54 +00:00
T_thermistor -= 273.15;
2024-03-13 12:07:29 +00:00
2024-04-16 15:24:54 +00:00
return T_thermistor;
2024-03-13 12:07:29 +00:00
}
// returns error message if the messured thermistor temp is higher than
// Choosen max_temp
2024-04-16 13:56:51 +00:00
bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){
2024-03-13 12:16:16 +00:00
// Return true if temp is higher then max value
2024-04-16 15:24:54 +00:00
return (thermistor_temp >= max_temp);
2024-04-16 13:56:51 +00:00
}
//print if the maximum threshold is exceeded.
2024-04-16 15:24:54 +00:00
void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){
2024-04-16 13:56:51 +00:00
if (voltage_threshold_bool){
printf("Error: maximum temperature exceeded");
2024-04-16 15:24:54 +00:00
PORTB.OUTSET = ledpin;
} else{
PORTB.OUTCLR = ledpin;
}
2024-05-03 15:16:29 +00:00
}