From 4105c1ff04a85b4548db7df06ef881a7b07f1446 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 13:37:29 +0100 Subject: [PATCH] moved voltage fram main to c file --- prosjekt.X/main.c | 49 ++----------------------- prosjekt.X/nbproject/configurations.xml | 3 +- prosjekt.X/voltage.c | 29 +++++++++++++++ prosjekt.X/{header.h => voltage.h} | 16 +++----- 4 files changed, 40 insertions(+), 57 deletions(-) create mode 100644 prosjekt.X/voltage.c rename prosjekt.X/{header.h => voltage.h} (87%) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 8d53fa6..4e48a49 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,7 +4,8 @@ * * Created on March 6, 2024, 12:34 PM */ -#include "header.h" +#include "voltage.h" +#include "uart.h" #define RTC_PERIOD (511) #define DELAY_TIME 1000 #include @@ -15,50 +16,6 @@ #define F_CPU 4E6 -#include -#include -#include "uart.h" -#include - -void sensor_init(void) { - /* Disable digital input buffer */ -} - -void ADC0_init(void) { - PORTD.PIN6CTRL &= ~PORT_ISC_gm; - PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; - PORTD.PIN6CTRL &= 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; -} - -bool ADC0_conversationDone(void) { - if (ADC0.INTFLAGS && ADC_RESRDY_bm) { - return true; - } else { - return false; - } - -} - int main() { sensor_init(); ADC0_init(); @@ -66,7 +23,7 @@ int main() { stdout = &USART_stream; while (1) { - adcVal = ADC0_read(); + uint16_t adcVal = ADC0_read(); VREF.ADC0REF = VREF_REFSEL_VDD_gc; printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); } diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 0bf8a5f..0799e0f 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -5,7 +5,7 @@ displayName="Header Files" projectFiles="true"> uart.h - header.h + voltage.h main.c uart.c + voltage.c Makefile diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c new file mode 100644 index 0000000..91ad6e5 --- /dev/null +++ b/prosjekt.X/voltage.c @@ -0,0 +1,29 @@ +#include "voltage.h" +void sensor_init(void) { + /* Disable digital input buffer */ +} + +void ADC0_init(void) { + PORTD.PIN6CTRL &= ~PORT_ISC_gm; + PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; + PORTD.PIN6CTRL &= 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; +} diff --git a/prosjekt.X/header.h b/prosjekt.X/voltage.h similarity index 87% rename from prosjekt.X/header.h rename to prosjekt.X/voltage.h index d374053..884a3f9 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/voltage.h @@ -44,19 +44,15 @@ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ - - // TODO If C++ is being used, regular C code needs function names to have C - // linkage so the functions can be used by the c code. - +void sensor_init(void); +void ADC0_init(void); +void ADC0_start(void); +uint16_t ADC0_read(void); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* XC_HEADER_TEMPLATE_H */ -void sensor_init(void); -void ADC0_init(void); -void ADC0_start(void); -bool ADC0_conversationDone(void); -uint16_t ADC0_read(void); -uint16_t adcVal; \ No newline at end of file + +