Merge branch 'thermistor-temp' into main

This commit is contained in:
Elp03 2024-04-16 18:00:41 +02:00
commit e6a3815dc2
4 changed files with 104 additions and 7 deletions

View File

@ -20,6 +20,8 @@
#include "uart.h" #include "uart.h"
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdint.h>
#include "themistor-temp.h"
int main() { int main() {
init_uart((uint16_t)9600); init_uart((uint16_t)9600);

View File

@ -6,10 +6,16 @@
projectFiles="true"> projectFiles="true">
<itemPath>command-handler.h</itemPath> <itemPath>command-handler.h</itemPath>
<itemPath>i2c.h</itemPath> <itemPath>i2c.h</itemPath>
<itemPath>themistor-temp.h</itemPath>
<itemPath>uart.h</itemPath> <itemPath>uart.h</itemPath>
<itemPath>eeprom.h</itemPath> <itemPath>eeprom.h</itemPath>
<itemPath>voltage.h</itemPath> <itemPath>voltage.h</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="ExternalFiles"
displayName="Important Files"
projectFiles="true">
<itemPath>Makefile</itemPath>
</logicalFolder>
<logicalFolder name="LinkerScript" <logicalFolder name="LinkerScript"
displayName="Linker Files" displayName="Linker Files"
projectFiles="true"> projectFiles="true">
@ -18,17 +24,10 @@
displayName="Source Files" displayName="Source Files"
projectFiles="true"> projectFiles="true">
<itemPath>main.c</itemPath> <itemPath>main.c</itemPath>
<itemPath>command-handler.c</itemPath>
<itemPath>i2c.c</itemPath>
<itemPath>uart.c</itemPath> <itemPath>uart.c</itemPath>
<itemPath>eeprom.c</itemPath> <itemPath>eeprom.c</itemPath>
<itemPath>voltage.c</itemPath> <itemPath>voltage.c</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="ExternalFiles"
displayName="Important Files"
projectFiles="false">
<itemPath>Makefile</itemPath>
</logicalFolder>
</logicalFolder> </logicalFolder>
<projectmakefile>Makefile</projectmakefile> <projectmakefile>Makefile</projectmakefile>
<confs> <confs>

View File

@ -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 <stdbool.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <avr/io.h>
#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 */

View File

@ -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;
}
}