diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 42e5289..5cb448f 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -6,7 +6,7 @@ uint16_t fan_speed; volatile uint16_t falling_edge_counter = 0; #define MAX_PER 0x50 -void TCA0_init() { +void init_TCA0() { //code PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; @@ -19,7 +19,6 @@ void TCA0_init() { TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03; TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm; - //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(); @@ -36,124 +35,14 @@ uint16_t RPM_calculation(uint16_t test, uint16_t time) { return fan_speed; } -void PORT_init() { - //compare or capture - TCB3.CCMP = 0x00; - //count - TCB3.CNT = 0x00; - - PORTA.IN = PIN5_bm; - PORTA.PIN5CTRL = PORT_PULLUPEN_bm; - - //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; - TCB3.CTRLB = 0x03; - - //DBGRUN disabled; - //TCB3.DBGCTRL = 0x00; - - //FILTER disabled; EDGE disabled; CAPTEI enabled; - TCB3.EVCTRL = 0x01; - - //OVF disabled; CAPT enabled; - TCB3.INTCTRL = 0x01; - - //OVF disabledAPT disabled; - //TCB3.INTFLAGS = 0; CAPT disabled; - //TCB3.INTFLAGS = 0x00; - - //Temporary Value - //TCB3.TEMP = 0x00; - //EVSYS.CHANNEL0 = 0x40; - //EVSYS.SWEVENTA = 0x01; - //EVSYS.USERTCB3CAPT = 0x01; - - //RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled; - TCB3.CTRLA = 0x01; -} - -void testsettup() { - PORTA.IN = PIN3_bm; - PORTA.PIN3CTRL = PORT_PULLUPEN_bm; - EVSYS.CHANNEL0 = 0x43; - TCB0.EVCTRL = TCB_CAPTEI_bm; - TCB0.INTCTRL = TCB_CAPT_bm; - TCB0.CTRLB = TCB_CNTMODE_FRQ_gc; - EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; - TCB0.CTRLA = TCB_CLKSEL_TCA0_gc | TCB_ENABLE_bm; - -} -/* -void TCB0_Init(void){ - TCB3.CTRLB = TCB_CNTMODE_FRQPW_gc; /* Input Capture Frequency */ -// TCB3.EVCTRL = TCB_CAPTEI_bm; /* Event Input Enable: enabled */ -// TCB3.EVCTRL |= (1 << TCB_CAPTEI_bp | 1 << TCB_EDGE_bp | 1 << TCB_FILTER_bp); /* Event Input Enable: enabled */ -// TCB3.INTCTRL = TCB_CAPT_bm; /* Capture or Timeout: enabled */ -//TCB3.CTRLA = TCB_CLKSEL_DIV1_gc /* CLK_PER/1 (From Prescaler) - This is needed to be able to count to 40 Hz with a 4 Mhz system clock */ -// | TCB_ENABLE_bm /* Enable: enabled */ -// | TCB_RUNSTDBY_bm; /* Run Standby: enabled */ -//} - -void EVSYS_Init(void) { - /* Zero-cross detector 0 out linked to event channel 0 */ - EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc; - /* TCB uses event channel 0 */ - EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; - - //test - //EVSYS.CHANNEL0 = 0x40; - //EVSYS.SWEVENTA = 0x01; - //EVSYS.USERTCB3CAPT = 0x01; - -} void PORTB_init() { PORTA.OUTSET |= PIN5_bm; PORTA.DIRSET |= PIN5_bm; } + //----------------------------------------------------------------------------------------------------- //-------------------------------------- S E T U P----------------------------------------------------- -/* -ISR ( TCA0_OVF_vect ) { - cli(); - RPM_calculation(); - - TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; - - sei(); -}*/ - -void printnie(uint8_t en, uint8_t to) { - printf("CNT %u", en); - printf("ccmp %u", to); -} - -/*ISR (TCB0_INT_vect){ - uint16_t yo = TCB0.CNT; - - uint16_t stewui = TCB0.CCMP; - - printnie(yo, stewui); - TCB3.INTFLAGS = 0x03; - //TCB3.INTFLAGS = PIN0_bm; - -} - */ - -/* -ISR(PORTD_PORT_vect){ - // cli(); - falling_edge_counter ++; - - PORTD.INTFLAGS = PIN2_bm; - //sei(); -} - * */ diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 0b2e0a6..4955f9c 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -24,15 +24,11 @@ extern "C" { #include #include - void TCA0_init (); - void PORT_init(); - void PORTB_init(); - void testsettup(); - - + void init_TCA0(); void TCA0_update_period_ms (); uint16_t RPM_calculation(uint16_t test, uint16_t time); - void EVSYS_Init(void); + + #ifdef __cplusplus } #endif diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index f1a9e28..4a7415e 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -22,56 +22,6 @@ #include "themistor-temp.h" #include "fan_speeeed.h" -/* -void main_1() { - - init_uart((uint16_t)9600); - stdout = &USART_stream; - TCA0_init ();*/ - //PORT_init(); - //test - //TCB0_Init(); - - //PORT_init(); - //EVSYS_Init(); - //PORTB_init(); - // testsettup(); - - //TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable - - //sei(); - - //while (1) { - /* - printf("loop"); - uint16_t yo = TCB3.CNT; - printf("CNT %u", yo); - uint16_t stewui = TCB3.CCMP; - printf("CCMP %u", stewui); - */ - //EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; - // EVSYS.CHANNEL0 = EVSYS_CHANNEL0_PORTA_PIN5_gc; - /* TCB uses event channel 0 */ - /// EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; - //uint8_t test = TCB3.CCMP; - //printf("ccmp %u", test); - // _delay_ms(500); - // ; - // } -//} - -/*ISR (TCB3_INT_vect){ - uint16_t yo = TCB3.CNT; - printf("CNT %u", yo); - - uint16_t stewui = TCB3.CCMP; - printf("CCMP %u", stewui); - - TCB3.INTFLAGS = 0x03; - //TCB3.INTFLAGS = PIN0_bm; - -}*/ - #define F_CPU 4000000UL #include @@ -79,69 +29,16 @@ void main_1() { #include -#define MAX_PER 0x50 -volatile uint16_t data = 0; -volatile uint16_t cnt = 0; int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; - //TCA Init -/* - PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; - - PORTD.DIR = PIN0_bm; - - TCA0.SINGLE.PERBUF = MAX_PER; - - TCA0.SINGLE.CMP0BUF = MAX_PER / 2; - - TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x3; - - TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm; -*/ - TCA0_init(); - - //TCB Init -/* - PORTA.IN = PIN3_bm; - PORTA.PIN3CTRL = PORT_PULLUPEN_bm; - - EVSYS.CHANNEL0 = 0x43; - - TCB0.EVCTRL = TCB_CAPTEI_bm; - - TCB0.INTCTRL = TCB_CAPT_bm; - - TCB0.CTRLB = TCB_CNTMODE_FRQ_gc; - - EVSYS.USERTCB0CAPT = EVSYS_USER_CHANNEL0_gc; - - TCB0.CTRLA = TCB_CLKSEL_TCA0_gc | TCB_ENABLE_bm; -*/ - //testsettup(); - PORT_init(); sei(); /* Replace with your application code */ while (1) { - if (data) { - printf("TEST1_____ %u ", data); - data = RPM_calculation(data, cnt); - printf("TEST1 %u ", data); - //printf("ccnt %d", cnt); - data = 0; - } } -} - -ISR(TCB0_INT_vect) { - - data = TCB0.CCMP; - cnt = TCB0.CNT; - - TCB0.INTFLAGS = TCB_CAPT_bm; } \ No newline at end of file