diff --git a/prosjekt.X/fan_speeeed.c b/prosjekt.X/fan_speeeed.c new file mode 100644 index 0000000..38540af --- /dev/null +++ b/prosjekt.X/fan_speeeed.c @@ -0,0 +1,107 @@ +#include "fan_speeeed.h" +#include "uart.h" + +uint16_t timer_period_ms = 1; +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_update_period(timer_period_ms); +} + +void TCA0_update_period(uint16_t timer_period) { + TCA0.SINGLE.PERBUF = (F_CPU * (1 / timer_period) / 1024); /* F_CPU * F_IRQ / TCA_prescaler */ +} + +// 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; + edge_counter = 0; + return fan_speed; +} + + +void init_fan_gpio() { + // 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(){ + //Wincontroll disabled + AC0.CTRLB = 0x00; + + //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON + // FAN USE PD6 COMPARE WITH PD3 + AC0.MUXCTRL = AC_MUXPOS_AINP3_gc | AC_MUXNEG_AINN2_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; + + // TURN ON INTERUPT + AC0.INTCTRL = 0x01; +} + + +void init_AC1(){ + //Wincontroll disabled + AC1.CTRLB = 0x00; + + //SELECT POSITIVE AND NEGATIVE INPUTS FOR COMPARRISON + // 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 + 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(); + fan1_edge_counter++; + AC0.STATUS |= 0x10; //CMP flag to 0 + sei(); +} + + +ISR(AC1_AC_vect){ // AC1 vec flag + cli(); + fan2_edge_counter++; + AC1.STATUS |= 0x10; //CMP flag to 0 + sei(); +} + +// TIMER INTERUPT +ISR (TCA0_OVF_vect) { + cli(); + 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(); +} diff --git a/prosjekt.X/fan_speeeed.h b/prosjekt.X/fan_speeeed.h new file mode 100644 index 0000000..160071d --- /dev/null +++ b/prosjekt.X/fan_speeeed.h @@ -0,0 +1,57 @@ +/* + * File: fanseeeed.h + * Author: inami, Helle Augland Grasmo + * + * Created on 13. mars 2024, 13:38 + */ + + + +#ifndef FANSEEEED_H +#define FANSEEEED_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include +#include +#include +#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 (uint16_t timer_period); + + // TAKES INN A TIME AND A THE COUNTED FAN DIPS + // RETURNS THE RPM OF THE FAN + uint16_t RPM_calculation(uint16_t edge_counter, uint16_t time_ms); + + // INITIALISING FAN PORTS + void init_fan_gpio(); + + // INIT AC0 TO COMPARE PD6 AND PD7 + void init_AC0(); + + // INIT AC1 TO COMPARE PD4 AND PD7 + void init_AC1(); + +#ifdef __cplusplus +} +#endif + +#endif /* FANSEEEED_H */ + diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 2fe917d..7dfb0f0 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,9 +4,9 @@ * * Created on March 6, 2024, 12:34 PM */ -#include #include "uart.h" #include "voltage.h" +#include #define RTC_PERIOD (511) #define DELAY_TIME 1000 #include "eeprom.h" @@ -24,11 +24,11 @@ #include // Fan history variables -volatile uint16_t fan1_history[512] = { 0 }; -volatile uint16_t fan2_history[512] = { 0 }; +volatile uint16_t fan1_history[512] = {0}; +volatile uint16_t fan2_history[512] = {0}; // Default config is 500ms sample rate -volatile config_t config = { 500 }; +volatile config_t config = {500}; volatile bool store_config = false; int main() { @@ -41,16 +41,16 @@ int main() { // Read the stored config struct config = read_struct_from_EEPROM(); - + PORTB.DIRSET = PIN3_bm; - + sei(); while (1) { - // If we have made a config change, store it. - if (store_config) { - write_struct_from_EEPROM(config); - store_config = false; - } + // If we have made a config change, store it. + if (store_config) { + write_struct_from_EEPROM(config); + store_config = false; + } } } diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index af7016b..46b7e18 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -4,17 +4,19 @@ - command-handler.h - i2c.h - themistor-temp.h uart.h - eeprom.h voltage.h + fan_speeeed.h + themistor-temp.h + command-handler.h + eeprom.h + i2c.h Makefile + prosjekt.mc3 main.c uart.c - eeprom.c voltage.c - i2c.c - command-handler.c + fan_speeeed.c thermistor-temp.c + command-handler.c + i2c.c + eeprom.c Makefile @@ -42,11 +45,11 @@ nEdbgTool XC8 - 2.46 + 2.45 2 - + @@ -111,6 +114,7 @@ + @@ -156,7 +160,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -181,7 +232,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index b06fa06..a3eb335 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -2,68 +2,57 @@ #include "uart.h" void ADC0_init(void) { - /* Initializing ADC0 pin*/ - /*Voltage reading on pin pd6*/ - PORTD.PIN6CTRL &= ~PORT_ISC_gm; - PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; - - /* Thermistor */ - PORTD.PIN3CTRL &= ~PORT_ISC_gm; - PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN3CTRL &= PORT_PULLUPEN_bm; - - ADC0.CTRLC = ADC_PRESC_DIV4_gc; - VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ - ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ - | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ - /* Select ADC channel */ - ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; + /* Initializing ADC0 pin*/ + /*Voltage reading on pin pd6*/ + PORTD.PIN6CTRL &= ~PORT_ISC_gm; + PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; + + /* Thermistor */ + PORTD.PIN3CTRL &= ~PORT_ISC_gm; + PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN3CTRL &= PORT_PULLUPEN_bm; + + ADC0.CTRLC = ADC_PRESC_DIV4_gc; + VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ + ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ + | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ + /* Select ADC channel */ + ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; } uint16_t ADC0_read(void) { - /* Start ADC conversion */ - ADC0.COMMAND = ADC_STCONV_bm; - /* Wait until ADC conversion done */ - while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { - ; - } - // Clear the interrupt flag by writing 1: - ADC0.INTFLAGS = ADC_RESRDY_bm; - return ADC0.RES; + /* Start ADC conversion */ + ADC0.COMMAND = ADC_STCONV_bm; + /* Wait until ADC conversion done */ + while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { + ; + } + // Clear the interrupt flag by writing 1: + ADC0.INTFLAGS = ADC_RESRDY_bm; + return ADC0.RES; } uint16_t thermistor_voltage_read() { - /* Gets value for the diode */ - ADC0.MUXPOS = 0x03; // Read PD3 - uint16_t adc_val = ADC0_read(); + /* Gets value for the diode */ + ADC0.MUXPOS = 0x03; // Read PD3 + uint16_t adc_val = ADC0_read(); - return adc_val; + return adc_val; } // Gets the value over thermistor -uint16_t external_voltage_read(){ - ADC0.MUXPOS = 0x06; // Read PD6 - uint16_t adc_val = ADC0_read(); - - return adc_val; +uint16_t external_voltage_read() { + ADC0.MUXPOS = 0x06; // Read PD6 + uint16_t adc_val = ADC0_read(); + + return adc_val; } uint16_t internal_voltage_read() { - /* Gets value for the internal voltage reffreance*/ - - ADC0.MUXPOS = 0x44; - uint8_t adc_val = ADC0_read(); - - return adc_val*10; -} + /* Gets value for the internal voltage reffreance*/ -/* Takes inn a messured value and converts it to voltage */ -uint16_t convert_to_voltage(uint16_t adc_val){ - uint16_t min_in= 0; - uint16_t min_out= 0; - uint16_t max_in= 1023; - uint16_t max_out= 3.3; - uint16_t voltage = (adc_val-min_in)*(max_out-min_out)/(max_in-min_in) + min_out; - return voltage; - -} \ No newline at end of file + ADC0.MUXPOS = 0x44; + uint8_t adc_val = ADC0_read(); + + return adc_val * 10; +}