Add sources

This commit is contained in:
Athamantis 2024-05-03 17:16:29 +02:00
parent 275c40498f
commit c5521a08fa
6 changed files with 76 additions and 17 deletions

View File

@ -19,6 +19,10 @@ extern "C" {
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <stdbool.h> #include <stdbool.h>
// The code is inspired by "EEPROM handling" form avr-libc
// web link: https://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
// Struct for information on the controller. // Struct for information on the controller.
typedef struct { typedef struct {
uint16_t ms_fanspeed_sample_rate; uint16_t ms_fanspeed_sample_rate;

View File

@ -22,33 +22,32 @@ extern "C" {
#include <math.h> #include <math.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
/* /*
The code has inspiration from "Getting Started with Analog comparator(AC)" by Microchip for setting up analog comparrator. The code has inspiration from "Getting Started with Analog comparator(AC)" by Microchip for setting up analog comparrator.
* web link: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/TB3211-Getting-Started-with-AC-DS90003211.pdf * web link: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/TB3211-Getting-Started-with-AC-DS90003211.pdf
* and inspiration form practice 6 for TCA0 setup * and inspiration form practice 6 for TCA0 setup
*/ */
// INITALICE TIMER COUNTER // INITALICE TIMER COUNTER
void init_TCA0(); void init_TCA0();
// UPDATE TIMER PERIOD // UPDATE TIMER PERIOD
void TCA0_update_period_ms (uint16_t timer_period); void TCA0_update_period_ms (uint16_t timer_period);
// TAKES INN A TIME AND A THE COUNTED FAN DIPS // TAKES INN A TIME AND A THE COUNTED FAN DIPS
// RETURNS THE RPM OF THE FAN // RETURNS THE RPM OF THE FAN
uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms); uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms);
// INITIALISING FAN PORTS // INITIALISING FAN PORTS
void init_fan_gpio(); void init_fan_gpio();
// INIT AC0 TO COMPARE PD6 AND PD7 // INIT AC0 TO COMPARE PD6 AND PD7
void init_AC0(); void init_AC0();
// INIT AC1 TO COMPARE PD4 AND PD7 // INIT AC1 TO COMPARE PD4 AND PD7
void init_AC1(); void init_AC1();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -13,7 +13,7 @@ extern "C" {
#endif #endif
#include <stdbool.h> #include <stdbool.h>
#include <float.h> #include <float.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -24,14 +24,14 @@ extern "C" {
#define B 3950 #define B 3950
#define R_1 1000 #define R_1 1000
#define ledpin PIN3_bm #define ledpin PIN3_bm
// Takes inn messured value // Takes inn messured value
// Calculates the temperature in celcius // Calculates the temperature in celcius
// Returns the thermistor themperature // Returns the thermistor themperature
float calculate_thermistor_temp(float readValue); float calculate_thermistor_temp(float readValue);
// Returns true if temperatur is higher than max_temp // Returns true if temperatur is higher than max_temp
bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp); bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp);
void alert_voltage_threshold_exceeded(bool voltage_threshold_bool); void alert_voltage_threshold_exceeded(bool voltage_threshold_bool);

View File

@ -5,6 +5,9 @@ void init_led(){
PORTB.DIRSET = ledpin; PORTB.DIRSET = ledpin;
} }
// The code is inspired by "Arduino thermistor guide" by valtentina Vogelman, 1 november 2023
// https://www.build-electronic-circuits.com/arduino-thermistor/
float calculate_thermistor_temp(float thermistor_voltage){ float calculate_thermistor_temp(float thermistor_voltage){
float R_thermistor; float R_thermistor;
float V_1; float V_1;
@ -46,4 +49,4 @@ void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){
} else{ } else{
PORTB.OUTCLR = ledpin; PORTB.OUTCLR = ledpin;
} }
} }

View File

@ -0,0 +1,52 @@
#include "themistor-temp.h"
void init_led(){
PORTB.DIRSET = ledpin;
}
https://www.build-electronic-circuits.com/arduino-thermistor/
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;
}
}

View File

@ -17,6 +17,7 @@ void ADC0_init(void) {
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */
/* Select ADC channel */ /* Select ADC channel */
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
} }