From 555d3efde4134b768c3d1fc99c5431f8ffe59bce Mon Sep 17 00:00:00 2001 From: Elp03 Date: Fri, 8 Mar 2024 11:48:49 +0100 Subject: [PATCH 1/8] add calculating code --- prosjekt.X/main.c | 36 +++++++++++++++++++++++++ prosjekt.X/nbproject/configurations.xml | 1 + prosjekt.X/themistor-temp.h | 23 ++++++++++++++++ prosjekt.X/thermistor-temp.c | 1 + 4 files changed, 61 insertions(+) create mode 100644 prosjekt.X/themistor-temp.h create mode 100644 prosjekt.X/thermistor-temp.c diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index b42afa9..5483a2b 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -12,6 +12,42 @@ #include "uart.h" #include +#define R_t0 10000 +#define T_0 298.15 +#define B 3950 +#define R_1 1000 + +float R_therm; +float V_1; +float ln; +float T_therm; +float V_therm; + +// Takes inn messured value +// Calculates the temperature in celcius +// Returns the themperature +float calculate_thermistor_temp(float readValue){ + uint8_t V_tot = 5; + // Calculate Voltage over thermistor + V_therm = (V_tot/1024)*readValue; + + // Voltage accross R_1 + V_1 = V_tot - V_therm; + + // Calculate Thermistor resistanse + R_therm = (V_therm)/ (V_1 / R_1); + + // Steinhart-Harts formula + ln = log(R_therm/R_t0); + T_therm = (1/ ((ln/B) + (1/T_0))); + + // Temperatur in celcius + T_therm -= 273.15; + + return T_therm; +} + + int main() { init_uart((uint16_t)9600); stdout = &USART_stream; diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index e8e1b9c..322cc4c 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -15,6 +15,7 @@ projectFiles="true"> main.c uart.c + thermistor-temp.c Date: Wed, 13 Mar 2024 10:25:36 +0100 Subject: [PATCH 2/8] Fix source files --- prosjekt.X/main.c | 38 ++----------------------- prosjekt.X/nbproject/configurations.xml | 1 + prosjekt.X/themistor-temp.h | 15 ++++++++++ prosjekt.X/thermistor-temp.c | 26 +++++++++++++++++ 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 5483a2b..6058f74 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -11,42 +11,8 @@ #include #include "uart.h" #include - -#define R_t0 10000 -#define T_0 298.15 -#define B 3950 -#define R_1 1000 - -float R_therm; -float V_1; -float ln; -float T_therm; -float V_therm; - -// Takes inn messured value -// Calculates the temperature in celcius -// Returns the themperature -float calculate_thermistor_temp(float readValue){ - uint8_t V_tot = 5; - // Calculate Voltage over thermistor - V_therm = (V_tot/1024)*readValue; - - // Voltage accross R_1 - V_1 = V_tot - V_therm; - - // Calculate Thermistor resistanse - R_therm = (V_therm)/ (V_1 / R_1); - - // Steinhart-Harts formula - ln = log(R_therm/R_t0); - T_therm = (1/ ((ln/B) + (1/T_0))); - - // Temperatur in celcius - T_therm -= 273.15; - - return T_therm; -} - +#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 322cc4c..208dedf 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -5,6 +5,7 @@ displayName="Header Files" projectFiles="true"> uart.h + themistor-temp.h +#include +#include +#include +#define R_t0 10000 +#define T_0 298.15 +#define B 3950 +#define R_1 1000 + +// Takes inn messured value +// Calculates the temperature in celcius +// Returns the themperature +float calculate_thermistor_temp(float readValue); + diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 8b13789..4aa1e41 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1 +1,27 @@ +#include "themistor-temp.h" +float calculate_thermistor_temp(float readValue){ + float R_therm; + float V_1; + float ln; + float T_therm; + float V_therm; + uint8_t V_tot = 5; + // Calculate Voltage over thermistor + V_therm = (V_tot/1024)*readValue; + + // Voltage accross R_1 + V_1 = V_tot - V_therm; + + // Calculate Thermistor resistanse + R_therm = (V_therm)/ (V_1 / R_1); + + // Steinhart-Harts formula + ln = log(R_therm/R_t0); + T_therm = (1/ ((ln/B) + (1/T_0))); + + // Temperatur in celcius + T_therm -= 273.15; + + return T_therm; +} From 8e58925dd1911c01fd0221e520de41407ec13bd8 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 13 Mar 2024 11:21:14 +0100 Subject: [PATCH 3/8] Add comments --- prosjekt.X/nbproject/configurations.xml | 10 ++++++++-- prosjekt.X/themistor-temp.h | 2 +- prosjekt.X/thermistor-temp.c | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 208dedf..047a263 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -4,7 +4,7 @@ - uart.h + eeprom.h themistor-temp.h main.c - uart.c + eeprom.c thermistor-temp.c + + + @@ -169,6 +172,9 @@ + + + diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index dc44ea8..2b715d1 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -23,7 +23,7 @@ extern "C" { // Takes inn messured value // Calculates the temperature in celcius -// Returns the themperature +// Returns the thermistor themperature float calculate_thermistor_temp(float readValue); diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 4aa1e41..7d6af52 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,5 +1,6 @@ #include "themistor-temp.h" + float calculate_thermistor_temp(float readValue){ float R_therm; float V_1; From 24190f1f15db665126956d36e570cf3b078a9322 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 13:07:29 +0100 Subject: [PATCH 4/8] Made a bool for error and high temp --- prosjekt.X/nbproject/configurations.xml | 360 ++++++++++++------------ prosjekt.X/themistor-temp.h | 78 ++--- prosjekt.X/thermistor-temp.c | 65 +++-- 3 files changed, 257 insertions(+), 246 deletions(-) diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 047a263..3cb0be9 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,180 +1,180 @@ - - - - - eeprom.h - themistor-temp.h - - - - - main.c - eeprom.c - thermistor-temp.c - - - Makefile - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.45 - 2 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + eeprom.h + themistor-temp.h + + + Makefile + + + + + main.c + eeprom.c + thermistor-temp.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.46 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index 2b715d1..65c622c 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -1,38 +1,40 @@ -/* - * 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 -#define R_t0 10000 -#define T_0 298.15 -#define B 3950 -#define R_1 1000 - -// Takes inn messured value -// Calculates the temperature in celcius -// Returns the thermistor themperature -float calculate_thermistor_temp(float readValue); - - - - - -#ifdef __cplusplus -} -#endif - -#endif /* THEMISTOR_TEMP_H */ - +/* + * 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 +#define R_t0 10000 +#define T_0 298.15 +#define B 3950 +#define R_1 1000 + +// Takes inn messured value +// Calculates the temperature in celcius +// Returns the thermistor themperature +float calculate_thermistor_temp(float readValue); + +bool error_message(float thermistor_temp); + + + +#ifdef __cplusplus +} +#endif + +#endif /* THEMISTOR_TEMP_H */ + diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 7d6af52..47442f5 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,28 +1,37 @@ -#include "themistor-temp.h" - - -float calculate_thermistor_temp(float readValue){ - float R_therm; - float V_1; - float ln; - float T_therm; - float V_therm; - uint8_t V_tot = 5; - // Calculate Voltage over thermistor - V_therm = (V_tot/1024)*readValue; - - // Voltage accross R_1 - V_1 = V_tot - V_therm; - - // Calculate Thermistor resistanse - R_therm = (V_therm)/ (V_1 / R_1); - - // Steinhart-Harts formula - ln = log(R_therm/R_t0); - T_therm = (1/ ((ln/B) + (1/T_0))); - - // Temperatur in celcius - T_therm -= 273.15; - - return T_therm; -} +#include "themistor-temp.h" + +float calculate_thermistor_temp(float readValue){ + float R_therm; + float V_1; + float ln; + float T_therm; + float V_therm; + uint8_t V_tot = 5; + // Calculate Voltage over thermistor + V_therm = (V_tot/1024)*readValue; + + // Voltage accross R_1 + V_1 = V_tot - V_therm; + + // Calculate Thermistor resistanse + R_therm = (V_therm)/ (V_1 / R_1); + + // Steinhart-Harts formula + ln = log(R_therm/R_t0); + T_therm = (1/ ((ln/B) + (1/T_0))); + + // Temperatur in celcius + T_therm -= 273.15; + + return T_therm; +} + +bool error_message(float thermistor_temp){ + int high_temp = 0; + if (thermistor_temp > high_temp){ + return true; + } + else { + return false; + } +} \ No newline at end of file From 4a66e1dcdc9a34cba35a5ca03e1dca7c692148ee Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 13:16:16 +0100 Subject: [PATCH 5/8] add comments --- prosjekt.X/themistor-temp.h | 2 +- prosjekt.X/thermistor-temp.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index 65c622c..e440f0c 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -27,7 +27,7 @@ extern "C" { // Calculates the temperature in celcius // Returns the thermistor themperature float calculate_thermistor_temp(float readValue); - +// Returns true if temperatur is higher then high_temp int bool error_message(float thermistor_temp); diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 47442f5..afba6d5 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -26,9 +26,13 @@ float calculate_thermistor_temp(float readValue){ return T_therm; } +// returns error message bool error_message(float thermistor_temp){ + // Max value int high_temp = 0; + // Return true if temp is higher then max value if (thermistor_temp > high_temp){ + printf("Error"); return true; } else { From 849c719b5981015b8a6dc3d46ab4c91422db404d Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 20 Mar 2024 11:06:46 +0100 Subject: [PATCH 6/8] Change error temp Make it able to innputt a chosen max temp --- prosjekt.X/nbproject/configurations.xml | 360 ++++++++++++------------ prosjekt.X/themistor-temp.h | 6 +- prosjekt.X/thermistor-temp.c | 9 +- 3 files changed, 188 insertions(+), 187 deletions(-) diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 3cb0be9..b0dae9e 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,180 +1,180 @@ - - - - - eeprom.h - themistor-temp.h - - - Makefile - - - - - main.c - eeprom.c - thermistor-temp.c - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 2 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + themistor-temp.h + uart.h + + + Makefile + + + + + main.c + thermistor-temp.c + uart.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.45 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index e440f0c..8668c9c 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -27,8 +27,10 @@ extern "C" { // Calculates the temperature in celcius // Returns the thermistor themperature float calculate_thermistor_temp(float readValue); -// Returns true if temperatur is higher then high_temp int -bool error_message(float thermistor_temp); + + +// Returns true if temperatur is higher than max_temp +bool error_message(float thermistor_temp, uint8_t max_temp); diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index afba6d5..4d41ea2 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -26,12 +26,11 @@ float calculate_thermistor_temp(float readValue){ return T_therm; } -// returns error message -bool error_message(float thermistor_temp){ - // Max value - int high_temp = 0; +// returns error message if the messured thermistor temp is higher than +// Choosen max_temp +bool error_message(float thermistor_temp, uint8_t max_temp){ // Return true if temp is higher then max value - if (thermistor_temp > high_temp){ + if (thermistor_temp >= max_temp){ printf("Error"); return true; } From 9f27bc845479104bc45d7cdfc2623cfc8bca9257 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 16 Apr 2024 15:56:51 +0200 Subject: [PATCH 7/8] Fix names --- prosjekt.X/themistor-temp.h | 3 ++- prosjekt.X/thermistor-temp.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index 8668c9c..d544435 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -30,8 +30,9 @@ float calculate_thermistor_temp(float readValue); // Returns true if temperatur is higher than max_temp -bool error_message(float thermistor_temp, uint8_t max_temp); +bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp); +void voltage_threshold_exceeded(bool voltage_threshold_bool); #ifdef __cplusplus diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 4d41ea2..2d333f8 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,6 +1,6 @@ #include "themistor-temp.h" -float calculate_thermistor_temp(float readValue){ +float calculate_thermistor_temp(float thermistor_voltage){ float R_therm; float V_1; float ln; @@ -8,7 +8,7 @@ float calculate_thermistor_temp(float readValue){ float V_therm; uint8_t V_tot = 5; // Calculate Voltage over thermistor - V_therm = (V_tot/1024)*readValue; + V_therm = (V_tot/1024)*thermistor_voltage; // Voltage accross R_1 V_1 = V_tot - V_therm; @@ -28,13 +28,19 @@ float calculate_thermistor_temp(float readValue){ // returns error message if the messured thermistor temp is higher than // Choosen max_temp -bool error_message(float thermistor_temp, uint8_t max_temp){ +bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){ // Return true if temp is higher then max value if (thermistor_temp >= max_temp){ - printf("Error"); return true; } else { return false; } +} + +//print if the maximum threshold is exceeded. +void voltage_threshold_exceeded(bool voltage_threshold_bool){ + if (voltage_threshold_bool){ + printf("Error: maximum temperature exceeded"); + } } \ No newline at end of file From 933949dcffc1e9a22bc49abcf3b0af52f5bb4b7c Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 16 Apr 2024 17:24:54 +0200 Subject: [PATCH 8/8] Fix variable name and led --- prosjekt.X/themistor-temp.h | 8 +++++-- prosjekt.X/thermistor-temp.c | 41 +++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index d544435..4d85568 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -18,10 +18,12 @@ extern "C" { #include #include #include -#define R_t0 10000 +#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 @@ -32,8 +34,10 @@ 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 voltage_threshold_exceeded(bool voltage_threshold_bool); +void alert_voltage_threshold_exceeded(bool voltage_threshold_bool); +// Initialise led +void init_led(); #ifdef __cplusplus } diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 2d333f8..71933a5 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,46 +1,49 @@ #include "themistor-temp.h" + +void init_led(){ + PORTB.DIRSET = ledpin; +} + float calculate_thermistor_temp(float thermistor_voltage){ - float R_therm; + float R_thermistor; float V_1; float ln; - float T_therm; - float V_therm; - uint8_t V_tot = 5; + float T_thermistor; + float V_thermistor; + #define V_TOT 5 // Calculate Voltage over thermistor - V_therm = (V_tot/1024)*thermistor_voltage; + V_thermistor = (V_TOT/1024)*thermistor_voltage; // Voltage accross R_1 - V_1 = V_tot - V_therm; + V_1 = V_TOT - V_thermistor; // Calculate Thermistor resistanse - R_therm = (V_therm)/ (V_1 / R_1); + R_thermistor = (V_thermistor)/ (V_1 / R_1); // Steinhart-Harts formula - ln = log(R_therm/R_t0); - T_therm = (1/ ((ln/B) + (1/T_0))); + ln = log(R_thermistor/R_T0); + T_thermistor = (1/ ((ln/B) + (1/T_0))); // Temperatur in celcius - T_therm -= 273.15; + T_thermistor -= 273.15; - return T_therm; + 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 - if (thermistor_temp >= max_temp){ - return true; - } - else { - return false; - } + return (thermistor_temp >= max_temp); } //print if the maximum threshold is exceeded. -void voltage_threshold_exceeded(bool voltage_threshold_bool){ +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