92 lines
2.6 KiB
C
92 lines
2.6 KiB
C
#include "fan_speeeed.h"
|
|
#include "uart.h"
|
|
|
|
uint16_t timer_period_ms = 1;
|
|
uint16_t fan_speed;
|
|
volatile uint16_t falling_edge_counter = 0;
|
|
|
|
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_update_period_ms();
|
|
}
|
|
|
|
void TCA0_update_period_ms() {
|
|
TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period_ms)/1024); /* F_CPU * F_IRQ / TCA_prescaler */
|
|
}
|
|
|
|
uint16_t RPM_calculation() {
|
|
fan_speed = (falling_edge_counter/timer_period_ms)*6000*3;
|
|
falling_edge_counter= 0;
|
|
printf("%u", fan_speed);
|
|
return fan_speed;
|
|
}
|
|
|
|
void PORT_init(){
|
|
//compare or capture
|
|
TCB3.CCMP = 0x00;
|
|
//count
|
|
TCB3.CNT = 0x00;
|
|
|
|
|
|
//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;
|
|
TCB2.CTRLB = 0x15;
|
|
|
|
//DBGRUN disabled;
|
|
TCB2.DBGCTRL = 0x00;
|
|
|
|
//FILTER disabled; EDGE disabled; CAPTEI enabled;
|
|
TCB2.EVCTRL = 0x01;
|
|
|
|
//OVF disabled; CAPT enabled;
|
|
TCB2.INTCTRL = 0x01;
|
|
|
|
//OVF disabled; CAPT disabled;
|
|
TCB2.INTFLAGS = 0x00;
|
|
|
|
//Temporary Value
|
|
TCB2.TEMP = 0x00;
|
|
|
|
//RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled;
|
|
TCB2.CTRLA = 0x01;
|
|
}
|
|
|
|
|
|
/*
|
|
ISR ( TCA0_OVF_vect ) {
|
|
cli();
|
|
RPM_calculation();
|
|
|
|
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;
|
|
|
|
sei();
|
|
}*/
|
|
|
|
ISR (TCB3_INT_vect){
|
|
uint16_t yo = TCB3.CNT;
|
|
printf("CNT %u", yo);
|
|
|
|
uint16_t stewui = TCB3.CCMP;
|
|
printf("CCMP %u", stewui);
|
|
|
|
//TCB3.INTFLAGS = PIN0_bm;
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
ISR(PORTD_PORT_vect){
|
|
// cli();
|
|
falling_edge_counter ++;
|
|
|
|
PORTD.INTFLAGS = PIN2_bm;
|
|
//sei();
|
|
}
|
|
* */
|