From bf5c83de2a0c9156361d1893f6ff9d25ab908881 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 14:58:06 +0100 Subject: [PATCH 01/19] try to figure out how stuff works --- prosjekt.X/main.c | 45 ++-- prosjekt.X/nbproject/configurations.xml | 340 ++++++++++++------------ 2 files changed, 197 insertions(+), 188 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 5f964b3..11f62d1 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -1,18 +1,27 @@ -/* - * File: main.c - * Author: Sebastian H. Gabrielli - * - * Created on March 6, 2024, 12:34 PM - */ - -#include -#include - -/* - * - */ -int main(int argc, char** argv) { - - return (EXIT_SUCCESS); -} - +/* + * File: main.c + * Author: Sebastian H. Gabrielli + * + * Created on March 6, 2024, 12:34 PM + */ + +#include +#include + +#define ADC_SAMPLES (uint8_t)200 //Number of ADC samples taken +#define ADC_REF_V (float)3.33 +#define ADC_GAIN (float)(ADC_REF_V / 4096) //ADC digital to analog voltage factor +#define ANALOG_GAIN (float)1750 //Hardware gain +#define ADC_CH_POS 6 //AC signal input channel +#define ADC_CH_NEG 7 //AC signal reference point input +V +void ADC_Handler(void); + +/* + * + */ +int main(int argc, char** argv) { + + return (EXIT_SUCCESS); +} + diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index d7f88a4..867e0b8 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,170 +1,170 @@ - - - - - - - - - main.c - - - Makefile - - - Makefile - - - - localhost - AVR128DB48 - - - noID - XC8 - 2.45 - 2 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + Makefile + + + + + main.c + + + Makefile + + + + localhost + AVR128DB48 + + + noID + XC8 + 2.45 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From e66d1979bb915867b25c1150c7ea72297f1cb602 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 15:16:01 +0100 Subject: [PATCH 02/19] added shit --- prosjekt.X/header.h | 78 +++++++++++++++++++ prosjekt.X/main.c | 99 +++++++++++++++++++++---- prosjekt.X/nbproject/configurations.xml | 1 + 3 files changed, 162 insertions(+), 16 deletions(-) create mode 100644 prosjekt.X/header.h diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h new file mode 100644 index 0000000..8a03290 --- /dev/null +++ b/prosjekt.X/header.h @@ -0,0 +1,78 @@ +/* Microchip Technology Inc. and its subsidiaries. You may use this software + * and any derivatives exclusively with Microchip products. + * + * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER + * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED + * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A + * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION + * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. + * + * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, + * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND + * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS + * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE + * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS + * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF + * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. + * + * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE + * TERMS. + */ + +/* + * File: + * Author: + * Comments: + * Revision history: + */ + +// This is a guard condition so that contents of this file are not included +// more than once. +#ifndef XC_HEADER_TEMPLATE_H +#define XC_HEADER_TEMPLATE_H + +#include // include processor files - each processor file is guarded. + +// TODO Insert appropriate #include <> + +// TODO Insert C++ class definitions if appropriate + +// TODO Insert declarations + +// Comment a function and leverage automatic documentation with slash star star +/** +

Function prototype:

+ +

Summary:

+ +

Description:

+ +

Precondition:

+ +

Parameters:

+ +

Returns:

+ +

Example:

+ + + + +

Remarks:

+ */ +// TODO Insert declarations or function prototypes (right here) to leverage +// live documentation + +#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. + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* XC_HEADER_TEMPLATE_H */ + diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 11f62d1..a7c1358 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,24 +4,91 @@ * * Created on March 6, 2024, 12:34 PM */ - +#include "oving.h" +#define F_CPU 4000000UL +#define RTC_PERIOD (511) +#define DELAY_TIME 1000 +#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) +#include +#include +#include +#include +#include #include -#include -#define ADC_SAMPLES (uint8_t)200 //Number of ADC samples taken -#define ADC_REF_V (float)3.33 -#define ADC_GAIN (float)(ADC_REF_V / 4096) //ADC digital to analog voltage factor -#define ANALOG_GAIN (float)1750 //Hardware gain -#define ADC_CH_POS 6 //AC signal input channel -#define ADC_CH_NEG 7 //AC signal reference point input -V -void ADC_Handler(void); +void USART3_init(void); +void USART3_sendChar(char c); +void USART3_sendString(char *str); +static int USART3_printChar(char c, FILE *stream); +uint16_t adcVal; +void led_init(void); +void ADC0_init(void); +uint16_t ADC0_read(); +void ADC0_start(void); +bool ADC0_conersionDone(void); -/* - * - */ -int main(int argc, char** argv) { +static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); - return (EXIT_SUCCESS); +int teller = 100; +int main(void) +{ led_init(); + ADC0_init(); + USART3_init(); + stdout = &USART_stream; + + while (1) + { + int teller = teller++; + if (teller <=100) + { + if (ADC0_conersionDone()) + { + float temp = (1/248.15)+(1/0.25)*(adcVal/1000) + adcVal = ADC0_read(); + printf("%d \n",adcVal); + teller = 0; + } + } + + if(adcVal < 10) + { + + PORTE.OUTSET = PIN2_bm; + + _delay_ms(DELAY_TIME); + } + else + { + PORTE.OUTCLR = PIN2_bm; + + } + if((adcVal < 25) ^ (adcVal > 10)) + { + PORTE.OUTSET = PIN1_bm; + + _delay_ms(DELAY_TIME); + } + else + { + PORTE.OUTCLR = PIN1_bm; + + } + if(adcVal > 25) + { + PORTE.OUTSET = PIN0_bm; + + _delay_ms(DELAY_TIME); + } + else + { + PORTE.OUTCLR = PIN0_bm; + + } + + } + /*char out_str[30] = {0}; + float flt_num = adcVal; + sprintf(out_str, "flt_num = %f\r\n", flt_num); + UartTxStr(out_str); */ + } - diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 867e0b8..a2ca271 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -4,6 +4,7 @@ + header.h Date: Wed, 6 Mar 2024 15:22:40 +0100 Subject: [PATCH 03/19] Small change in added shit --- prosjekt.X/header.h | 124 ++++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 39 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 8a03290..9cfe156 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -26,6 +26,17 @@ * Revision history: */ +#define F_CPU 4000000UL +#define RTC_PERIOD (511) +#define DELAY_TIME 1000 +#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) +#include +#include +#include +#include +#include +#include + // This is a guard condition so that contents of this file are not included // more than once. #ifndef XC_HEADER_TEMPLATE_H @@ -33,46 +44,81 @@ #include // include processor files - each processor file is guarded. -// TODO Insert appropriate #include <> +uint16_t adcVal; -// TODO Insert C++ class definitions if appropriate - -// TODO Insert declarations - -// Comment a function and leverage automatic documentation with slash star star -/** -

Function prototype:

- -

Summary:

- -

Description:

- -

Precondition:

- -

Parameters:

- -

Returns:

- -

Example:

- - - - -

Remarks:

- */ -// TODO Insert declarations or function prototypes (right here) to leverage -// live documentation - -#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. - -#ifdef __cplusplus +void led_init(void) +{ + /* Disable digital input buffer */ + PORTC.PIN6CTRL &= ~PORT_ISC_gm; + PORTC.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; + /* Disable pull-up resistor */ + PORTC.PIN6CTRL &= ~PORT_PULLUPEN_bm; + PORTE.DIRSET = PIN0_bm; + PORTE.DIRSET = PIN1_bm; + PORTE.DIRSET = PIN2_bm; + } -#endif /* __cplusplus */ -#endif /* XC_HEADER_TEMPLATE_H */ +void USART3_init(void) +{ + PORTB.DIRCLR &= ~PIN1_bm; + PORTB.DIRSET |= PIN0_bm; + PORTB.DIRCLR = PIN2_bm; + PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; + USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and + RX */ + USART3.BAUD = USART3_BAUD_RATE (9600); /* Setting the baudrate */ +} +void ADC0_init(void) +{ + + 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; +} + +void USART3_sendChar(char c) +{ + while (!(USART3.STATUS & USART_DREIF_bm)) + { + ; + } + USART3.TXDATAL = c; +} + +void USART3_sendString(char *str) +{ + for(size_t i = 0; i < strlen(str); i++) + { + USART3_sendChar(str[i]); + } +} + +static int USART3_printChar(char c, FILE *stream) +{ + USART3_sendChar(c); + return 0; +} + +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_conersionDone(void) +{ + return (ADC0.INTFLAGS & ADC_RESRDY_bm); +} From 65fd9d375c922184361a181228aade1402c8cc72 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 15:27:54 +0100 Subject: [PATCH 04/19] Corrected header --- prosjekt.X/header.h | 101 +++++++++++--------------------------------- 1 file changed, 24 insertions(+), 77 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 9cfe156..4789bf9 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -44,81 +44,28 @@ #include // include processor files - each processor file is guarded. + +#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. + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* XC_HEADER_TEMPLATE_H */ + + +void USART3_init(void); +void USART3_sendChar(char c); +void USART3_sendString(char *str); +static int USART3_printChar(char c, FILE *stream); uint16_t adcVal; - -void led_init(void) -{ - /* Disable digital input buffer */ - PORTC.PIN6CTRL &= ~PORT_ISC_gm; - PORTC.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; - /* Disable pull-up resistor */ - PORTC.PIN6CTRL &= ~PORT_PULLUPEN_bm; - PORTE.DIRSET = PIN0_bm; - PORTE.DIRSET = PIN1_bm; - PORTE.DIRSET = PIN2_bm; - -} - -void USART3_init(void) -{ - PORTB.DIRCLR &= ~PIN1_bm; - PORTB.DIRSET |= PIN0_bm; - PORTB.DIRCLR = PIN2_bm; - PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; - USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and - RX */ - USART3.BAUD = USART3_BAUD_RATE (9600); /* Setting the baudrate */ -} - -void ADC0_init(void) -{ - - 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; -} - -void USART3_sendChar(char c) -{ - while (!(USART3.STATUS & USART_DREIF_bm)) - { - ; - } - USART3.TXDATAL = c; -} - -void USART3_sendString(char *str) -{ - for(size_t i = 0; i < strlen(str); i++) - { - USART3_sendChar(str[i]); - } -} - -static int USART3_printChar(char c, FILE *stream) -{ - USART3_sendChar(c); - return 0; -} - -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_conersionDone(void) -{ - return (ADC0.INTFLAGS & ADC_RESRDY_bm); -} +void led_init(void); +void ADC0_init(void); +uint16_t ADC0_read(); +void ADC0_start(void); +bool ADC0_conersionDone(void); \ No newline at end of file From 5be15a53f97d35efb460edae75f4b16f6ad23392 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 15:46:56 +0100 Subject: [PATCH 05/19] Remove stuff that was unnecessary --- prosjekt.X/main.c | 130 +++++++++++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 53 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index a7c1358..7f6a09f 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,7 +4,7 @@ * * Created on March 6, 2024, 12:34 PM */ -#include "oving.h" +#include "header.h" #define F_CPU 4000000UL #define RTC_PERIOD (511) #define DELAY_TIME 1000 @@ -16,74 +16,98 @@ #include #include -void USART3_init(void); -void USART3_sendChar(char c); -void USART3_sendString(char *str); -static int USART3_printChar(char c, FILE *stream); + uint16_t adcVal; -void led_init(void); -void ADC0_init(void); -uint16_t ADC0_read(); -void ADC0_start(void); -bool ADC0_conersionDone(void); + +void sensor_init(void) +{ + PORTE.DIRSET = PIN0_bm; + PORTE.DIRSET = PIN1_bm; + PORTE.DIRSET = PIN2_bm; + +} + +void USART3_init(void) +{ + PORTB.DIRCLR &= ~PIN1_bm; + PORTB.DIRSET |= PIN0_bm; + PORTB.DIRCLR = PIN2_bm; + PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; + USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and + RX */ +} + +void ADC0_init(void) +{ + + 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; +} + +void USART3_sendChar(char c) +{ + while (!(USART3.STATUS & USART_DREIF_bm)) + { + ; + } + USART3.TXDATAL = c; +} + +void USART3_sendString(char *str) +{ + for(size_t i = 0; i < strlen(str); i++) + { + USART3_sendChar(str[i]); + } +} + +static int USART3_printChar(char c, FILE *stream) +{ + USART3_sendChar(c); + return 0; +} + +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_conversionDone(void) +{ + return (ADC0.INTFLAGS & ADC_RESRDY_bm); +} + static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); int teller = 100; int main(void) -{ led_init(); +{ sensor_init(); ADC0_init(); USART3_init(); stdout = &USART_stream; while (1) - { - int teller = teller++; - if (teller <=100) - { - if (ADC0_conersionDone()) + { + if (ADC0_conversionDone()) { - float temp = (1/248.15)+(1/0.25)*(adcVal/1000) adcVal = ADC0_read(); printf("%d \n",adcVal); - teller = 0; } } - - if(adcVal < 10) - { - - PORTE.OUTSET = PIN2_bm; - - _delay_ms(DELAY_TIME); - } - else - { - PORTE.OUTCLR = PIN2_bm; - - } - if((adcVal < 25) ^ (adcVal > 10)) - { - PORTE.OUTSET = PIN1_bm; - - _delay_ms(DELAY_TIME); - } - else - { - PORTE.OUTCLR = PIN1_bm; - - } - if(adcVal > 25) - { - PORTE.OUTSET = PIN0_bm; - - _delay_ms(DELAY_TIME); - } - else - { - PORTE.OUTCLR = PIN0_bm; - - } } /*char out_str[30] = {0}; From d2a74fb024fae5363bc66de740a143fc3970f7bf Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 15:54:01 +0100 Subject: [PATCH 06/19] Small fix --- prosjekt.X/main.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 7f6a09f..073a804 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -32,7 +32,6 @@ void USART3_init(void) PORTB.DIRCLR &= ~PIN1_bm; PORTB.DIRSET |= PIN0_bm; PORTB.DIRCLR = PIN2_bm; - PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and RX */ } @@ -110,9 +109,3 @@ int main(void) } } - /*char out_str[30] = {0}; - float flt_num = adcVal; - sprintf(out_str, "flt_num = %f\r\n", flt_num); - UartTxStr(out_str); */ - -} From 0aa9376fa6c5c974910257c59ea6dd4e41ada21b Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 15:54:29 +0100 Subject: [PATCH 07/19] header fixed --- prosjekt.X/header.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 4789bf9..0c836a5 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -64,8 +64,8 @@ void USART3_sendChar(char c); void USART3_sendString(char *str); static int USART3_printChar(char c, FILE *stream); uint16_t adcVal; -void led_init(void); +void sensor_init(void); void ADC0_init(void); uint16_t ADC0_read(); void ADC0_start(void); -bool ADC0_conersionDone(void); \ No newline at end of file +bool ADC0_conversionDone(void); \ No newline at end of file From 6b0608e04b77ff2d5829ffad3098fb8d8049f20a Mon Sep 17 00:00:00 2001 From: Inamr Date: Fri, 8 Mar 2024 13:29:55 +0100 Subject: [PATCH 08/19] Create draft, kinda workes --- prosjekt.X/header.h | 5 ----- prosjekt.X/main.c | 46 ++++----------------------------------------- 2 files changed, 4 insertions(+), 47 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 0c836a5..2e1bc2f 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -58,11 +58,6 @@ extern "C" { #endif /* XC_HEADER_TEMPLATE_H */ - -void USART3_init(void); -void USART3_sendChar(char c); -void USART3_sendString(char *str); -static int USART3_printChar(char c, FILE *stream); uint16_t adcVal; void sensor_init(void); void ADC0_init(void); diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 073a804..d67a150 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -18,24 +18,15 @@ uint16_t adcVal; +uint16_t internalVal; void sensor_init(void) { - PORTE.DIRSET = PIN0_bm; - PORTE.DIRSET = PIN1_bm; - PORTE.DIRSET = PIN2_bm; + PORTB.DIRSET = PIN0_bm; + PORTB.DIRSET = PIN1_bm; } -void USART3_init(void) -{ - PORTB.DIRCLR &= ~PIN1_bm; - PORTB.DIRSET |= PIN0_bm; - PORTB.DIRCLR = PIN2_bm; - USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and - RX */ -} - void ADC0_init(void) { @@ -47,29 +38,6 @@ void ADC0_init(void) ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; } -void USART3_sendChar(char c) -{ - while (!(USART3.STATUS & USART_DREIF_bm)) - { - ; - } - USART3.TXDATAL = c; -} - -void USART3_sendString(char *str) -{ - for(size_t i = 0; i < strlen(str); i++) - { - USART3_sendChar(str[i]); - } -} - -static int USART3_printChar(char c, FILE *stream) -{ - USART3_sendChar(c); - return 0; -} - uint16_t ADC0_read(void) { /* Start ADC conversion */ @@ -89,23 +57,17 @@ bool ADC0_conversionDone(void) return (ADC0.INTFLAGS & ADC_RESRDY_bm); } - -static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); - -int teller = 100; int main(void) { sensor_init(); ADC0_init(); - USART3_init(); - stdout = &USART_stream; while (1) { if (ADC0_conversionDone()) { adcVal = ADC0_read(); - printf("%d \n",adcVal); } + internalVal = VREF.ADC0REF; } } From 3c08e59843a395966ecb73785a38e6d06d17b780 Mon Sep 17 00:00:00 2001 From: Inamr Date: Fri, 8 Mar 2024 14:00:01 +0100 Subject: [PATCH 09/19] Some stuff workes better --- prosjekt.X/main.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index d67a150..a9c73c1 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -5,18 +5,6 @@ * Created on March 6, 2024, 12:34 PM */ #include "header.h" -#define F_CPU 4000000UL -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) -#include -#include -#include -#include -#include -#include - - uint16_t adcVal; uint16_t internalVal; @@ -52,7 +40,7 @@ uint16_t ADC0_read(void) return ADC0.RES; } -bool ADC0_conversionDone(void) +bool ADC0_conversationDone(void) { return (ADC0.INTFLAGS & ADC_RESRDY_bm); } From 8f63e7b0c39cdccf14d77170ae6c731174d91ee0 Mon Sep 17 00:00:00 2001 From: Inamr Date: Mon, 11 Mar 2024 15:48:20 +0100 Subject: [PATCH 10/19] Dont work --- prosjekt.X/header.h | 8 ++- prosjekt.X/main.c | 79 ++++++++++++++++++++----- prosjekt.X/nbproject/configurations.xml | 6 +- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 2e1bc2f..0d90bd2 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -58,9 +58,13 @@ extern "C" { #endif /* XC_HEADER_TEMPLATE_H */ +void USART3_init(void); +void USART3_sendChar(char c); +void USART3_sendString(char *str); +static int USART3_printChar(char c, FILE *stream); uint16_t adcVal; -void sensor_init(void); +void led_init(void); void ADC0_init(void); uint16_t ADC0_read(); void ADC0_start(void); -bool ADC0_conversionDone(void); \ No newline at end of file +bool ADC0_conversationDone(void); \ No newline at end of file diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index a9c73c1..1b1e6dd 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -5,19 +5,38 @@ * Created on March 6, 2024, 12:34 PM */ #include "header.h" -uint16_t adcVal; -uint16_t internalVal; +#define F_CPU 4000000UL +#define RTC_PERIOD (511) +#define DELAY_TIME 1000 +#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) +#include +#include +#include +#include +#include +#include + +static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); void sensor_init(void) { - PORTB.DIRSET = PIN0_bm; - PORTB.DIRSET = PIN1_bm; + /* Disable digital input buffer */ } +void USART3_init(void) +{ + PORTB.DIRCLR &= ~PIN1_bm; + PORTB.DIRSET |= PIN0_bm; + PORTB.DIRCLR = PIN2_bm; + PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; + USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and + RX */ + USART3.BAUD = USART3_BAUD_RATE (9600); /* Setting the baudrate */ +} + void ADC0_init(void) { - ADC0.CTRLC = ADC_PRESC_DIV4_gc; VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ @@ -26,6 +45,29 @@ void ADC0_init(void) ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; } +void USART3_sendChar(char c) +{ + while (!(USART3.STATUS & USART_DREIF_bm)) + { + ; + } + USART3.TXDATAL = c; +} + +void USART3_sendString(char *str) +{ + for(size_t i = 0; i < strlen(str); i++) + { + USART3_sendChar(str[i]); + } +} + +static int USART3_printChar(char c, FILE *stream) +{ + USART3_sendChar(c); + return 0; +} + uint16_t ADC0_read(void) { /* Start ADC conversion */ @@ -42,20 +84,29 @@ uint16_t ADC0_read(void) bool ADC0_conversationDone(void) { - return (ADC0.INTFLAGS & ADC_RESRDY_bm); + if(ADC0.INTFLAGS && ADC_RESRDY_bm){ + return true; + }else{ + return false; + } + } int main(void) { sensor_init(); ADC0_init(); + USART3_init(); + stdout = &USART_stream; while (1) - { - if (ADC0_conversionDone()) + { + //printf("loop\n"); + if (ADC0_conversationDone()) { - adcVal = ADC0_read(); - } - internalVal = VREF.ADC0REF; - } - - } + adcVal = ADC0_read(); + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + printf("The values: \n"); + //printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); + } + } +} \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index a2ca271..8857de3 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -29,10 +29,10 @@ AVR128DB48 - noID + nEdbgTool XC8 - 2.45 - 2 + 2.46 + 3 From 86c127d3cd62d5ba5bd769c347d4c5bf05e6aa0a Mon Sep 17 00:00:00 2001 From: Inamr Date: Mon, 11 Mar 2024 15:54:52 +0100 Subject: [PATCH 11/19] small fix --- prosjekt.X/header.h | 9 +--- prosjekt.X/main.c | 125 ++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 76 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 0d90bd2..aaef92e 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -26,10 +26,6 @@ * Revision history: */ -#define F_CPU 4000000UL -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) #include #include #include @@ -62,9 +58,8 @@ void USART3_init(void); void USART3_sendChar(char c); void USART3_sendString(char *str); static int USART3_printChar(char c, FILE *stream); -uint16_t adcVal; void led_init(void); void ADC0_init(void); -uint16_t ADC0_read(); void ADC0_start(void); -bool ADC0_conversationDone(void); \ No newline at end of file +bool ADC0_conversationDone(void); +uint16_t adcVal; \ No newline at end of file diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 1b1e6dd..8795130 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -18,95 +18,82 @@ static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); -void sensor_init(void) -{ - /* Disable digital input buffer */ - +void sensor_init(void) { + /* Disable digital input buffer */ + } -void USART3_init(void) -{ - PORTB.DIRCLR &= ~PIN1_bm; - PORTB.DIRSET |= PIN0_bm; - PORTB.DIRCLR = PIN2_bm; - PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; - USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and +void USART3_init(void) { + PORTB.DIRCLR &= ~PIN1_bm; + PORTB.DIRSET |= PIN0_bm; + PORTB.DIRCLR = PIN2_bm; + PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; + USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and RX */ - USART3.BAUD = USART3_BAUD_RATE (9600); /* Setting the baudrate */ + USART3.BAUD = USART3_BAUD_RATE(9600); /* Setting the baudrate */ } -void ADC0_init(void) -{ - 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; +void ADC0_init(void) { + 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; } -void USART3_sendChar(char c) -{ - while (!(USART3.STATUS & USART_DREIF_bm)) - { - ; - } - USART3.TXDATAL = c; +void USART3_sendChar(char c) { + while (!(USART3.STATUS & USART_DREIF_bm)) { + ; + } + USART3.TXDATAL = c; } -void USART3_sendString(char *str) -{ - for(size_t i = 0; i < strlen(str); i++) - { - USART3_sendChar(str[i]); - } +void USART3_sendString(char *str) { + for (size_t i = 0; i < strlen(str); i++) { + USART3_sendChar(str[i]); + } } -static int USART3_printChar(char c, FILE *stream) -{ - USART3_sendChar(c); - return 0; +static int USART3_printChar(char c, FILE *stream) { + USART3_sendChar(c); + return 0; } -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; +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){ +bool ADC0_conversationDone(void) { + if (ADC0.INTFLAGS && ADC_RESRDY_bm) { return true; - }else{ + } else { return false; } - + } -int main(void) -{ sensor_init(); - ADC0_init(); - USART3_init(); - stdout = &USART_stream; - - while (1) - { +int main(void) { + sensor_init(); + ADC0_init(); + USART3_init(); + stdout = &USART_stream; + + while (1) { //printf("loop\n"); - if (ADC0_conversationDone()) - { - adcVal = ADC0_read(); - VREF.ADC0REF = VREF_REFSEL_VDD_gc; - printf("The values: \n"); - //printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); - } + if (ADC0_conversationDone()) { + adcVal = ADC0_read(); + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + printf("The values: \n"); + //printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); + } } } \ No newline at end of file From 57a0c0979b3a8d37f4cf72885bf4b45eaa14b07c Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 12:49:42 +0100 Subject: [PATCH 12/19] Fixed ADC0_init --- prosjekt.X/main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 8795130..bc1c4db 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -34,6 +34,10 @@ void USART3_init(void) { } 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 */ @@ -93,7 +97,7 @@ int main(void) { adcVal = ADC0_read(); VREF.ADC0REF = VREF_REFSEL_VDD_gc; printf("The values: \n"); - //printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); + printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); } } } \ No newline at end of file From f657a7d0c70f4eff9e81639994360614e240bfb8 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 12:55:57 +0100 Subject: [PATCH 13/19] Remove UART --- prosjekt.X/header.h | 4 ---- prosjekt.X/main.c | 33 +-------------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index aaef92e..4b61ac2 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -54,10 +54,6 @@ extern "C" { #endif /* XC_HEADER_TEMPLATE_H */ -void USART3_init(void); -void USART3_sendChar(char c); -void USART3_sendString(char *str); -static int USART3_printChar(char c, FILE *stream); void led_init(void); void ADC0_init(void); void ADC0_start(void); diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index bc1c4db..177c5e4 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -8,7 +8,6 @@ #define F_CPU 4000000UL #define RTC_PERIOD (511) #define DELAY_TIME 1000 -#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5) #include #include #include @@ -16,23 +15,12 @@ #include #include -static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); void sensor_init(void) { /* Disable digital input buffer */ } -void USART3_init(void) { - PORTB.DIRCLR &= ~PIN1_bm; - PORTB.DIRSET |= PIN0_bm; - PORTB.DIRCLR = PIN2_bm; - PORTB.PIN2CTRL |= PORT_PULLUPEN_bm; - USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and - RX */ - USART3.BAUD = USART3_BAUD_RATE(9600); /* Setting the baudrate */ -} - void ADC0_init(void) { PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; @@ -46,23 +34,6 @@ void ADC0_init(void) { ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; } -void USART3_sendChar(char c) { - while (!(USART3.STATUS & USART_DREIF_bm)) { - ; - } - USART3.TXDATAL = c; -} - -void USART3_sendString(char *str) { - for (size_t i = 0; i < strlen(str); i++) { - USART3_sendChar(str[i]); - } -} - -static int USART3_printChar(char c, FILE *stream) { - USART3_sendChar(c); - return 0; -} uint16_t ADC0_read(void) { /* Start ADC conversion */ @@ -88,9 +59,7 @@ bool ADC0_conversationDone(void) { int main(void) { sensor_init(); ADC0_init(); - USART3_init(); - stdout = &USART_stream; - + while (1) { //printf("loop\n"); if (ADC0_conversationDone()) { From 9a38ea89118c2376151698c6e7dab68d55b5d883 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 13:13:06 +0100 Subject: [PATCH 14/19] Fix merge issues --- prosjekt.X/header.h | 3 +- prosjekt.X/main.c | 11 +- prosjekt.X/nbproject/configurations.xml | 346 ++++++++++++------------ 3 files changed, 181 insertions(+), 179 deletions(-) diff --git a/prosjekt.X/header.h b/prosjekt.X/header.h index 4b61ac2..d374053 100644 --- a/prosjekt.X/header.h +++ b/prosjekt.X/header.h @@ -54,8 +54,9 @@ extern "C" { #endif /* XC_HEADER_TEMPLATE_H */ -void led_init(void); +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 diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 3157197..8f6879c 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -20,9 +20,6 @@ #include "uart.h" #include -int main() { - init_uart((uint16_t)9600); - stdout = &USART_stream; void sensor_init(void) { /* Disable digital input buffer */ @@ -68,10 +65,12 @@ bool ADC0_conversationDone(void) { } -int main(void) { +int main() { sensor_init(); ADC0_init(); - + init_uart((uint16_t)9600); + stdout = &USART_stream; + while (1) { //printf("loop\n"); if (ADC0_conversationDone()) { @@ -79,6 +78,8 @@ int main(void) { VREF.ADC0REF = VREF_REFSEL_VDD_gc; printf("The values: \n"); printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); + } else { + printf("Not done\n"); } } } \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 9fa5b00..0bf8a5f 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,173 +1,173 @@ - - - - - uart.h - header.h - - - Makefile - - - - - main.c - uart.c - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 3 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + uart.h + header.h + + + Makefile + + + + + main.c + uart.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.46 + 3 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 18ea74bbb6bb4dc6a176b550ffb88199ce455e6b Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 13:26:12 +0100 Subject: [PATCH 15/19] IT WORKS --- prosjekt.X/main.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 8f6879c..8d53fa6 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -20,14 +20,8 @@ #include "uart.h" #include - void sensor_init(void) { - /* Disable digital input buffer */ - - while (1) { - printf("Hello, world!\n"); - _delay_ms(500); - } + /* Disable digital input buffer */ } void ADC0_init(void) { @@ -72,14 +66,8 @@ int main() { stdout = &USART_stream; while (1) { - //printf("loop\n"); - if (ADC0_conversationDone()) { - adcVal = ADC0_read(); - VREF.ADC0REF = VREF_REFSEL_VDD_gc; - printf("The values: \n"); - printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal); - } else { - printf("Not done\n"); - } + adcVal = ADC0_read(); + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); } } \ No newline at end of file From 4105c1ff04a85b4548db7df06ef881a7b07f1446 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 12 Mar 2024 13:37:29 +0100 Subject: [PATCH 16/19] 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 + + From efe623936a9364d75d10e1a08dd21f499664b56c Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 11:46:02 +0100 Subject: [PATCH 17/19] Made it into its own func --- prosjekt.X/main.c | 57 ++++++++++++++++++++++---------------------- prosjekt.X/voltage.c | 7 ++++++ prosjekt.X/voltage.h | 3 ++- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 4e48a49..e13df82 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -1,30 +1,29 @@ -/* - * 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 - -int main() { - sensor_init(); - ADC0_init(); - init_uart((uint16_t)9600); - stdout = &USART_stream; - - while (1) { - uint16_t adcVal = ADC0_read(); - VREF.ADC0REF = VREF_REFSEL_VDD_gc; - 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 + +int main() { + sensor_init(); + ADC0_init(); + init_uart((uint16_t)9600); + stdout = &USART_stream; + + while (1) { + voltage_values(); + //printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); + } } \ No newline at end of file diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 91ad6e5..4474dcd 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -1,4 +1,5 @@ #include "voltage.h" + void sensor_init(void) { /* Disable digital input buffer */ } @@ -27,3 +28,9 @@ uint16_t ADC0_read(void) { ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } + +uint8_t voltage_values(void) { + uint8_t adcVal = ADC0_read(); + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + return adcVal; +} \ No newline at end of file diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index 884a3f9..27bc2df 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -47,7 +47,8 @@ extern "C" { void sensor_init(void); void ADC0_init(void); void ADC0_start(void); -uint16_t ADC0_read(void); +uint8_t ADC0_read(void); +void voltage_values(void); #ifdef __cplusplus } #endif /* __cplusplus */ From 25050d45000355a522cd289bfd84ece295682cfd Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 13 Mar 2024 12:13:38 +0100 Subject: [PATCH 18/19] Made comments on the code --- prosjekt.X/voltage.c | 5 ++++- prosjekt.X/voltage.h | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 4474dcd..127ef13 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -1,12 +1,14 @@ #include "voltage.h" +#include "uart.h" void sensor_init(void) { /* Disable digital input buffer */ } void ADC0_init(void) { + /* Initializing ADC0 pin*/ PORTD.PIN6CTRL &= ~PORT_ISC_gm; - PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; + PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc; @@ -30,6 +32,7 @@ uint16_t ADC0_read(void) { } uint8_t voltage_values(void) { + /* Gets values */ uint8_t adcVal = ADC0_read(); VREF.ADC0REF = VREF_REFSEL_VDD_gc; return adcVal; diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index 27bc2df..a2fdecd 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -44,11 +44,16 @@ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +// Initializing of the sensor pins void sensor_init(void); +// Initializing of the ADC0 pins void ADC0_init(void); -void ADC0_start(void); -uint8_t ADC0_read(void); -void voltage_values(void); +// +//void ADC0_start(void); +//Start ADC conversion +uint16_t ADC0_read(void); +// Gets the value from sensor and internal voltage +uint8_t voltage_values(void); #ifdef __cplusplus } #endif /* __cplusplus */ From 1b6294b04b1b19658c679d27034294b1448b1e46 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Wed, 20 Mar 2024 14:18:45 +0100 Subject: [PATCH 19/19] Fix overflow uint8_t -> uint16_t --- prosjekt.X/main.c | 4 +- prosjekt.X/nbproject/configurations.xml | 354 ++++++++++++------------ prosjekt.X/voltage.c | 9 +- prosjekt.X/voltage.h | 2 +- 4 files changed, 188 insertions(+), 181 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index e13df82..4325f26 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -23,7 +23,7 @@ int main() { stdout = &USART_stream; while (1) { - voltage_values(); - //printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); + uint16_t adcVal = voltage_values(); + printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); } } \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 0799e0f..6efab0a 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,174 +1,180 @@ - - - - - uart.h - voltage.h - - - Makefile - - - - - main.c - uart.c - voltage.c - - - Makefile - - - - localhost - AVR128DB48 - - - nEdbgTool - XC8 - 2.46 - 3 - - - - - - - - - - - - - - - false - false - - - - - - - false - false - - false - - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + uart.h + voltage.h + + + Makefile + + + + + main.c + uart.c + voltage.c + + + Makefile + + + + localhost + AVR128DB48 + + + nEdbgTool + XC8 + 2.45 + 2 + + + + + + + + + + + + + + + false + false + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 127ef13..2983dfb 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -3,6 +3,7 @@ void sensor_init(void) { /* Disable digital input buffer */ + } void ADC0_init(void) { @@ -26,14 +27,14 @@ uint16_t ADC0_read(void) { while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { ; } - /* Clear the interrupt flag by writing 1: */ - ADC0.INTFLAGS = ADC_RESRDY_bm; + // Clear the interrupt flag by writing 1: + ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } -uint8_t voltage_values(void) { +uint16_t voltage_values(void) { /* Gets values */ - uint8_t adcVal = ADC0_read(); + uint16_t adcVal = ADC0_read(); VREF.ADC0REF = VREF_REFSEL_VDD_gc; return adcVal; } \ No newline at end of file diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index a2fdecd..221b741 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -53,7 +53,7 @@ void ADC0_init(void); //Start ADC conversion uint16_t ADC0_read(void); // Gets the value from sensor and internal voltage -uint8_t voltage_values(void); +uint16_t voltage_values(void); #ifdef __cplusplus } #endif /* __cplusplus */