made code for fan speed

This commit is contained in:
Inamr 2024-04-09 14:53:32 +02:00
parent 9b23172eba
commit f1c9a0707e
2 changed files with 33 additions and 77 deletions

View File

@ -1,84 +1,35 @@
#include "fan_speeeed.h" #include "fan_speeeed.h"
int voltage_value_counter = 0; void TCA0_init() {
int voltage_value [30]; TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ;
TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */
TCA0.SINGLE.PERBUF = 0x0F42;
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm;
int RTC_init() {
uint8_t temp;
/* Initialize 32.768kHz Oscillator: */
/* Disable oscillator: */
temp = CLKCTRL.XOSC32KCTRLA;
temp &= ~CLKCTRL_ENABLE_bm;
/* Writing to protected register */
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
while (CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm) {
; /* Wait until XOSC32KS becomes 0 */
}
/* SEL = 0 (Use External Crystal): */
temp = CLKCTRL.XOSC32KCTRLA;
temp &= ~CLKCTRL_SEL_bm;
/* Writing to protected register */
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
/* Enable oscillator: */
temp = CLKCTRL.XOSC32KCTRLA;
temp |= CLKCTRL_ENABLE_bm;
/* Writing to protected register */
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
/* Initialize RTC: */
while (RTC.STATUS > 0) {
; /* Wait for all register to be synchronized */
}
/* 32.768kHz External Crystal Oscillator (XOSC32K) */
RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc;
/* Run in debug: enabled */
RTC.DBGCTRL = RTC_DBGRUN_bm;
RTC.PITINTCTRL = RTC_PI_bm; /* Periodic Interrupt: enabled */
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc /* RTC Clock Cycles 32768 */
| RTC_PITEN_bm; /* Enable: enabled */
} }
// skal lage en som finner data hvert ... sekund. Datane skal samles inn i en array. Når arrayen er full skal jeg regne ut rpm. Vet t det er ... mange sek mellom så blir enklere. så noe signalbehandling shit.
int read_array_get_RPM() { void TCA0_update_period_ms() {
TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period)/1024); /* F_CPU * F_IRQ / TCA_prescaler */
f = ((1 / t)*1000); }
fan_speed = (f * 60) / 2;
int read_input_get_RPM() {
fan_speed=(voltage_value_counter/timer_period)*6000*3;
voltage_value_counter = 0;
return fan_speed; return fan_speed;
} }
inline void put_in_array() { void PORT_init(){
voltage_values[voltage_value_counter] = read_photodiode_voltage(); PORTB.PIN2CTRL = PORT_ISC_FALLING_gc;
voltage_value_counter++;
}
void reset_voltage_array(){
for(int i = 0; i < voltage_values_counter; i++){
voltage_values[i]= 0;
}
voltage_value_counter = 0;
} }
ISR(RTC_PIT_vect) {
RTC.PITINTFLAGS = RTC_PI_bm; ISR ( TCA0_OVF_vect ) {
if(voltage_value_counter < sizeof(voltage_values)/sizeof(voltage_value[0])){ read_input_get_RPM();
put_in_array();
}
else{
read_array_get_RPM();
reset_voltage_array();
}
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
}
ISR(PORTB_PORT_vect){
PORTB.INTFLAGS = PIN2_bm;
voltage_value_counter ++;
} }

View File

@ -23,12 +23,17 @@ extern "C" {
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/cpufunc.h> #include <avr/cpufunc.h>
#include "voltage.h"
int RTC_init();
int read_array_get_RPM(int voltage_value);
inline void put_in_array();
volatile uint16_t voltage_value_counter = 0;
int timer_period = 1;
int fan_speed;
void TCA0_init ();
void PORT_init();
void TCA0_update_period_ms ();
int read_input_get_RPM();
int interrpt();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif