mikrokontrollersystemer-pro.../prosjekt.X/fan_speeeed.c

49 lines
983 B
C
Raw Normal View History

2024-03-20 14:27:59 +00:00
#include "fan_speeeed.h"
2024-04-09 13:39:11 +00:00
#include "uart.h"
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 */
2024-04-09 14:46:54 +00:00
TCA0_update_period_ms();
2024-04-09 12:53:32 +00:00
}
2024-03-20 11:33:03 +00:00
2024-04-09 12:53:32 +00:00
void TCA0_update_period_ms() {
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
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(){
2024-04-09 14:46:54 +00:00
PORTD.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 ) {
cli();
RPM_calculation();
2024-04-09 12:53:32 +00:00
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
2024-04-09 14:46:54 +00:00
sei();
2024-04-09 12:53:32 +00:00
}
2024-04-09 14:46:54 +00:00
ISR(PORTD_PORT_vect){
cli();
falling_edge_counter ++;
2024-04-09 14:46:54 +00:00
PORTD.INTFLAGS = PIN2_bm;
sei();
}