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

105 lines
2.9 KiB
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() {
2024-04-10 12:53:17 +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
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-10 12:53:17 +00:00
//compare or capture
2024-04-17 12:59:46 +00:00
//TCB3.CCMP = 0x00;
2024-04-10 12:53:17 +00:00
//count
TCB3.CNT = 0x00;
2024-04-17 12:59:46 +00:00
PORTA.IN = PIN0_bm;
PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
2024-04-10 12:53:17 +00:00
//PORTD.PIN2CTRL = PORT_ISC_FALLING_gc | 0b01000000;
// TCB3.CTRLB =0<< TCB_ASYNC_bp /* Asynchronous Enable: disabled */|0<< TCB_CCMPEN_bp /* Pin Output Enable: disabled */|0<< TCB_CCMPINIT_bp /* Pin Initial State: disabled */| TCB_CNTMODE_FRQ_gc;/* Input Capture Frequency measurement */
// TCB3.EVCTRL =1<< TCB_CAPTEI_bp /* Event Input Enable: enabled */|0<< TCB_EDGE_bp /* Event Edge: disabled */|0<< TCB_FILTER_bp;/* Input Capture Noise Cancellation Filter: disabled */
// TCB3.INTCTRL =1<< TCB_CAPT_bp /* Capture or Timeout: enabled */|0<< TCB_OVF_bp;/* OverFlow Interrupt: disabled */
// TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER */|1<< TCB_ENABLE_bp /* Enable: enabled */|0<< TCB_RUNSTDBY_bp /* Run Standby: disabled */|0<< TCB_SYNCUPD_bp /* Synchronize Update: disabled */|0<< TCB_CASCADE_bp;/* Cascade Two Timer/Counters: disabled */
//ASYNC disabled; CCMPINIT disabled; CCMPEN disabled; CNTMODE FRQPW;
2024-04-17 12:59:46 +00:00
TCB3.CTRLB = 0x04;
2024-04-10 12:53:17 +00:00
//DBGRUN disabled;
2024-04-17 12:59:46 +00:00
//TCB3.DBGCTRL = 0x00;
2024-03-20 13:36:58 +00:00
2024-04-10 12:53:17 +00:00
//FILTER disabled; EDGE disabled; CAPTEI enabled;
2024-04-10 13:52:07 +00:00
TCB3.EVCTRL = 0x01;
2024-04-10 12:53:17 +00:00
//OVF disabled; CAPT enabled;
2024-04-17 12:59:46 +00:00
TCB3.INTCTRL = 0x01;
2024-04-10 12:53:17 +00:00
2024-04-17 12:59:46 +00:00
//OVF disabledAPT disabled;
//TCB3.INTFLAGS = 0; CAPT disabled;
//TCB3.INTFLAGS = 0x00;
2024-04-10 12:53:17 +00:00
//Temporary Value
2024-04-17 12:59:46 +00:00
//TCB3.TEMP = 0x00;
2024-04-10 12:53:17 +00:00
2024-04-10 13:52:07 +00:00
2024-04-17 12:59:46 +00:00
EVSYS.CHANNEL0 = 0x40;
//EVSYS.SWEVENTA = 0x01;
2024-04-10 13:52:07 +00:00
EVSYS.USERTCB3CAPT = 0x01;
2024-04-17 12:59:46 +00:00
//RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled;
TCB3.CTRLA = 0x01;
2024-04-10 12:53:17 +00:00
}
2024-03-20 11:33:03 +00:00
2024-04-10 12:53:17 +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-10 12:53:17 +00:00
}*/
2024-04-17 12:59:46 +00:00
void printnie(uint8_t en, uint8_t to){
printf("CNT %u", en);
printf("ccmp %u", to);
}
2024-04-10 12:53:17 +00:00
ISR (TCB3_INT_vect){
uint16_t yo = TCB3.CNT;
2024-04-17 12:59:46 +00:00
2024-04-10 12:53:17 +00:00
uint16_t stewui = TCB3.CCMP;
2024-04-17 12:59:46 +00:00
printnie(yo, stewui);
2024-04-10 13:52:07 +00:00
TCB3.INTFLAGS = 0x03;
2024-04-10 12:53:17 +00:00
//TCB3.INTFLAGS = PIN0_bm;
2024-04-09 12:53:32 +00:00
}
2024-04-10 12:53:17 +00:00
/*
2024-04-09 14:46:54 +00:00
ISR(PORTD_PORT_vect){
2024-04-10 12:53:17 +00:00
// cli();
falling_edge_counter ++;
2024-04-09 14:46:54 +00:00
PORTD.INTFLAGS = PIN2_bm;
2024-04-10 12:53:17 +00:00
//sei();
}
2024-04-10 12:53:17 +00:00
* */