2024-03-13 12:39:35 +00:00
2024-03-20 14:25:59 +00:00
2024-03-20 14:27:59 +00:00
# include "fan_speeeed.h"
2024-03-20 14:25:59 +00:00
int voltage_value_counter = 0 ;
int voltage_value [ 30 ] ;
2024-03-20 11:33:03 +00:00
RTC . PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm ;
2024-03-19 11:59:56 +00:00
2024-03-20 11:33:03 +00:00
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 */
2024-03-19 13:29:57 +00:00
}
2024-03-20 11:33:03 +00:00
/* 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 */
}
2024-03-20 13:36:58 +00:00
// 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.
2024-03-20 14:25:59 +00:00
int read_array_get_RPM ( ) {
2024-03-20 11:33:03 +00:00
f = ( ( 1 / t ) * 1000 ) ;
fan_speed = ( f * 60 ) / 2 ;
2024-03-20 14:25:59 +00:00
2024-03-20 11:33:03 +00:00
return fan_speed ;
}
2024-03-20 13:36:58 +00:00
inline void put_in_array ( ) {
2024-03-20 14:25:59 +00:00
voltage_values [ voltage_value_counter ] = read_photodiode_voltage ( ) ;
voltage_value_counter + + ;
2024-03-20 13:36:58 +00:00
}
2024-03-20 14:25:59 +00:00
void reset_voltage_array ( ) {
for ( int i = 0 ; i < voltage_values_counter ; i + + ) {
voltage_values [ i ] = 0 ;
}
voltage_value_counter = 0 ;
2024-03-20 11:33:03 +00:00
}
2024-03-20 14:25:59 +00:00
ISR ( RTC_PIT_vect ) {
RTC . PITINTFLAGS = RTC_PI_bm ;
if ( voltage_value_counter < sizeof ( voltage_values ) / sizeof ( voltage_value [ 0 ] ) ) {
put_in_array ( ) ;
}
else {
read_array_get_RPM ( ) ;
reset_voltage_array ( ) ;
2024-03-20 11:33:03 +00:00
}
2024-03-20 14:25:59 +00:00
}