mikrokontrollersystemer-pro.../prosjekt.X/fan-speed.c

128 lines
3.4 KiB
C
Raw Permalink Normal View History

#include "fan-speed.h"
2024-04-09 13:39:11 +00:00
#include "uart.h"
2024-04-30 14:17:52 +00:00
uint16_t timer_period_ms = 1000;
2024-04-27 16:45:04 +00:00
uint16_t fan_speed = 0;
volatile uint16_t fan1_edge_counter = 0;
volatile uint16_t fan2_edge_counter = 0;
2024-04-26 15:00:04 +00:00
2024-04-24 12:24:40 +00:00
void init_TCA0() {
2024-04-26 15:00:04 +00:00
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */
2024-04-09 12:53:32 +00:00
}
2024-03-20 11:33:03 +00:00
2024-04-30 14:17:52 +00:00
void TCA0_update_period(uint16_t period_ms) {
float period_s = period_ms / 1E3; // Convert the ms to s
float frequency = 1/ period_s; // convert the period to a frequency
float perbuf_val = frequency * F_CPU / 1024; // F_IQR * F_CPU / TCA_prescaler
TCA0.SINGLE.PERBUF = (uint16_t)perbuf_val;
timer_period_ms = period_ms;
2024-03-20 11:33:03 +00:00
}
2024-04-09 12:53:32 +00:00
2024-04-27 16:45:04 +00:00
// COUNTINGS / TIME = FREQUENCY
// FREQ / 2 = GIVES ACTUAL FREQ
// FREQ * SEC-IN-A-MINUTE(60) / FAN-BLADES
uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms) {
2024-04-30 14:17:52 +00:00
fan_speed = (edge_counter / (float)((float)time_ms/1000.0) );
2024-04-26 15:00:04 +00:00
fan_speed = fan_speed/2;
fan_speed = fan_speed * 60/5;
2024-04-27 16:45:04 +00:00
edge_counter = 0;
2024-03-20 11:33:03 +00:00
return fan_speed;
}
2024-04-21 10:43:51 +00:00
2024-04-27 16:45:04 +00:00
void init_fan_gpio() {
2024-04-25 10:29:10 +00:00
// CONFIGURE PINS AS ANALOG INPUTS
2024-04-27 15:31:00 +00:00
// PIN FOR FAN 1
2024-04-26 15:00:04 +00:00
PORTD.DIRSET &= ~PIN6_bm;
2024-04-26 13:13:45 +00:00
PORTD.PIN6CTRL &= ~ PORT_ISC_gm;
2024-04-25 12:06:51 +00:00
PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;
2024-04-25 10:29:10 +00:00
2024-04-27 15:31:00 +00:00
// PIN FOR FAN 2
2024-04-27 15:26:35 +00:00
PORTD.DIRSET &= ~PIN4_bm;
PORTD.PIN4CTRL &= ~ PORT_ISC_gm ;
PORTD.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc;
2024-04-27 15:31:00 +00:00
// PIN FOR REFRENCE
2024-04-27 15:26:35 +00:00
PORTD.DIRSET &= PIN7_bm;
2024-04-25 12:06:51 +00:00
PORTD.PIN7CTRL &= ~ PORT_ISC_gm ;
PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc;
2024-04-27 15:26:35 +00:00
}
2024-04-27 16:45:04 +00:00
void init_AC0(){
2024-04-25 12:06:51 +00:00
//Wincontroll disabled
AC0.CTRLB = 0x00;
2024-04-25 10:29:10 +00:00
2024-04-25 12:06:51 +00:00
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
2024-04-27 15:31:00 +00:00
// FAN USE PD6 COMPARE WITH PD3
2024-04-27 15:26:35 +00:00
AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_gc;
2024-04-25 10:29:10 +00:00
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
2024-04-26 15:00:04 +00:00
AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
2024-04-25 10:29:10 +00:00
2024-04-26 15:00:04 +00:00
// TURN ON INTERUPT
2024-04-25 10:29:10 +00:00
AC0.INTCTRL = 0x01;
}
2024-04-26 13:13:45 +00:00
2024-04-27 16:45:04 +00:00
void init_AC1(){
2024-04-27 15:26:35 +00:00
//Wincontroll disabled
AC1.CTRLB = 0x00;
//SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON
2024-04-27 15:31:00 +00:00
// FAN USE PD4, COMPARE WITH PD3
2024-04-27 15:26:35 +00:00
AC1.MUXCTRL = AC_MUXPOS_AINP2_gc | AC_MUXNEG_AINN2_gc;
// ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA
AC1.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm;
// TURN ON INTERUPT
AC1.INTCTRL = 0x01;
}
2024-04-25 10:29:10 +00:00
ISR(AC0_AC_vect){ // AC0 vec flag
2024-04-26 13:47:02 +00:00
cli();
2024-04-27 16:45:04 +00:00
fan1_edge_counter++;
2024-04-27 15:26:35 +00:00
AC0.STATUS |= 0x10; //CMP flag to 0
2024-04-26 13:47:02 +00:00
sei();
2024-04-25 10:29:10 +00:00
}
2024-04-24 12:24:40 +00:00
2024-04-27 15:26:35 +00:00
2024-04-27 15:31:00 +00:00
ISR(AC1_AC_vect){ // AC1 vec flag
2024-04-27 15:26:35 +00:00
cli();
2024-04-27 16:45:04 +00:00
fan2_edge_counter++;
2024-04-27 15:26:35 +00:00
AC1.STATUS |= 0x10; //CMP flag to 0
sei();
}
2024-04-27 15:31:00 +00:00
// TIMER INTERUPT
2024-04-26 13:47:02 +00:00
ISR (TCA0_OVF_vect) {
cli();
// Increment the index, or reset if it is at the top
if (fan1_history_index < 512) {
fan1_history_index++;
} else {
fan1_history_index = 0;
}
if (fan2_history_index < 512) {
fan2_history_index++;
} else {
fan2_history_index = 0;
}
// Calculate the fanspeed
2024-04-30 14:17:52 +00:00
fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter, timer_period_ms);
fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter, timer_period_ms);
// Reset the edge counter
2024-04-27 16:45:04 +00:00
fan1_edge_counter = 0;
fan2_edge_counter = 0;
2024-04-26 13:47:02 +00:00
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
sei();
}