diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 80e332b..428b4ff 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -20,6 +20,8 @@ #include "uart.h" #include #include +#include +#include "themistor-temp.h" int main() { init_uart((uint16_t)9600); diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index d9ab8f2..9e4c7b2 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -6,10 +6,16 @@ projectFiles="true"> command-handler.h i2c.h + themistor-temp.h uart.h eeprom.h voltage.h + + Makefile + @@ -18,17 +24,10 @@ displayName="Source Files" projectFiles="true"> main.c - command-handler.c - i2c.c uart.c eeprom.c voltage.c - - Makefile - Makefile diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h new file mode 100644 index 0000000..4d85568 --- /dev/null +++ b/prosjekt.X/themistor-temp.h @@ -0,0 +1,47 @@ +/* + * File: themistor-temp.h + * Author: athamantis + * + * Created on 08 March 2024, 11:46 + */ + +#ifndef THEMISTOR_TEMP_H +#define THEMISTOR_TEMP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include +#include +#include +#include +#define R_T0 10000 +#define T_0 298.15 +#define B 3950 +#define R_1 1000 +#define ledpin PIN3_bm + +// Takes inn messured value +// Calculates the temperature in celcius +// Returns the thermistor themperature +float calculate_thermistor_temp(float readValue); + + +// Returns true if temperatur is higher than max_temp +bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp); + +void alert_voltage_threshold_exceeded(bool voltage_threshold_bool); + +// Initialise led +void init_led(); + +#ifdef __cplusplus +} +#endif + +#endif /* THEMISTOR_TEMP_H */ + diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c new file mode 100644 index 0000000..71933a5 --- /dev/null +++ b/prosjekt.X/thermistor-temp.c @@ -0,0 +1,49 @@ +#include "themistor-temp.h" + + +void init_led(){ + PORTB.DIRSET = ledpin; +} + +float calculate_thermistor_temp(float thermistor_voltage){ + float R_thermistor; + float V_1; + float ln; + float T_thermistor; + float V_thermistor; + #define V_TOT 5 + // Calculate Voltage over thermistor + V_thermistor = (V_TOT/1024)*thermistor_voltage; + + // Voltage accross R_1 + V_1 = V_TOT - V_thermistor; + + // Calculate Thermistor resistanse + R_thermistor = (V_thermistor)/ (V_1 / R_1); + + // Steinhart-Harts formula + ln = log(R_thermistor/R_T0); + T_thermistor = (1/ ((ln/B) + (1/T_0))); + + // Temperatur in celcius + T_thermistor -= 273.15; + + return T_thermistor; +} + +// returns error message if the messured thermistor temp is higher than +// Choosen max_temp +bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){ + // Return true if temp is higher then max value + return (thermistor_temp >= max_temp); +} + +//print if the maximum threshold is exceeded. +void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){ + if (voltage_threshold_bool){ + printf("Error: maximum temperature exceeded"); + PORTB.OUTSET = ledpin; + } else{ + PORTB.OUTCLR = ledpin; + } +} \ No newline at end of file