2024-03-20 14:27:59 +00:00
|
|
|
#include "fan_speeeed.h"
|
2024-04-09 13:39:11 +00:00
|
|
|
#include "uart.h"
|
2024-03-20 14:25:59 +00:00
|
|
|
|
2024-04-09 14:17:34 +00:00
|
|
|
uint16_t timer_period_ms = 1;
|
|
|
|
|
uint16_t fan_speed;
|
|
|
|
|
volatile uint16_t falling_edge_counter = 0;
|
|
|
|
|
|
2024-04-09 12:53:32 +00:00
|
|
|
void TCA0_init() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-03-20 11:33:03 +00:00
|
|
|
|
2024-04-09 12:53:32 +00:00
|
|
|
void TCA0_update_period_ms() {
|
2024-04-09 14:17:34 +00:00
|
|
|
TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period_ms)/1024); /* F_CPU * F_IRQ / TCA_prescaler */
|
2024-03-20 11:33:03 +00:00
|
|
|
}
|
2024-04-09 12:53:32 +00:00
|
|
|
|
2024-04-09 14:17:34 +00:00
|
|
|
uint16_t RPM_calculation() {
|
|
|
|
|
fan_speed = (falling_edge_counter/timer_period_ms)*6000*3;
|
|
|
|
|
falling_edge_counter= 0;
|
|
|
|
|
printf("%u", fan_speed);
|
2024-03-20 11:33:03 +00:00
|
|
|
return fan_speed;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 12:53:32 +00:00
|
|
|
void PORT_init(){
|
|
|
|
|
PORTB.PIN2CTRL = PORT_ISC_FALLING_gc;
|
2024-03-20 13:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-20 11:33:03 +00:00
|
|
|
|
|
|
|
|
|
2024-04-09 12:53:32 +00:00
|
|
|
ISR ( TCA0_OVF_vect ) {
|
2024-04-09 14:17:34 +00:00
|
|
|
cli();
|
|
|
|
|
RPM_calculation();
|
2024-03-20 14:25:59 +00:00
|
|
|
|
2024-04-09 12:53:32 +00:00
|
|
|
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
|
2024-04-09 14:17:34 +00:00
|
|
|
sei();
|
2024-04-09 12:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ISR(PORTB_PORT_vect){
|
|
|
|
|
PORTB.INTFLAGS = PIN2_bm;
|
2024-04-09 14:17:34 +00:00
|
|
|
falling_edge_counter ++;
|
2024-03-20 14:25:59 +00:00
|
|
|
}
|