From a4a760a8708164ac73c364303b9ea7c022121fb6 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 13:39:35 +0100 Subject: [PATCH 01/33] Made files --- prosjekt.X/fan speeeed.h | 23 +++++++++++++++++++++++ prosjekt.X/fan speeeeeeeed.c | 1 + prosjekt.X/fanspeeeed.c | 1 + prosjekt.X/nbproject/configurations.xml | 2 ++ 4 files changed, 27 insertions(+) create mode 100644 prosjekt.X/fan speeeed.h create mode 100644 prosjekt.X/fan speeeeeeeed.c create mode 100644 prosjekt.X/fanspeeeed.c diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h new file mode 100644 index 0000000..6df19c8 --- /dev/null +++ b/prosjekt.X/fan speeeed.h @@ -0,0 +1,23 @@ +/* + * File: fanseeeed.h + * Author: inami + * + * Created on 13. mars 2024, 13:38 + */ + +#ifndef FANSEEEED_H +#define FANSEEEED_H + +#ifdef __cplusplus +extern "C" { +#endif + + + + +#ifdef __cplusplus +} +#endif + +#endif /* FANSEEEED_H */ + diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/prosjekt.X/fan speeeeeeeed.c @@ -0,0 +1 @@ + diff --git a/prosjekt.X/fanspeeeed.c b/prosjekt.X/fanspeeeed.c new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/prosjekt.X/fanspeeeed.c @@ -0,0 +1 @@ + diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 3cb0be9..6916640 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -6,6 +6,7 @@ projectFiles="true"> eeprom.h themistor-temp.h + fan speeeed.h main.c eeprom.c thermistor-temp.c + fan speeeeeeeed.c Makefile From a0fa78405a9d713f2051175477168baece0b2f36 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 13:57:38 +0100 Subject: [PATCH 02/33] made func --- prosjekt.X/nbproject/configurations.xml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 6916640..ccf6ebe 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -4,7 +4,6 @@ - eeprom.h themistor-temp.h fan speeeed.h @@ -21,9 +20,7 @@ displayName="Source Files" projectFiles="true"> main.c - eeprom.c thermistor-temp.c - fan speeeeeeeed.c Makefile @@ -37,7 +34,7 @@ nEdbgTool XC8 2.46 - 2 + 3 From b7e6bb6d0458584a194bd50547cbe990c40cad68 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 13:58:14 +0100 Subject: [PATCH 03/33] made func --- prosjekt.X/fan speeeed.h | 4 +++- prosjekt.X/fanspeeeed.c | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index 6df19c8..5be0cc8 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -4,6 +4,8 @@ * * Created on 13. mars 2024, 13:38 */ +#include "uart.h" +#include "voltage.h" #ifndef FANSEEEED_H #define FANSEEEED_H @@ -12,7 +14,7 @@ extern "C" { #endif - + int speed(uint16_t adcVal); #ifdef __cplusplus diff --git a/prosjekt.X/fanspeeeed.c b/prosjekt.X/fanspeeeed.c index d3f5a12..29957d7 100644 --- a/prosjekt.X/fanspeeeed.c +++ b/prosjekt.X/fanspeeeed.c @@ -1 +1,5 @@ +#include "fan speeeed.h" +int speed(int internal_voltage){ + return ; +} \ No newline at end of file From 49c0874445f771767182d8045f9b3c38a11995df Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 19 Mar 2024 12:30:20 +0100 Subject: [PATCH 04/33] delete unused source file --- prosjekt.X/fanspeeeed.c | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 prosjekt.X/fanspeeeed.c diff --git a/prosjekt.X/fanspeeeed.c b/prosjekt.X/fanspeeeed.c deleted file mode 100644 index 29957d7..0000000 --- a/prosjekt.X/fanspeeeed.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "fan speeeed.h" - -int speed(int internal_voltage){ - return ; -} \ No newline at end of file From d14044e8df726dddae4eee95a078d02033a9e873 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 19 Mar 2024 12:59:56 +0100 Subject: [PATCH 05/33] Made a formula for calc speed --- prosjekt.X/fan speeeed.h | 14 ++++++++++---- prosjekt.X/fan speeeeeeeed.c | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index 5be0cc8..ecd0919 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -4,8 +4,8 @@ * * Created on 13. mars 2024, 13:38 */ -#include "uart.h" -#include "voltage.h" + + #ifndef FANSEEEED_H #define FANSEEEED_H @@ -13,8 +13,14 @@ #ifdef __cplusplus extern "C" { #endif - - int speed(uint16_t adcVal); + #include "uart.h" + #include + #include + #include + #include + #include + #include + int fan_speed(float value); #ifdef __cplusplus diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index d3f5a12..aca9bec 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -1 +1,21 @@ +#include "fan speeeed.h" + +int fan_speed(float value){ + float fan_speed; + float p = 0; + float timelate = 0; + float starttime = 0; + int oldPR = 0; + int curlPR; + int f; + if (curlPr > 300 && oldPr < 300){ + starttime = millis(); + p =(starttime - timelate); + timelate = startime; + } + oldPR = curlPR; + f = ((1/p)*1000); + fan_speed = (f*60)/2; + return fan_speed; + } \ No newline at end of file From 9aa9c444c14fee0591effd0a19cbc82eed2e0ddb Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 19 Mar 2024 13:11:52 +0100 Subject: [PATCH 06/33] Fix libaries --- prosjekt.X/fan speeeed.h | 2 +- prosjekt.X/fan speeeeeeeed.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index ecd0919..110682f 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -13,7 +13,7 @@ #ifdef __cplusplus extern "C" { #endif - #include "uart.h" + #include #include #include diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index aca9bec..df71665 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -1,21 +1,20 @@ #include "fan speeeed.h" -int fan_speed(float value){ +int fan_speed(float voltage_value){ float fan_speed; - float p = 0; - float timelate = 0; - float starttime = 0; - int oldPR = 0; - int curlPR; + float t = 0; + float past_time = 0; + float start_time = 0; + int old_value = 0; int f; - if (curlPr > 300 && oldPr < 300){ - starttime = millis(); - p =(starttime - timelate); - timelate = startime; + if (voltage_value > 300 && old_value < 300){ + start_time = millis(); + t =(start_time - past_time); + paste_time = start_time; } - oldPR = curlPR; - f = ((1/p)*1000); + voltage_value = old_value; + f = ((1/t)*1000); fan_speed = (f*60)/2; return fan_speed; } \ No newline at end of file From df3d0c6ce634ede0570fdf7d8107aea2798f9044 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 19 Mar 2024 14:29:57 +0100 Subject: [PATCH 07/33] made interrupts as timer --- prosjekt.X/fan speeeed.h | 3 +++ prosjekt.X/fan speeeeeeeed.c | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index 110682f..8774e4f 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -20,6 +20,9 @@ extern "C" { #include #include #include + #include + + int fan_speed(float value); diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index df71665..f3a8d41 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -2,6 +2,8 @@ #include "fan speeeed.h" int fan_speed(float voltage_value){ + init_millis() + sei(); float fan_speed; float t = 0; float past_time = 0; @@ -9,12 +11,22 @@ int fan_speed(float voltage_value){ int old_value = 0; int f; if (voltage_value > 300 && old_value < 300){ - start_time = millis(); + start_time = init_millis() t =(start_time - past_time); - paste_time = start_time; + past_time = start_time; } voltage_value = old_value; f = ((1/t)*1000); fan_speed = (f*60)/2; return fan_speed; - } \ No newline at end of file + } + +ISR(PORTB_PORT_vect) +{ + static int counter = 0; + if (counter >=5){ + PORTB.OUT ^= 0x0F; + counter = 0; + } + PORTB.INFLAFS = PIN2_bm; +} \ No newline at end of file From 88415ddec1724c615662c601459a2ebb99c137da Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 12:33:03 +0100 Subject: [PATCH 08/33] RTC init and timer --- prosjekt.X/fan speeeed.h | 23 ++++----- prosjekt.X/fan speeeeeeeed.c | 96 ++++++++++++++++++++++++++---------- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index 8774e4f..1fe8baf 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -13,18 +13,19 @@ #ifdef __cplusplus extern "C" { #endif - - #include - #include - #include - #include - #include - #include - #include - - - int fan_speed(float value); +#include +#include +#include +#include +#include +#include +#include +#include +#include + int RTC_init(); + int fan_speed(float voltage_value); + inline void fan_speed_toggle(); #ifdef __cplusplus } diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index f3a8d41..59429bf 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -1,32 +1,76 @@ #include "fan speeeed.h" +RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm; -int fan_speed(float voltage_value){ - init_millis() - sei(); - float fan_speed; - float t = 0; - float past_time = 0; - float start_time = 0; - int old_value = 0; - int f; - if (voltage_value > 300 && old_value < 300){ - start_time = init_millis() - t =(start_time - past_time); - past_time = start_time; - } - voltage_value = old_value; - f = ((1/t)*1000); - fan_speed = (f*60)/2; - return fan_speed; +int RTC_init() { + uint8_t temp; + /* Initialize 32.768kHz Oscillator: */ + /* Disable oscillator: */ + temp = CLKCTRL.XOSC32KCTRLA; + temp &= ~CLKCTRL_ENABLE_bm; + /* Writing to protected register */ + ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); + + while (CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm) { + ; /* Wait until XOSC32KS becomes 0 */ } + /* SEL = 0 (Use External Crystal): */ + temp = CLKCTRL.XOSC32KCTRLA; + temp &= ~CLKCTRL_SEL_bm; + /* Writing to protected register */ + ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); -ISR(PORTB_PORT_vect) -{ - static int counter = 0; - if (counter >=5){ - PORTB.OUT ^= 0x0F; - counter = 0; - } - PORTB.INFLAFS = PIN2_bm; + /* Enable oscillator: */ + temp = CLKCTRL.XOSC32KCTRLA; + temp |= CLKCTRL_ENABLE_bm; + /* Writing to protected register */ + ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); + + /* Initialize RTC: */ + while (RTC.STATUS > 0) { + ; /* Wait for all register to be synchronized */ + } + /* 32.768kHz External Crystal Oscillator (XOSC32K) */ + RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc; + /* Run in debug: enabled */ + RTC.DBGCTRL = RTC_DBGRUN_bm; + + RTC.PITINTCTRL = RTC_PI_bm; /* Periodic Interrupt: enabled */ + + RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc /* RTC Clock Cycles 32768 */ + | RTC_PITEN_bm; /* Enable: enabled */ + +} + +int fan_speed(float voltage_value) { + int counter = 0; + int old_value = 0; + + if (voltage_value > 300 && old_value < 300) { + counter++; + } + voltage_value = old_value; + return old_value; +} + +inline void fan_speed_toggle() { + float fan_speed; + float t = 0; + f = ((1 / t)*1000); + fan_speed = (f * 60) / 2; + return fan_speed; +} + +ISR(RTC_PIT_vect) { + RTC.PITINTFLAGS = RTC_PI_bm; + fan_speed_toggle(); +} + +int main(void) { + fan_speed(); + RTC_init(); + sei(); + while (1) { + + } } \ No newline at end of file From 427000ff9eb9711761115e6f3e97bf42f2a6136c Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 14:36:58 +0100 Subject: [PATCH 09/33] made array --- prosjekt.X/fan speeeed.h | 4 ++-- prosjekt.X/fan speeeeeeeed.c | 30 +++++++++++++----------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index 1fe8baf..f13d447 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -24,8 +24,8 @@ extern "C" { #include #include int RTC_init(); - int fan_speed(float voltage_value); - inline void fan_speed_toggle(); + int read_array_get_RPM(int voltage_value); + inline void put_in_array(); #ifdef __cplusplus } diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index 59429bf..504093f 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -41,35 +41,31 @@ int RTC_init() { | RTC_PITEN_bm; /* Enable: enabled */ } - -int fan_speed(float voltage_value) { - int counter = 0; - int old_value = 0; +// skal lage en som finner data hvert ... sekund. Datane skal samles inn i en array. Når arrayen er full skal jeg regne ut rpm. Vet t det er ... mange sek mellom så blir enklere. så noe signalbehandling shit. +int read_array_get_RPM(int voltage_value) { - if (voltage_value > 300 && old_value < 300) { - counter++; - } - voltage_value = old_value; - return old_value; -} - -inline void fan_speed_toggle() { - float fan_speed; - float t = 0; f = ((1 / t)*1000); fan_speed = (f * 60) / 2; return fan_speed; } +inline void put_in_array() { + int voltage_value [30] = {}; + for(int i = 0; i <30; i++) + scanf("%d", &voltage_value[i]); + +} + ISR(RTC_PIT_vect) { RTC.PITINTFLAGS = RTC_PI_bm; - fan_speed_toggle(); + + put_in_array(); } int main(void) { - fan_speed(); - RTC_init(); sei(); + read_array_get_RPM(); + RTC_init(); while (1) { } From dc8a85820418e03cc503f1397b8f8ca531097267 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 15:25:59 +0100 Subject: [PATCH 10/33] resets array and puts diode readings in array --- prosjekt.X/fan speeeed.h | 2 + prosjekt.X/fan speeeeeeeed.c | 44 +-- prosjekt.X/main.c | 73 ++--- prosjekt.X/nbproject/configurations.xml | 366 ++++++++++++------------ prosjekt.X/nbproject/project.xml | 4 +- 5 files changed, 258 insertions(+), 231 deletions(-) diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan speeeed.h index f13d447..7746664 100644 --- a/prosjekt.X/fan speeeed.h +++ b/prosjekt.X/fan speeeed.h @@ -23,6 +23,8 @@ extern "C" { #include #include #include +#include "voltage.h" + int RTC_init(); int read_array_get_RPM(int voltage_value); inline void put_in_array(); diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan speeeeeeeed.c index 504093f..ae17d2c 100644 --- a/prosjekt.X/fan speeeeeeeed.c +++ b/prosjekt.X/fan speeeeeeeed.c @@ -1,5 +1,13 @@ + + #include "fan speeeed.h" + +int voltage_value_counter = 0; +int voltage_value [30]; + + + RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm; int RTC_init() { @@ -42,31 +50,35 @@ int RTC_init() { } // skal lage en som finner data hvert ... sekund. Datane skal samles inn i en array. Når arrayen er full skal jeg regne ut rpm. Vet t det er ... mange sek mellom så blir enklere. så noe signalbehandling shit. -int read_array_get_RPM(int voltage_value) { +int read_array_get_RPM() { f = ((1 / t)*1000); fan_speed = (f * 60) / 2; + return fan_speed; } inline void put_in_array() { - int voltage_value [30] = {}; - for(int i = 0; i <30; i++) - scanf("%d", &voltage_value[i]); - + voltage_values[voltage_value_counter] = read_photodiode_voltage(); + voltage_value_counter++; } +void reset_voltage_array(){ + for(int i = 0; i < voltage_values_counter; i++){ + voltage_values[i]= 0; + } + voltage_value_counter = 0; +} + + ISR(RTC_PIT_vect) { RTC.PITINTFLAGS = RTC_PI_bm; - - put_in_array(); -} - -int main(void) { - sei(); - read_array_get_RPM(); - RTC_init(); - while (1) { - + if(voltage_value_counter < sizeof(voltage_values)/sizeof(voltage_value[0])){ + put_in_array(); } -} \ No newline at end of file + else{ + read_array_get_RPM(); + reset_voltage_array(); + } + +} diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index d9ed9e8..ab2f11a 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -1,36 +1,39 @@ -/* - * File: main.c - * Author: Sebastian H. Gabrielli - * - * Created on March 6, 2024, 12:34 PM - */ -#include "voltage.h" -#include "uart.h" -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#include -#include -#include -#include -#include - -#define F_CPU 4E6 - -#include -#include -#include "uart.h" -#include -#include -#include "themistor-temp.h" - -int main() { - sensor_init(); - ADC0_init(); - init_uart((uint16_t)9600); - stdout = &USART_stream; - - while (1) { - uint16_t adcVal = voltage_values(); - printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); - } +/* + * File: main.c + * Author: Sebastian H. Gabrielli + * + * Created on March 6, 2024, 12:34 PM + */ +#include "voltage.h" +#include "uart.h" +#define RTC_PERIOD (511) +#define DELAY_TIME 1000 +#include +#include +#include +#include +#include + +#define F_CPU 4E6 + +#include +#include +#include "uart.h" +#include +#include +#include "themistor-temp.h" +#include "fan speeeed.h" + + +int main() { + sensor_init(); + ADC0_init(); + RTC_init(); + sei(); + init_uart((uint16_t)9600); + stdout = &USART_stream; + + while (1) { + ; + } } \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 765c75e..87ae9eb 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,179 +1,187 @@ - - - - - themistor-temp.h - fan speeeed.h - - - Makefile - - - - - main.c - thermistor-temp.c - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 3 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + themistor-temp.h + fan speeeed.h + uart.h + voltage.h + + + Makefile + + + + + main.c + thermistor-temp.c + uart.c + voltage.c + fan speeeeeeeed.c + + + + . + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.46 + 3 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/nbproject/project.xml b/prosjekt.X/nbproject/project.xml index 9d6ede3..718a0c6 100644 --- a/prosjekt.X/nbproject/project.xml +++ b/prosjekt.X/nbproject/project.xml @@ -8,7 +8,9 @@ 0 ISO-8859-1 - + + . + default From ce58f79c6f45ad4d75252df89f53a76a1f0f119e Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 15:27:41 +0100 Subject: [PATCH 11/33] renamed files --- prosjekt.X/{fan speeeeeeeed.c => fan_speeeed.c} | 0 prosjekt.X/{fan speeeed.h => fan_speeeed.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename prosjekt.X/{fan speeeeeeeed.c => fan_speeeed.c} (100%) rename prosjekt.X/{fan speeeed.h => fan_speeeed.h} (100%) diff --git a/prosjekt.X/fan speeeeeeeed.c b/prosjekt.X/fan_speeeed.c similarity index 100% rename from prosjekt.X/fan speeeeeeeed.c rename to prosjekt.X/fan_speeeed.c diff --git a/prosjekt.X/fan speeeed.h b/prosjekt.X/fan_speeeed.h similarity index 100% rename from prosjekt.X/fan speeeed.h rename to prosjekt.X/fan_speeeed.h From 48308b9b751b86152dc3c6f5f7d8285e01e485e6 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 15:27:59 +0100 Subject: [PATCH 12/33] renames files --- prosjekt.X/fan_speeeed.c | 2 +- prosjekt.X/main.c | 2 +- prosjekt.X/nbproject/configurations.xml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index ae17d2c..48dd5df 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,7 +1,7 @@ -#include "fan speeeed.h" +#include "fan_speeeed.h" int voltage_value_counter = 0; int voltage_value [30]; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index ab2f11a..ebf7d35 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -22,7 +22,7 @@ #include #include #include "themistor-temp.h" -#include "fan speeeed.h" +#include "fan_speeeed.h" int main() { diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 87ae9eb..7e835d5 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -5,9 +5,9 @@ displayName="Header Files" projectFiles="true"> themistor-temp.h - fan speeeed.h uart.h voltage.h + fan_speeeed.h thermistor-temp.c uart.c voltage.c - fan speeeeeeeed.c + fan_speeeed.c From f1c9a0707e5dfc27692e9c50bf73e4edac480bf3 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 9 Apr 2024 14:53:32 +0200 Subject: [PATCH 13/33] made code for fan speed --- prosjekt.X/fan_speeeed.c | 95 ++++++++++------------------------------ prosjekt.X/fan_speeeed.h | 15 ++++--- 2 files changed, 33 insertions(+), 77 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 48dd5df..ea66791 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,84 +1,35 @@ - - - #include "fan_speeeed.h" -int voltage_value_counter = 0; -int voltage_value [30]; - - - -RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm; - -int RTC_init() { - uint8_t temp; - /* Initialize 32.768kHz Oscillator: */ - /* Disable oscillator: */ - temp = CLKCTRL.XOSC32KCTRLA; - temp &= ~CLKCTRL_ENABLE_bm; - /* Writing to protected register */ - ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); - - while (CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm) { - ; /* Wait until XOSC32KS becomes 0 */ - } - /* SEL = 0 (Use External Crystal): */ - temp = CLKCTRL.XOSC32KCTRLA; - temp &= ~CLKCTRL_SEL_bm; - /* Writing to protected register */ - ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); - - /* Enable oscillator: */ - temp = CLKCTRL.XOSC32KCTRLA; - temp |= CLKCTRL_ENABLE_bm; - /* Writing to protected register */ - ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp); - - /* Initialize RTC: */ - while (RTC.STATUS > 0) { - ; /* Wait for all register to be synchronized */ - } - /* 32.768kHz External Crystal Oscillator (XOSC32K) */ - RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc; - /* Run in debug: enabled */ - RTC.DBGCTRL = RTC_DBGRUN_bm; - - RTC.PITINTCTRL = RTC_PI_bm; /* Periodic Interrupt: enabled */ - - RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc /* RTC Clock Cycles 32768 */ - | RTC_PITEN_bm; /* Enable: enabled */ - +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; } -// skal lage en som finner data hvert ... sekund. Datane skal samles inn i en array. Når arrayen er full skal jeg regne ut rpm. Vet t det er ... mange sek mellom så blir enklere. så noe signalbehandling shit. -int read_array_get_RPM() { - - f = ((1 / t)*1000); - fan_speed = (f * 60) / 2; + +void TCA0_update_period_ms() { +TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period)/1024); /* F_CPU * F_IRQ / TCA_prescaler */ +} + +int read_input_get_RPM() { + fan_speed=(voltage_value_counter/timer_period)*6000*3; + voltage_value_counter = 0; return fan_speed; } -inline void put_in_array() { - voltage_values[voltage_value_counter] = read_photodiode_voltage(); - voltage_value_counter++; -} - -void reset_voltage_array(){ - for(int i = 0; i < voltage_values_counter; i++){ - voltage_values[i]= 0; - } - voltage_value_counter = 0; +void PORT_init(){ + PORTB.PIN2CTRL = PORT_ISC_FALLING_gc; } -ISR(RTC_PIT_vect) { - RTC.PITINTFLAGS = RTC_PI_bm; - if(voltage_value_counter < sizeof(voltage_values)/sizeof(voltage_value[0])){ - put_in_array(); - } - else{ - read_array_get_RPM(); - reset_voltage_array(); - } + +ISR ( TCA0_OVF_vect ) { + read_input_get_RPM(); + TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; +} + +ISR(PORTB_PORT_vect){ + PORTB.INTFLAGS = PIN2_bm; + voltage_value_counter ++; } diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 7746664..476d539 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -23,12 +23,17 @@ extern "C" { #include #include #include -#include "voltage.h" - - int RTC_init(); - int read_array_get_RPM(int voltage_value); - inline void put_in_array(); + volatile uint16_t voltage_value_counter = 0; + int timer_period = 1; + int fan_speed; + + void TCA0_init (); + void PORT_init(); + + void TCA0_update_period_ms (); + int read_input_get_RPM(); + int interrpt(); #ifdef __cplusplus } #endif From a6d1afb0bf672cb7ab328b68536b1d0a888483b6 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 9 Apr 2024 15:26:13 +0200 Subject: [PATCH 14/33] removed old funv from main. copied config from main --- prosjekt.X/main.c | 10 +- prosjekt.X/nbproject/configurations.xml | 194 +----------------------- prosjekt.X/nbproject/project.xml | 50 +++--- 3 files changed, 32 insertions(+), 222 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index ebf7d35..2135b9f 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -14,6 +14,7 @@ #include #include + #define F_CPU 4E6 #include @@ -26,13 +27,14 @@ int main() { - sensor_init(); - ADC0_init(); - RTC_init(); + + + + read_input_get_RPM(); sei(); init_uart((uint16_t)9600); stdout = &USART_stream; - + while (1) { ; } diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 5dd5854..c5d79cd 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,199 +1,9 @@ -<<<<<<< Updated upstream - - - - - themistor-temp.h - uart.h - voltage.h - fan_speeeed.h - - - Makefile - - - - - main.c - thermistor-temp.c - uart.c - voltage.c - fan_speeeed.c - - - - . - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 3 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -======= - themistor-temp.h uart.h voltage.h fan_speeeed.h @@ -211,7 +21,6 @@ displayName="Source Files" projectFiles="true"> main.c - thermistor-temp.c uart.c voltage.c fan_speeeed.c @@ -231,7 +40,7 @@ nEdbgTool XC8 2.46 - 3 + 2 @@ -374,4 +183,3 @@ ->>>>>>> Stashed changes diff --git a/prosjekt.X/nbproject/project.xml b/prosjekt.X/nbproject/project.xml index 718a0c6..9ac6a37 100644 --- a/prosjekt.X/nbproject/project.xml +++ b/prosjekt.X/nbproject/project.xml @@ -1,25 +1,25 @@ - - - com.microchip.mplab.nbide.embedded.makeproject - - - prosjekt - 928a6534-8606-496e-8a74-8a6544316339 - 0 - ISO-8859-1 - - - . - - - - default - 2 - - - - false - - - - + + + com.microchip.mplab.nbide.embedded.makeproject + + + prosjekt + 928a6534-8606-496e-8a74-8a6544316339 + 0 + ISO-8859-1 + + + . + + + + default + 2 + + + + false + + + + From cd97face0fe5cbc38d6fb9477d36929cc4d23782 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 15:39:11 +0200 Subject: [PATCH 15/33] Fix xml file (maybe) --- prosjekt.X/fan_speeeed.c | 1 + prosjekt.X/main.c | 5 +- prosjekt.X/nbproject/configurations.xml | 369 ++++++++++++------------ prosjekt.X/nbproject/project.xml | 4 +- prosjekt.X/voltage.c | 12 - 5 files changed, 187 insertions(+), 204 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index ea66791..f60d65b 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,4 +1,5 @@ #include "fan_speeeed.h" +#include "uart.h" void TCA0_init() { TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 2135b9f..ab8146f 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -15,7 +15,7 @@ #include -#define F_CPU 4E6 +//#define F_CPU 4E6 #include #include @@ -27,9 +27,6 @@ int main() { - - - read_input_get_RPM(); sei(); init_uart((uint16_t)9600); diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index c5d79cd..534a367 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,185 +1,184 @@ - - - - - uart.h - voltage.h - fan_speeeed.h - - - Makefile - - - - - main.c - uart.c - voltage.c - fan_speeeed.c - - - - . - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 2 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + uart.h + voltage.h + fan_speeeed.h + themistor-temp.h + + + + + main.c + uart.c + voltage.c + fan_speeeed.c + thermistor-temp.c + + + Makefile + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.45 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/nbproject/project.xml b/prosjekt.X/nbproject/project.xml index 9ac6a37..cd4eb8b 100644 --- a/prosjekt.X/nbproject/project.xml +++ b/prosjekt.X/nbproject/project.xml @@ -8,9 +8,7 @@ 0 ISO-8859-1 - - . - + default diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 8c43dfd..2e4b60a 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -39,15 +39,3 @@ uint16_t internal_voltage_read() { VREF.ADC0REF = VREF_REFSEL_VDD_gc; } - -uint16_t diode_voltage_read(){ - /* Gets value for the diode */ - uint8_t adcVal = ADC0_read(); - return adcVal; -} -uint16_t internal_voltage_read() { - /* Gets value for the internal voltage reffreance*/ - - VREF.ADC0REF = VREF_REFSEL_VDD_gc; - return VREF_REFSEL_VDD_gc; -} \ No newline at end of file From 63aa397b784880497e5df96976456cb31582c65a Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 9 Apr 2024 16:17:34 +0200 Subject: [PATCH 16/33] testing with hardware cli and function names --- prosjekt.X/fan_speeeed.c | 20 +- prosjekt.X/fan_speeeed.h | 7 +- prosjekt.X/main.c | 5 +- prosjekt.X/nbproject/configurations.xml | 459 ++++++++++++++---------- 4 files changed, 293 insertions(+), 198 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index f60d65b..26c7292 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,6 +1,10 @@ #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 */ @@ -8,13 +12,13 @@ TCA0.SINGLE.PERBUF = 0x0F42; } void TCA0_update_period_ms() { -TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period)/1024); /* F_CPU * F_IRQ / TCA_prescaler */ +TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period_ms)/1024); /* F_CPU * F_IRQ / TCA_prescaler */ } -int read_input_get_RPM() { - - fan_speed=(voltage_value_counter/timer_period)*6000*3; - voltage_value_counter = 0; +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; } @@ -25,12 +29,14 @@ void PORT_init(){ ISR ( TCA0_OVF_vect ) { - read_input_get_RPM(); + cli(); + RPM_calculation(); TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; + sei(); } ISR(PORTB_PORT_vect){ PORTB.INTFLAGS = PIN2_bm; - voltage_value_counter ++; + falling_edge_counter ++; } diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 476d539..337e034 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -23,17 +23,12 @@ extern "C" { #include #include #include - - volatile uint16_t voltage_value_counter = 0; - int timer_period = 1; - int fan_speed; void TCA0_init (); void PORT_init(); void TCA0_update_period_ms (); - int read_input_get_RPM(); - int interrpt(); + uint16_t RPM_calculation(); #ifdef __cplusplus } #endif diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index ab8146f..9c5b6af 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -27,10 +27,13 @@ int main() { - read_input_get_RPM(); + + sei(); init_uart((uint16_t)9600); stdout = &USART_stream; + TCA0_init (); + PORT_init(); while (1) { ; diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 534a367..8884ea2 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,184 +1,275 @@ - - - - - uart.h - voltage.h - fan_speeeed.h - themistor-temp.h - - - - - main.c - uart.c - voltage.c - fan_speeeed.c - thermistor-temp.c - - - Makefile - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.45 - 2 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + uart.h + voltage.h + fan_speeeed.h + themistor-temp.h + + + Makefile + + + + + main.c + uart.c + voltage.c + fan_speeeed.c + thermistor-temp.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.46 + 3 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 23904b03eaf1b0146aa131867640d96eabc915dc Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 9 Apr 2024 16:46:54 +0200 Subject: [PATCH 17/33] right pins --- prosjekt.X/fan_speeeed.c | 14 ++++++++++---- prosjekt.X/nbproject/configurations.xml | 4 +++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 26c7292..d686cab 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -8,7 +8,7 @@ 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.SINGLE.PERBUF = 0x0F42; +TCA0_update_period_ms(); } void TCA0_update_period_ms() { @@ -23,7 +23,7 @@ uint16_t RPM_calculation() { } void PORT_init(){ - PORTB.PIN2CTRL = PORT_ISC_FALLING_gc; + PORTD.PIN2CTRL = PORT_ISC_FALLING_gc; } @@ -33,10 +33,16 @@ ISR ( TCA0_OVF_vect ) { RPM_calculation(); TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; + sei(); } -ISR(PORTB_PORT_vect){ - PORTB.INTFLAGS = PIN2_bm; +ISR(PORTD_PORT_vect){ + cli(); falling_edge_counter ++; + + PORTD.INTFLAGS = PIN2_bm; + sei(); + + } diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 8884ea2..f9bbb88 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -42,7 +42,7 @@ 3 - + @@ -184,6 +184,8 @@ + Date: Wed, 10 Apr 2024 14:53:17 +0200 Subject: [PATCH 18/33] MCC does not work --- prosjekt.X/defmplabxtrace.log | 0 prosjekt.X/defmplabxtrace.log.inx | Bin 0 -> 25 bytes prosjekt.X/fan_speeeed.c | 57 +- prosjekt.X/main.c | 7 +- prosjekt.X/nbproject/configurations.xml | 555 +- prosjekt.X/prosjekt.mc3 | 8047 +++++++++++++++++++++++ 6 files changed, 8378 insertions(+), 288 deletions(-) create mode 100644 prosjekt.X/defmplabxtrace.log create mode 100644 prosjekt.X/defmplabxtrace.log.inx create mode 100644 prosjekt.X/prosjekt.mc3 diff --git a/prosjekt.X/defmplabxtrace.log b/prosjekt.X/defmplabxtrace.log new file mode 100644 index 0000000..e69de29 diff --git a/prosjekt.X/defmplabxtrace.log.inx b/prosjekt.X/defmplabxtrace.log.inx new file mode 100644 index 0000000000000000000000000000000000000000..29197e3a865f7122649ec45edb31854093b4a422 GIT binary patch literal 25 NcmZQzKn1)oE&u?I01f~E literal 0 HcmV?d00001 diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index d686cab..6bc1d1e 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -12,7 +12,7 @@ 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 */ + TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period_ms)/1024); /* F_CPU * F_IRQ / TCA_prescaler */ } uint16_t RPM_calculation() { @@ -23,11 +23,42 @@ uint16_t RPM_calculation() { } void PORT_init(){ - PORTD.PIN2CTRL = PORT_ISC_FALLING_gc; + //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(); @@ -35,14 +66,26 @@ ISR ( TCA0_OVF_vect ) { 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(); + // cli(); falling_edge_counter ++; PORTD.INTFLAGS = PIN2_bm; - sei(); - - + //sei(); } + * */ diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 9c5b6af..c611707 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -27,15 +27,14 @@ int main() { - - - sei(); init_uart((uint16_t)9600); stdout = &USART_stream; TCA0_init (); PORT_init(); - + sei(); + while (1) { + //printf("loop") ; } } \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index f9bbb88..07900d9 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,277 +1,278 @@ - - - - - uart.h - voltage.h - fan_speeeed.h - themistor-temp.h - - - Makefile - - - - - main.c - uart.c - voltage.c - fan_speeeed.c - thermistor-temp.c - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 3 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + uart.h + voltage.h + fan_speeeed.h + themistor-temp.h + + + Makefile + prosjekt.mc3 + + + + + main.c + uart.c + voltage.c + fan_speeeed.c + thermistor-temp.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.45 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/prosjekt.mc3 b/prosjekt.X/prosjekt.mc3 new file mode 100644 index 0000000..367352f --- /dev/null +++ b/prosjekt.X/prosjekt.mc3 @@ -0,0 +1,8047 @@ + + + + + BOD + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.bod.BOD + + + CLKCTRL + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.clkctrl.CLKCTRL + + + CPU + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.cpu.CPU + + + CPUINT + class com.microchip.mcc.mcu8.interruptManager.cpuint.CPUINT + + + Interrupt Manager + class com.microchip.mcc.mcu8.interruptManager.InterruptManager + + + Pin Module + class com.microchip.mcc.mcu8.pinManager.PinManager + + + RSTCTRL + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.rstctrl.RSTCTRL + + + SLPCTRL + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.slpctrl.SLPCTRL + + + System Module + class com.microchip.mcc.mcu8.systemManager.SystemManager + + + WDT + class com.microchip.mcc.mcu8.systemManager.tiny_mega_init.wdt.WDT + + + + + + + + + ISR_BOD_VLM + + + + false + + + + 0 + + + + 1 + + + + 3 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 2 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 1 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + DIS + + + + 128HZ + + + + DIS + + + + BODLEVEL0 + + + + FALLING + + + + disabled + + + + disabled + + + + ABOVE + + + + disabled + + + + disabled + + + + OFF + + + + ISR_CLKCTRL_CFD + + + + + + + + 0 + + + + 1 + + + + 3 + + + + 1 + + + + 0 + + + + 2 + + + + 9 + + + + 10 + + + + 3 + + + + 11 + + + + 0 + + + + 4 + + + + 12 + + + + 1 + + + + 5 + + + + 8 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 2 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 6 + + + + 7 + + + + 0 + + + + 8 + + + + 9 + + + + 1 + + + + 2 + + + + 3 + + + + 5 + + + + 0 + + + + 1 + + + + 1 + + + + 2 + + + + 0 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 0 + + + + 2 + + + + 3 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 0 + + + + 2 + + + + 0 + + + + 1 + + + + 1 + + + + 2 + + + + 3 + + + + 0 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 12 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + disabled + + + + OSCHF + + + + 2X + + + + disabled + + + + disabled + + + + CLKMAIN + + + + disabled + + + + disabled + + + + INT + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + 4M + + + + disabled + + + + 0 + + + + DISABLE + + + + disabled + + + + OSCHF + + + + 1K + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + 256 + + + + disabled + + + + 8M + + + + disabled + + + + CRYSTAL + + + + 216 + + + + 157 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 157 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + SPM + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + disabled + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + 0 + + + + 0 + + + + {"type":"tableDynamicControls","key":"interruptVectorTable","dataArray": [{"Module": {"id":"Module_rowCount_0","data":{"text":"BOD","value":"BOD"}},"Interrupt": {"id":"Interrupt_rowCount_0","data":{"text":"VLM","value":"VLM"}},"Enable": {"id":"Enable_rowCount_0","data":{"text":"false","value":"false"}}}] } + + + + false + + + + false + + + + false + + + + false + + + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + false + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + false + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + none + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + QFN48 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 3 + + + + 4 + + + + 0 + + + + 5 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + 1 + + + + 2 + + + + 0 + + + + 3 + + + + 1 + + + + 2 + + + + 0 + + + + 3 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 4 + + + + 5 + + + + 6 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 0 + + + + 1 + + + + 2 + + + + 0 + + + + 1 + + + + 2 + + + + 0 + + + + 1 + + + + 0 + + + + 3 + + + + 1 + + + + 0 + + + + 3 + + + + 1 + + + + 0 + + + + 3 + + + + 1 + + + + 0 + + + + 3 + + + + 1 + + + + 0 + + + + 3 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + disabled + + + + disabled + + + + INTDISABLE + + + + disabled + + + + 0 + + + + 0 + + + + 0 + + + + disabled + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + PORTA + + + + PORTB + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + DEFAULT + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + disabled + + + + 0 + + + + 1 + + + + 0 + + + + 2 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + disabled + + + + IDLE + + + + OFF + + + + AUTO + + + + Internal high-frequency oscillator + + + + 1000000 + + + + 1-32MHz internal oscillator + + + + 4 MHz system clock (default) + + + + 4000000 + + + + 1 + + + + 2X + + + + 4000000 + + + + 4000000 + + + + Sample frequency is 128 Hz + + + + 1.9V + + + + BOD disabled + + + + Disabled + + + + false + + + + false + + + + false + + + + false + + + + VDD falls below VLM threshold + + + + false + + + + VLM Disabled + + + + Off + + + + Off + + + + 0 + + + + 1 + + + + 3 + + + + 2 + + + + 0 + + + + 1 + + + + 2 + + + + 3 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 2 + + + + 0 + + + + 1 + + + + 1 + + + + 0 + + + + 0 + + + + 1 + + + + 1 + + + + 2 + + + + 0 + + + + 3 + + + + 0 + + + + 1 + + + + 0 + + + + 2 + + + + 1 + + + + 2 + + + + 0 + + + + 5 + + + + 1 + + + + 2 + + + + 6 + + + + 3 + + + + 7 + + + + 4 + + + + 5 + + + + 2 + + + + 8 + + + + 6 + + + + 9 + + + + 3 + + + + 10 + + + + 7 + + + + 4 + + + + 1 + + + + 11 + + + + 0 + + + + 5 + + + + 2 + + + + 8 + + + + 6 + + + + 9 + + + + 3 + + + + 10 + + + + 7 + + + + 4 + + + + 1 + + + + 11 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + 192 + + + + 8 + + + + 0 + + + + DISABLE + + + + BODLEVEL0 + + + + 128Hz + + + + DISABLE + + + + disabled + + + + OSCHF + + + + CRC16 + + + + NOCRC + + + + disabled + + + + GPIO + + + + DUAL + + + + 0MS + + + + OFF + + + + OFF + + + + 5 + + + + 2 + + + + 8 + + + + 6 + + + + 9 + + + + 3 + + + + 10 + + + + 7 + + + + 4 + + + + 1 + + + + 11 + + + + 0 + + + + 5 + + + + 2 + + + + 8 + + + + 6 + + + + 9 + + + + 3 + + + + 10 + + + + 7 + + + + 4 + + + + 1 + + + + 11 + + + + 0 + + + + 0 + + + + 1 + + + + 0 + + + + 1 + + + + 0 + + + + 0 + + + + OFF + + + + OFF + + + + disabled + + + + disabled + + + + \ No newline at end of file From 9cf37461d2d15db53181a425976b96306c688bc5 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 10 Apr 2024 15:52:07 +0200 Subject: [PATCH 19/33] Add something stuff, not work --- prosjekt.X/fan_speeeed.c | 20 +++++++++++++------- prosjekt.X/main.c | 6 +++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 6bc1d1e..aecd729 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -36,25 +36,30 @@ void PORT_init(){ // 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; + TCB3.CTRLB = 0x15; //DBGRUN disabled; - TCB2.DBGCTRL = 0x00; + TCB3.DBGCTRL = 0x00; //FILTER disabled; EDGE disabled; CAPTEI enabled; - TCB2.EVCTRL = 0x01; + TCB3.EVCTRL = 0x01; //OVF disabled; CAPT enabled; - TCB2.INTCTRL = 0x01; + TCB3.INTCTRL = 0x03; //OVF disabled; CAPT disabled; - TCB2.INTFLAGS = 0x00; + TCB3.INTFLAGS = 0x00; //Temporary Value - TCB2.TEMP = 0x00; + TCB3.TEMP = 0x00; //RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled; - TCB2.CTRLA = 0x01; + TCB3.CTRLA = 0x01; + + EVSYS.CHANNEL0 = 0x48 + 0x05; + EVSYS.SWEVENTA = 0x01; + EVSYS.USERTCB3CAPT = 0x01; + } @@ -75,6 +80,7 @@ ISR (TCB3_INT_vect){ uint16_t stewui = TCB3.CCMP; printf("CCMP %u", stewui); + TCB3.INTFLAGS = 0x03; //TCB3.INTFLAGS = PIN0_bm; } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index c611707..32a2a94 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -35,6 +35,10 @@ int main() { while (1) { //printf("loop") - ; + uint16_t yo = TCB3.CNT; + printf("CNT %u", yo); + + uint16_t stewui = TCB3.CCMP; + printf("CCMP %u", stewui); } } \ No newline at end of file From 58014e17d15e633f2c2d2e4d0428f0f30c452d5e Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 16 Apr 2024 13:25:51 +0200 Subject: [PATCH 20/33] not this --- prosjekt.X/fan_speeeed.c | 26 ++++++++++++++++++++++++++ prosjekt.X/main.c | 21 +++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index aecd729..84a6f69 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -62,6 +62,30 @@ void PORT_init(){ } +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; +} + +void PORT_Init(void) +{ + PORTB.OUTSET |= PIN5_bm; + PORTB.DIRSET |= PIN5_bm; +} +//----------------------------------------------------------------------------------------------------- +//-------------------------------------- S E T U P----------------------------------------------------- /* ISR ( TCA0_OVF_vect ) { @@ -77,6 +101,8 @@ ISR (TCB3_INT_vect){ uint16_t yo = TCB3.CNT; printf("CNT %u", yo); + printf("too"); + uint16_t stewui = TCB3.CCMP; printf("CCMP %u", stewui); diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 32a2a94..bf49931 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -30,15 +30,24 @@ int main() { init_uart((uint16_t)9600); stdout = &USART_stream; TCA0_init (); - PORT_init(); + //PORT_init(); + //test + + TCB0_Init(); + EVSYS_Init(); + PORT_Init(); + TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable + sei(); while (1) { - //printf("loop") - uint16_t yo = TCB3.CNT; - printf("CNT %u", yo); + + //printf("loop"); + //uint16_t yo = TCB3.CNT; + //printf("CNT %u", yo); - uint16_t stewui = TCB3.CCMP; - printf("CCMP %u", stewui); + //uint16_t stewui = TCB3.CCMP; + //printf("CCMP %u", stewui) + ; } } \ No newline at end of file From abc13364ecb77b3bfbef69578c084e7288c92941 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 17 Apr 2024 14:59:46 +0200 Subject: [PATCH 21/33] comment somehting --- prosjekt.X/fan_speeeed.c | 35 +++++++++++++++++++++-------------- prosjekt.X/main.c | 20 +++++++++++++++++--- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index aecd729..8f83e25 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -24,10 +24,12 @@ uint16_t RPM_calculation() { void PORT_init(){ //compare or capture - TCB3.CCMP = 0x00; + //TCB3.CCMP = 0x00; //count TCB3.CNT = 0x00; + PORTA.IN = PIN0_bm; + PORTA.PIN0CTRL = 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 */ @@ -36,30 +38,31 @@ void PORT_init(){ // 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 = 0x15; + TCB3.CTRLB = 0x04; //DBGRUN disabled; - TCB3.DBGCTRL = 0x00; + //TCB3.DBGCTRL = 0x00; //FILTER disabled; EDGE disabled; CAPTEI enabled; TCB3.EVCTRL = 0x01; //OVF disabled; CAPT enabled; - TCB3.INTCTRL = 0x03; + TCB3.INTCTRL = 0x01; - //OVF disabled; CAPT disabled; - TCB3.INTFLAGS = 0x00; + //OVF disabledAPT disabled; + //TCB3.INTFLAGS = 0; CAPT disabled; + //TCB3.INTFLAGS = 0x00; //Temporary Value - TCB3.TEMP = 0x00; + //TCB3.TEMP = 0x00; - //RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled; - TCB3.CTRLA = 0x01; - EVSYS.CHANNEL0 = 0x48 + 0x05; - EVSYS.SWEVENTA = 0x01; + EVSYS.CHANNEL0 = 0x40; + //EVSYS.SWEVENTA = 0x01; EVSYS.USERTCB3CAPT = 0x01; + //RUNSTDBY disabled; CASCADE disabled; SYNCUPD disabled; CLKSEL DIV1; ENABLE enabled; + TCB3.CTRLA = 0x01; } @@ -73,13 +76,17 @@ ISR ( TCA0_OVF_vect ) { sei(); }*/ +void printnie(uint8_t en, uint8_t to){ + printf("CNT %u", en); + printf("ccmp %u", to); +} + ISR (TCB3_INT_vect){ uint16_t yo = TCB3.CNT; - printf("CNT %u", yo); - + uint16_t stewui = TCB3.CCMP; - printf("CCMP %u", stewui); + printnie(yo, stewui); TCB3.INTFLAGS = 0x03; //TCB3.INTFLAGS = PIN0_bm; diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 32a2a94..be010ab 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -34,11 +34,25 @@ int main() { sei(); while (1) { - //printf("loop") + /* + printf("loop"); uint16_t yo = TCB3.CNT; printf("CNT %u", yo); - uint16_t stewui = TCB3.CCMP; printf("CCMP %u", stewui); + */ + ; } -} \ No newline at end of file +} + +/*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; + +}*/ \ No newline at end of file From a48a089a3c415ce5d78a3b7ab659ee9df595e4a7 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Sun, 21 Apr 2024 12:43:51 +0200 Subject: [PATCH 22/33] works but not freq --- prosjekt.X/fan_speeeed.c | 115 +++++++++++++++++++++++++-------------- prosjekt.X/fan_speeeed.h | 6 +- prosjekt.X/main.c | 114 ++++++++++++++++++++++++++++++++------ 3 files changed, 176 insertions(+), 59 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 21d1b05..42e5289 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -4,41 +4,55 @@ uint16_t timer_period_ms = 1; uint16_t fan_speed; volatile uint16_t falling_edge_counter = 0; +#define MAX_PER 0x50 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(); + + //code + PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; + // port d ??? why + PORTD.DIR = PIN0_bm; + //code + //TCA0.SINGLE.PERBUF = MAX_PER; + TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ + TCA0.SINGLE.CMP0BUF = MAX_PER/2; + 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(); } void TCA0_update_period_ms() { - TCA0.SINGLE.PERBUF = (F_CPU*(1/timer_period_ms)/1024); /* F_CPU * F_IRQ / TCA_prescaler */ + 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; +uint16_t RPM_calculation(uint16_t test, uint16_t time) { + fan_speed = (test / time)*6000 * 3; + falling_edge_counter = 0; printf("%u", fan_speed); return fan_speed; } -void PORT_init(){ +void PORT_init() { //compare or capture - //TCB3.CCMP = 0x00; + TCB3.CCMP = 0x00; //count TCB3.CNT = 0x00; - - PORTA.IN = PIN0_bm; - PORTA.PIN0CTRL = PORT_PULLUPEN_bm; - + + 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 */ - + // 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 = 0x04; + TCB3.CTRLB = 0x03; //DBGRUN disabled; //TCB3.DBGCTRL = 0x00; @@ -56,36 +70,53 @@ void PORT_init(){ //Temporary Value //TCB3.TEMP = 0x00; - - EVSYS.CHANNEL0 = 0x40; + + //EVSYS.CHANNEL0 = 0x40; //EVSYS.SWEVENTA = 0x01; - EVSYS.USERTCB3CAPT = 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) -{ +// 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 PORT_Init(void) -{ - PORTB.OUTSET |= PIN5_bm; - PORTB.DIRSET |= PIN5_bm; + +void PORTB_init() { + PORTA.OUTSET |= PIN5_bm; + PORTA.DIRSET |= PIN5_bm; } //----------------------------------------------------------------------------------------------------- //-------------------------------------- S E T U P----------------------------------------------------- @@ -100,22 +131,22 @@ ISR ( TCA0_OVF_vect ) { sei(); }*/ -void printnie(uint8_t en, uint8_t to){ +void printnie(uint8_t en, uint8_t to) { printf("CNT %u", en); printf("ccmp %u", to); } -ISR (TCB3_INT_vect){ - uint16_t yo = TCB3.CNT; +/*ISR (TCB0_INT_vect){ + uint16_t yo = TCB0.CNT; - uint16_t stewui = TCB3.CCMP; + uint16_t stewui = TCB0.CCMP; printnie(yo, stewui); TCB3.INTFLAGS = 0x03; //TCB3.INTFLAGS = PIN0_bm; } - + */ /* ISR(PORTD_PORT_vect){ diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 337e034..0b2e0a6 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -26,9 +26,13 @@ extern "C" { void TCA0_init (); void PORT_init(); + void PORTB_init(); + void testsettup(); + void TCA0_update_period_ms (); - uint16_t RPM_calculation(); + 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 06b2b72..f1a9e28 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -13,10 +13,7 @@ #include #include #include - - //#define F_CPU 4E6 - #include #include #include "uart.h" @@ -25,22 +22,26 @@ #include "themistor-temp.h" #include "fan_speeeed.h" - -int main() { +/* +void main_1() { + init_uart((uint16_t)9600); stdout = &USART_stream; - TCA0_init (); + TCA0_init ();*/ //PORT_init(); //test + //TCB0_Init(); - TCB0_Init(); - EVSYS_Init(); - PORT_Init(); - TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable + //PORT_init(); + //EVSYS_Init(); + //PORTB_init(); + // testsettup(); - sei(); + //TCB3.INTCTRL = 0b00000001; // Bit 0 ? CAPT Capture Interrupt Enable + + //sei(); - while (1) { + //while (1) { /* printf("loop"); uint16_t yo = TCB3.CNT; @@ -48,9 +49,16 @@ int main() { 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; @@ -62,4 +70,78 @@ int main() { TCB3.INTFLAGS = 0x03; //TCB3.INTFLAGS = PIN0_bm; -}*/ \ No newline at end of file +}*/ + +#define F_CPU 4000000UL + +#include +#include +#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 From c9b220616daa900f6b0c201a2aead5f1ee779ed4 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 24 Apr 2024 14:24:40 +0200 Subject: [PATCH 23/33] clean comments --- prosjekt.X/fan_speeeed.c | 115 +-------------------------------------- prosjekt.X/fan_speeeed.h | 10 +--- prosjekt.X/main.c | 103 ----------------------------------- 3 files changed, 5 insertions(+), 223 deletions(-) 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 From 8649d59d8d07e8395902d0428c4267b9927d75f9 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Thu, 25 Apr 2024 12:29:10 +0200 Subject: [PATCH 24/33] add positive edge --- prosjekt.X/fan_speeeed.c | 32 ++++++++++++++++++++++++++++++++ prosjekt.X/main.c | 2 ++ 2 files changed, 34 insertions(+) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 5cb448f..b20d219 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,3 +1,5 @@ +#include + #include "fan_speeeed.h" #include "uart.h" @@ -42,6 +44,36 @@ void PORTB_init() { PORTA.OUTSET |= PIN5_bm; PORTA.DIRSET |= PIN5_bm; } +// Voltage reference +#define VDACREF +#define VREF +#define DACREF_VALUE (VDACREF * 256 / VREF) + + +void init_ac(){ + // CONFIGURE PINS AS ANALOG INPUTS + PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; + + // SET BENCH MARK + + + //SELECT POSITIVER INPUTS muxs + AC0.MUXCTRLA = AC_MUXPOS_AINP0_gc | AC_MUXNEG_DACREF_gc; + + // OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN + AC0.CTRLA = 0x40; // 0b01000000 + + // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA + AC0.CTRLA |= PIN0_bm; // 0b00000001 + AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; + + // SET CMP TO 1 + AC0.INTCTRL = 0x01; +} + +ISR(AC0_AC_vect){ // AC0 vec flag + AC0.STATUS |= 0x10; //CMP flag to 0. +} //----------------------------------------------------------------------------------------------------- //-------------------------------------- S E T U P----------------------------------------------------- diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 4a7415e..185972b 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -38,6 +38,8 @@ int main(void) { sei(); + PIN2_bm + /* Replace with your application code */ while (1) { } From eb7d96dd820ab8acdc733169d54cfea30512fe2d Mon Sep 17 00:00:00 2001 From: Elp03 Date: Thu, 25 Apr 2024 14:06:51 +0200 Subject: [PATCH 25/33] maybe --- prosjekt.X/fan_speeeed.c | 27 ++++++++++++++++----------- prosjekt.X/fan_speeeed.h | 2 +- prosjekt.X/main.c | 7 +++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index b20d219..0b5f905 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,4 +1,4 @@ -#include +//#include #include "fan_speeeed.h" #include "uart.h" @@ -50,29 +50,34 @@ void PORTB_init() { #define DACREF_VALUE (VDACREF * 256 / VREF) -void init_ac(){ +void init_ac0(){ // CONFIGURE PINS AS ANALOG INPUTS - PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; + PORTD.PIN6CTRL &= ~ PORT_ISC_gm ; + PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; - // SET BENCH MARK + PORTD.PIN7CTRL &= ~ PORT_ISC_gm ; + PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; + //Wincontroll disabled + AC0.CTRLB = 0x00; - //SELECT POSITIVER INPUTS muxs - AC0.MUXCTRLA = AC_MUXPOS_AINP0_gc | AC_MUXNEG_DACREF_gc; + //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON + // FAN USE PD6 & PD7, COMPARE WITH PD3 + AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXPOS_AINP1_gc | AC_MUXNEG_AINN0_gc ; // OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN - AC0.CTRLA = 0x40; // 0b01000000 - + //AC0.CTRLA = 0x40; // 0b01000000 // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA - AC0.CTRLA |= PIN0_bm; // 0b00000001 - AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; + //AC0.CTRLA |= PIN0_bm; // 0b00000001 + //AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; // SET CMP TO 1 AC0.INTCTRL = 0x01; } ISR(AC0_AC_vect){ // AC0 vec flag - AC0.STATUS |= 0x10; //CMP flag to 0. + AC0.STATUS |= 0x10; //CMP flag to 0. + printf("yo"); } //----------------------------------------------------------------------------------------------------- diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 4955f9c..2511dc6 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -27,7 +27,7 @@ extern "C" { void init_TCA0(); void TCA0_update_period_ms (); uint16_t RPM_calculation(uint16_t test, uint16_t time); - + void init_ac0(); #ifdef __cplusplus } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 185972b..1eb9e16 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -35,12 +35,11 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; - + init_ac0(); sei(); - - PIN2_bm - + /* Replace with your application code */ while (1) { + printf("loop"); } } \ No newline at end of file From 6528a82b0402a1d057c64d7e150b19cb39e9f5cb Mon Sep 17 00:00:00 2001 From: Elp03 Date: Fri, 26 Apr 2024 15:13:45 +0200 Subject: [PATCH 26/33] works --- prosjekt.X/fan_speeeed.c | 18 ++++++++++++++---- prosjekt.X/main.c | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 0b5f905..8a08756 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -52,32 +52,42 @@ void PORTB_init() { void init_ac0(){ // CONFIGURE PINS AS ANALOG INPUTS - PORTD.PIN6CTRL &= ~ PORT_ISC_gm ; + PORTD.DIRSET &= PIN6_bm; + PORTD.PIN6CTRL &= ~ PORT_ISC_gm; PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; PORTD.PIN7CTRL &= ~ PORT_ISC_gm ; PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; + PORTD.PIN3CTRL &= ~ PORT_ISC_gm ; + PORTD.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc; + //Wincontroll disabled AC0.CTRLB = 0x00; //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON // FAN USE PD6 & PD7, COMPARE WITH PD3 - AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXPOS_AINP1_gc | AC_MUXNEG_AINN0_gc ; + AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN0_gc; + //AC_MUXPOS_AINP1_gc // OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN //AC0.CTRLA = 0x40; // 0b01000000 // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA - //AC0.CTRLA |= PIN0_bm; // 0b00000001 + AC0.CTRLA |= PIN0_bm; // 0b00000001 //AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; // SET CMP TO 1 AC0.INTCTRL = 0x01; } +void test(){ + printf("yo"); +} + ISR(AC0_AC_vect){ // AC0 vec flag AC0.STATUS |= 0x10; //CMP flag to 0. - printf("yo"); + //printf("yo"); + test(); } //----------------------------------------------------------------------------------------------------- diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 1eb9e16..567b09e 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -40,6 +40,7 @@ int main(void) { /* Replace with your application code */ while (1) { - printf("loop"); + //printf("loop") + ; } } \ No newline at end of file From 022ccb13a012afb75de8fdbdb265d8cefcee0c66 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Fri, 26 Apr 2024 15:20:28 +0200 Subject: [PATCH 27/33] timer --- prosjekt.X/fan_speeeed.c | 10 +++------- prosjekt.X/main.c | 1 + 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 8a08756..127916f 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -38,17 +38,10 @@ uint16_t RPM_calculation(uint16_t test, uint16_t time) { } - - void PORTB_init() { PORTA.OUTSET |= PIN5_bm; PORTA.DIRSET |= PIN5_bm; } -// Voltage reference -#define VDACREF -#define VREF -#define DACREF_VALUE (VDACREF * 256 / VREF) - void init_ac0(){ // CONFIGURE PINS AS ANALOG INPUTS @@ -86,10 +79,13 @@ void test(){ ISR(AC0_AC_vect){ // AC0 vec flag AC0.STATUS |= 0x10; //CMP flag to 0. + falling_edge_counter++; //printf("yo"); test(); } + + //----------------------------------------------------------------------------------------------------- //-------------------------------------- S E T U P----------------------------------------------------- diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 567b09e..f403438 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -35,6 +35,7 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; + init_TCA0(); init_ac0(); sei(); From f2019dd57acbea618a6a244223d0627c3f07444a Mon Sep 17 00:00:00 2001 From: Elp03 Date: Fri, 26 Apr 2024 15:47:02 +0200 Subject: [PATCH 28/33] timer not worke --- prosjekt.X/fan_speeeed.c | 10 +++++++++- prosjekt.X/main.c | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 127916f..2d4f7a8 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -74,16 +74,24 @@ void init_ac0(){ } void test(){ - printf("yo"); + printf("yo %u", falling_edge_counter); } ISR(AC0_AC_vect){ // AC0 vec flag + cli(); AC0.STATUS |= 0x10; //CMP flag to 0. falling_edge_counter++; //printf("yo"); test(); + sei(); } +ISR (TCA0_OVF_vect) { + cli(); + RPM_calculation(falling_edge_counter,1E-3); + TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; + sei(); +} //----------------------------------------------------------------------------------------------------- diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index f403438..e8d9cc3 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -36,6 +36,7 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; init_TCA0(); + TCA0_update_period_ms(); init_ac0(); sei(); From 10c82d001eea8851c70eb022880660b33f26ae8b Mon Sep 17 00:00:00 2001 From: Elp03 Date: Fri, 26 Apr 2024 17:00:04 +0200 Subject: [PATCH 29/33] Fixed fanspeed --- prosjekt.X/fan_speeeed.c | 48 +++++++++++++++++++++++----------------- prosjekt.X/fan_speeeed.h | 2 +- prosjekt.X/main.c | 1 - 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 2d4f7a8..6db2be2 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -8,32 +8,42 @@ uint16_t fan_speed; volatile uint16_t falling_edge_counter = 0; #define MAX_PER 0x50 + void init_TCA0() { //code - PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; + // PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; // port d ??? why - PORTD.DIR = PIN0_bm; + //PORTD.DIR = PIN0_bm; //code //TCA0.SINGLE.PERBUF = MAX_PER; - TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ - TCA0.SINGLE.CMP0BUF = MAX_PER/2; - TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | 0x03; - TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm; + //TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ + //TCA0.SINGLE.CMP0BUF = MAX_PER/2; + //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(); + TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; + TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ + TCA0.SINGLE.PERBUF = 3906; + //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(uint16_t test, uint16_t time) { - fan_speed = (test / time)*6000 * 3; +uint16_t RPM_calculation(uint16_t time) { + printf("time: %u", time); + fan_speed = (falling_edge_counter / time); + fan_speed = fan_speed/2; + fan_speed = fan_speed * 60/5; + printf("falling edge: %u", falling_edge_counter); falling_edge_counter = 0; - printf("%u", fan_speed); + printf("falling edge: ", falling_edge_counter); + printf("fanspeed %u \n", fan_speed); return fan_speed; } @@ -45,13 +55,15 @@ void PORTB_init() { void init_ac0(){ // CONFIGURE PINS AS ANALOG INPUTS - PORTD.DIRSET &= PIN6_bm; + PORTD.DIRSET &= ~PIN6_bm; PORTD.PIN6CTRL &= ~ PORT_ISC_gm; PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; + PORTD.DIRSET &= ~PIN7_bm; PORTD.PIN7CTRL &= ~ PORT_ISC_gm ; PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; + PORTD.DIRSET &= PIN3_bm; PORTD.PIN3CTRL &= ~ PORT_ISC_gm ; PORTD.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc; @@ -61,34 +73,30 @@ void init_ac0(){ //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON // FAN USE PD6 & PD7, COMPARE WITH PD3 AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN0_gc; - //AC_MUXPOS_AINP1_gc + //AC_MUXPOS_AINP1_gc // OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN //AC0.CTRLA = 0x40; // 0b01000000 // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA - AC0.CTRLA |= PIN0_bm; // 0b00000001 - //AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; + //AC0.CTRLA |= PIN0_bm; // 0b00000001 + AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; - // SET CMP TO 1 + // TURN ON INTERUPT AC0.INTCTRL = 0x01; } -void test(){ - printf("yo %u", falling_edge_counter); -} ISR(AC0_AC_vect){ // AC0 vec flag cli(); - AC0.STATUS |= 0x10; //CMP flag to 0. falling_edge_counter++; + AC0.STATUS |= 0x10; //CMP flag to 0. //printf("yo"); - test(); sei(); } ISR (TCA0_OVF_vect) { cli(); - RPM_calculation(falling_edge_counter,1E-3); + RPM_calculation(1); TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; sei(); } diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 2511dc6..53666c9 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -26,7 +26,7 @@ extern "C" { void init_TCA0(); void TCA0_update_period_ms (); - uint16_t RPM_calculation(uint16_t test, uint16_t time); + uint16_t RPM_calculation(uint16_t time); void init_ac0(); #ifdef __cplusplus diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index e8d9cc3..f403438 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -36,7 +36,6 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; init_TCA0(); - TCA0_update_period_ms(); init_ac0(); sei(); From e01c4d41ad6ea001672e2c42f7aed68de10be938 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Sat, 27 Apr 2024 17:26:35 +0200 Subject: [PATCH 30/33] add second fan --- prosjekt.X/fan_speeeed.c | 94 ++++++++++++++++++++++------------------ prosjekt.X/fan_speeeed.h | 6 ++- prosjekt.X/main.c | 5 ++- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 6db2be2..474c9c6 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -5,26 +5,12 @@ uint16_t timer_period_ms = 1; uint16_t fan_speed; -volatile uint16_t falling_edge_counter = 0; +volatile uint16_t falling_edge_counter_1 = 0; +volatile uint16_t falling_edge_counter_2 = 0; #define MAX_PER 0x50 void init_TCA0() { - - //code - // PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTD_gc; - // port d ??? why - //PORTD.DIR = PIN0_bm; - //code - //TCA0.SINGLE.PERBUF = MAX_PER; - //TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ - //TCA0.SINGLE.CMP0BUF = MAX_PER/2; - //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(); TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ TCA0.SINGLE.PERBUF = 3906; @@ -35,50 +21,44 @@ 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(uint16_t time) { +uint16_t RPM_calculation(uint16_t fancounter, uint16_t time) { printf("time: %u", time); - fan_speed = (falling_edge_counter / time); + fan_speed = (fancounter / time); fan_speed = fan_speed/2; fan_speed = fan_speed * 60/5; - printf("falling edge: %u", falling_edge_counter); - falling_edge_counter = 0; - printf("falling edge: ", falling_edge_counter); + printf("falling edge: %u", fancounter); + fancounter = 0; + printf("falling edge: ", fancounter); printf("fanspeed %u \n", fan_speed); return fan_speed; } -void PORTB_init() { - PORTA.OUTSET |= PIN5_bm; - PORTA.DIRSET |= PIN5_bm; -} - -void init_ac0(){ +void init_fanports() { // CONFIGURE PINS AS ANALOG INPUTS PORTD.DIRSET &= ~PIN6_bm; PORTD.PIN6CTRL &= ~ PORT_ISC_gm; PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; - PORTD.DIRSET &= ~PIN7_bm; + PORTD.DIRSET &= ~PIN4_bm; + PORTD.PIN4CTRL &= ~ PORT_ISC_gm ; + PORTD.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc; + + PORTD.DIRSET &= PIN7_bm; PORTD.PIN7CTRL &= ~ PORT_ISC_gm ; PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; - - PORTD.DIRSET &= PIN3_bm; - PORTD.PIN3CTRL &= ~ PORT_ISC_gm ; - PORTD.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc; - +} + +void init_ac0_fan1(){ //Wincontroll disabled AC0.CTRLB = 0x00; //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON // FAN USE PD6 & PD7, COMPARE WITH PD3 - AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN0_gc; + AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_gc; //AC_MUXPOS_AINP1_gc - // OPTIONAL: ENABLE INNPUT PIN BY WRITING "1" TO OUTEN - //AC0.CTRLA = 0x40; // 0b01000000 // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA - //AC0.CTRLA |= PIN0_bm; // 0b00000001 AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; // TURN ON INTERUPT @@ -86,17 +66,49 @@ void init_ac0(){ } +void init_ac1_fan2(){ + //Wincontroll disabled + AC1.CTRLB = 0x00; + + //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON + // FAN USE PD6 & PD7, COMPARE WITH PD3 + AC1.MUXCTRL = AC_MUXPOS_AINP2_gc | AC_MUXNEG_AINN2_gc; + + // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA + AC1.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; + + // TURN ON INTERUPT + AC1.INTCTRL = 0x01; +} + + ISR(AC0_AC_vect){ // AC0 vec flag cli(); - falling_edge_counter++; - AC0.STATUS |= 0x10; //CMP flag to 0. - //printf("yo"); + falling_edge_counter_1++; + printf("ac0"); + AC0.STATUS |= 0x10; //CMP flag to 0 sei(); } + +ISR(AC1_AC_vect){ // AC0 vec flag + cli(); + falling_edge_counter_2++; + printf("ac1"); + AC1.STATUS |= 0x10; //CMP flag to 0 + sei(); +} + + ISR (TCA0_OVF_vect) { cli(); - RPM_calculation(1); + + printf("fan1"); + RPM_calculation(falling_edge_counter_1,1); + printf("fan2"); + RPM_calculation(falling_edge_counter_2,1); + falling_edge_counter_1 = 0; + falling_edge_counter_2 = 0; TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; sei(); } diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 53666c9..930b468 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -26,8 +26,10 @@ extern "C" { void init_TCA0(); void TCA0_update_period_ms (); - uint16_t RPM_calculation(uint16_t time); - void init_ac0(); + uint16_t RPM_calculation(uint16_t fancounter, uint16_t time); + void init_fanports(); + void init_ac0_fan1(); + void init_ac1_fan2(); #ifdef __cplusplus } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index f403438..a8b60f7 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -36,7 +36,10 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; init_TCA0(); - init_ac0(); + init_fanports(); + init_ac0_fan1(); + init_ac1_fan2(); + sei(); /* Replace with your application code */ From f0a435d57868d98b8bbdd7e1382c5adf2d1c97b0 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Sat, 27 Apr 2024 17:31:00 +0200 Subject: [PATCH 31/33] add comment --- prosjekt.X/fan_speeeed.c | 26 ++++++++++---------------- prosjekt.X/fan_speeeed.h | 16 ++++++++++++++-- prosjekt.X/main.c | 4 ++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 474c9c6..b4f001d 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -22,41 +22,40 @@ void TCA0_update_period_ms() { } uint16_t RPM_calculation(uint16_t fancounter, uint16_t time) { - printf("time: %u", time); fan_speed = (fancounter / time); fan_speed = fan_speed/2; fan_speed = fan_speed * 60/5; - printf("falling edge: %u", fancounter); fancounter = 0; - printf("falling edge: ", fancounter); - printf("fanspeed %u \n", fan_speed); return fan_speed; } void init_fanports() { // CONFIGURE PINS AS ANALOG INPUTS + + // PIN FOR FAN 1 PORTD.DIRSET &= ~PIN6_bm; PORTD.PIN6CTRL &= ~ PORT_ISC_gm; PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; + // PIN FOR FAN 2 PORTD.DIRSET &= ~PIN4_bm; PORTD.PIN4CTRL &= ~ PORT_ISC_gm ; PORTD.PIN4CTRL = PORT_ISC_INPUT_DISABLE_gc; + // PIN FOR REFRENCE PORTD.DIRSET &= PIN7_bm; PORTD.PIN7CTRL &= ~ PORT_ISC_gm ; PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; } -void init_ac0_fan1(){ +void init_ac0(){ //Wincontroll disabled AC0.CTRLB = 0x00; //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON - // FAN USE PD6 & PD7, COMPARE WITH PD3 + // FAN USE PD6 COMPARE WITH PD3 AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_gc; - //AC_MUXPOS_AINP1_gc // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NORMAL_POSEDGE_gc | AC_OUTEN_bm; @@ -66,12 +65,12 @@ void init_ac0_fan1(){ } -void init_ac1_fan2(){ +void init_ac1(){ //Wincontroll disabled AC1.CTRLB = 0x00; //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON - // FAN USE PD6 & PD7, COMPARE WITH PD3 + // FAN USE PD4, COMPARE WITH PD3 AC1.MUXCTRL = AC_MUXPOS_AINP2_gc | AC_MUXNEG_AINN2_gc; // ENABLE AC BY WRITING 1 TO ENABLE BIT IN ACN.CTRLA @@ -85,27 +84,22 @@ void init_ac1_fan2(){ ISR(AC0_AC_vect){ // AC0 vec flag cli(); falling_edge_counter_1++; - printf("ac0"); AC0.STATUS |= 0x10; //CMP flag to 0 sei(); } -ISR(AC1_AC_vect){ // AC0 vec flag +ISR(AC1_AC_vect){ // AC1 vec flag cli(); falling_edge_counter_2++; - printf("ac1"); AC1.STATUS |= 0x10; //CMP flag to 0 sei(); } - +// TIMER INTERUPT ISR (TCA0_OVF_vect) { cli(); - - printf("fan1"); RPM_calculation(falling_edge_counter_1,1); - printf("fan2"); RPM_calculation(falling_edge_counter_2,1); falling_edge_counter_1 = 0; falling_edge_counter_2 = 0; diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 930b468..9f40eaf 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -24,12 +24,24 @@ extern "C" { #include #include + // INITALICE TIMER COUNTER void init_TCA0(); + + // UPDATE TIMER PERIODE void TCA0_update_period_ms (); + + // TAKES INN A TIME AND A THE COUNTED FAN DIPS + // RETURNS THE RPM OF THE FAN uint16_t RPM_calculation(uint16_t fancounter, uint16_t time); + + // INITIALISING FAN PORTS void init_fanports(); - void init_ac0_fan1(); - void init_ac1_fan2(); + + // INIT AC0 TO COMPARE PD6 AND PD7 + void init_ac0(); + + // INIT AC1 TO COMPARE PD4 AND PD7 + void init_ac1(); #ifdef __cplusplus } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index a8b60f7..4d05257 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -37,8 +37,8 @@ int main(void) { stdout = &USART_stream; init_TCA0(); init_fanports(); - init_ac0_fan1(); - init_ac1_fan2(); + init_ac0(); + init_ac1(); sei(); From ac5a054f8b6e04b06183bea9d740d63066d6ef22 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Sat, 27 Apr 2024 18:45:04 +0200 Subject: [PATCH 32/33] no worries --- prosjekt.X/fan_speeeed.c | 50 ++++++++++++++++++---------------------- prosjekt.X/fan_speeeed.h | 15 ++++++------ prosjekt.X/main.c | 15 ++++-------- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index b4f001d..49e0c42 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -1,36 +1,37 @@ -//#include - #include "fan_speeeed.h" #include "uart.h" uint16_t timer_period_ms = 1; -uint16_t fan_speed; -volatile uint16_t falling_edge_counter_1 = 0; -volatile uint16_t falling_edge_counter_2 = 0; -#define MAX_PER 0x50 +uint16_t timer_period = 0.01; +uint16_t fan_speed = 0; +volatile uint16_t fan1_edge_counter = 0; +volatile uint16_t fan2_edge_counter = 0; void init_TCA0() { TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ - TCA0.SINGLE.PERBUF = 3906; - //TCA0_update_period_ms(); + //TCA0.SINGLE.PERBUF = 3906; + TCA0_update_period(); } -void TCA0_update_period_ms() { - TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period_ms) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ +void TCA0_update_period() { + TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ } -uint16_t RPM_calculation(uint16_t fancounter, uint16_t time) { - fan_speed = (fancounter / time); +// COUNTINGS / TIME = FREQUENCY +// FREQ / 2 = GIVES ACTUAL FREQ +// FREQ * SEC-IN-A-MINUTE(60) / FAN-BLADES +uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms) { + fan_speed = (edge_counter / time_ms); fan_speed = fan_speed/2; fan_speed = fan_speed * 60/5; - fancounter = 0; + edge_counter = 0; return fan_speed; } -void init_fanports() { +void init_fan_gpio() { // CONFIGURE PINS AS ANALOG INPUTS // PIN FOR FAN 1 @@ -49,7 +50,7 @@ void init_fanports() { PORTD.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; } -void init_ac0(){ +void init_AC0(){ //Wincontroll disabled AC0.CTRLB = 0x00; @@ -65,7 +66,7 @@ void init_ac0(){ } -void init_ac1(){ +void init_AC1(){ //Wincontroll disabled AC1.CTRLB = 0x00; @@ -83,7 +84,7 @@ void init_ac1(){ ISR(AC0_AC_vect){ // AC0 vec flag cli(); - falling_edge_counter_1++; + fan1_edge_counter++; AC0.STATUS |= 0x10; //CMP flag to 0 sei(); } @@ -91,7 +92,7 @@ ISR(AC0_AC_vect){ // AC0 vec flag ISR(AC1_AC_vect){ // AC1 vec flag cli(); - falling_edge_counter_2++; + fan2_edge_counter++; AC1.STATUS |= 0x10; //CMP flag to 0 sei(); } @@ -99,15 +100,10 @@ ISR(AC1_AC_vect){ // AC1 vec flag // TIMER INTERUPT ISR (TCA0_OVF_vect) { cli(); - RPM_calculation(falling_edge_counter_1,1); - RPM_calculation(falling_edge_counter_2,1); - falling_edge_counter_1 = 0; - falling_edge_counter_2 = 0; + RPM_calculation(fan1_edge_counter,timer_period_ms); + RPM_calculation(fan2_edge_counter,timer_period_ms); + fan1_edge_counter = 0; + fan2_edge_counter = 0; TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; sei(); } - - -//----------------------------------------------------------------------------------------------------- -//-------------------------------------- S E T U P----------------------------------------------------- - diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 9f40eaf..0f430ee 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -1,6 +1,6 @@ /* * File: fanseeeed.h - * Author: inami + * Author: inami, Helle Augland Grasmo * * Created on 13. mars 2024, 13:38 */ @@ -22,26 +22,27 @@ extern "C" { #include #include #include -#include + + // INITALICE TIMER COUNTER void init_TCA0(); - // UPDATE TIMER PERIODE + // UPDATE TIMER PERIOD void TCA0_update_period_ms (); // TAKES INN A TIME AND A THE COUNTED FAN DIPS // RETURNS THE RPM OF THE FAN - uint16_t RPM_calculation(uint16_t fancounter, uint16_t time); + uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms); // INITIALISING FAN PORTS - void init_fanports(); + void init_fan_gpio(); // INIT AC0 TO COMPARE PD6 AND PD7 - void init_ac0(); + void init_AC0(); // INIT AC1 TO COMPARE PD4 AND PD7 - void init_ac1(); + void init_AC1(); #ifdef __cplusplus } diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 4d05257..71d3e55 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -24,10 +24,6 @@ #define F_CPU 4000000UL -#include -#include -#include - @@ -36,15 +32,12 @@ int main(void) { init_uart((uint16_t)9600); stdout = &USART_stream; init_TCA0(); - init_fanports(); - init_ac0(); - init_ac1(); - + init_fan_gpio(); + init_AC0(); + init_AC1(); sei(); - - /* Replace with your application code */ + while (1) { - //printf("loop") ; } } \ No newline at end of file From 772960d0467d71c2c7a88cb6743d8f6e7a7e2d69 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Mon, 29 Apr 2024 08:57:02 +0200 Subject: [PATCH 33/33] Fix comment, variables and function add referance --- prosjekt.X/fan_speeeed.c | 6 ++---- prosjekt.X/fan_speeeed.h | 7 ++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c index 49e0c42..38540af 100644 --- a/prosjekt.X/fan_speeeed.c +++ b/prosjekt.X/fan_speeeed.c @@ -2,7 +2,6 @@ #include "uart.h" uint16_t timer_period_ms = 1; -uint16_t timer_period = 0.01; uint16_t fan_speed = 0; volatile uint16_t fan1_edge_counter = 0; volatile uint16_t fan2_edge_counter = 0; @@ -11,11 +10,10 @@ volatile uint16_t fan2_edge_counter = 0; void init_TCA0() { TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1024_gc ; /* Sysclk /1024 */ - //TCA0.SINGLE.PERBUF = 3906; - TCA0_update_period(); + TCA0_update_period(timer_period_ms); } -void TCA0_update_period() { +void TCA0_update_period(uint16_t timer_period) { TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ } diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h index 0f430ee..160071d 100644 --- a/prosjekt.X/fan_speeeed.h +++ b/prosjekt.X/fan_speeeed.h @@ -23,13 +23,18 @@ extern "C" { #include #include + /* + The code has inspiration from "Getting Started with Analog comparator(AC)" by Microchip for setting up analog comparrator. + * web link: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/TB3211-Getting-Started-with-AC-DS90003211.pdf + * and inspiration form practice 6 for TCA0 setup + */ // INITALICE TIMER COUNTER void init_TCA0(); // UPDATE TIMER PERIOD - void TCA0_update_period_ms (); + void TCA0_update_period_ms (uint16_t timer_period); // TAKES INN A TIME AND A THE COUNTED FAN DIPS // RETURNS THE RPM OF THE FAN