From bf5c83de2a0c9156361d1893f6ff9d25ab908881 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 6 Mar 2024 14:58:06 +0100 Subject: [PATCH 01/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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 */ From b86226636219bd0ab443f62975011abd4d22b36b Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 15:45:24 +0100 Subject: [PATCH 20/32] Sync changes and added func for both voltages --- prosjekt.X/nbproject/configurations.xml | 190 ++++++++++++++++++++++++ prosjekt.X/nbproject/project.xml | 48 +++--- prosjekt.X/voltage.c | 19 +-- prosjekt.X/voltage.h | 7 +- 4 files changed, 229 insertions(+), 35 deletions(-) diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 6efab0a..ff5c2b4 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,3 +1,4 @@ +<<<<<<< Updated upstream @@ -178,3 +179,192 @@ +======= + + + + + 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +>>>>>>> Stashed changes diff --git a/prosjekt.X/nbproject/project.xml b/prosjekt.X/nbproject/project.xml index 9d6ede3..9ac6a37 100644 --- a/prosjekt.X/nbproject/project.xml +++ b/prosjekt.X/nbproject/project.xml @@ -1,23 +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 + + + + diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 2983dfb..3a26823 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -1,11 +1,6 @@ #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; @@ -32,9 +27,15 @@ uint16_t ADC0_read(void) { return ADC0.RES; } -uint16_t voltage_values(void) { - /* Gets values */ - uint16_t adcVal = ADC0_read(); - VREF.ADC0REF = VREF_REFSEL_VDD_gc; +uint16_t diode_voltage_read(void){ + /* Gets value for the diode */ + uint8_t adcVal = ADC0_read(); + return adcVal; +} +uint16_t internal_voltage_read(void) { + /* Gets value for the internal voltage reffreance*/ + + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + } \ No newline at end of file diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index 221b741..381f8fd 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -44,16 +44,17 @@ #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); //Start ADC conversion uint16_t ADC0_read(void); // Gets the value from sensor and internal voltage -uint16_t voltage_values(void); +uint16_t internal_voltage_read(void); +uint16_t diode_voltage_read(void) #ifdef __cplusplus } #endif /* __cplusplus */ From 980e6b7c5a555236b45941b6f0b3b60eb8ccb33f Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 15:53:26 +0100 Subject: [PATCH 21/32] renamed diode voltage read --- prosjekt.X/voltage.c | 11 ++++++----- prosjekt.X/voltage.h | 18 +++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 3a26823..95b79a3 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -23,19 +23,20 @@ uint16_t ADC0_read(void) { ; } // Clear the interrupt flag by writing 1: - ADC0.INTFLAGS = ADC_RESRDY_bm; + ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } -uint16_t diode_voltage_read(void){ +uint16_t diode_voltage_read() { /* Gets value for the diode */ uint8_t adcVal = ADC0_read(); return adcVal; } -uint16_t internal_voltage_read(void) { + +uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ - + VREF.ADC0REF = VREF_REFSEL_VDD_gc; - + } \ No newline at end of file diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index 381f8fd..c006796 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -45,16 +45,16 @@ extern "C" { #endif /* __cplusplus */ -// Initializing of the ADC0 pins + // Initializing of the ADC0 pins -void ADC0_init(void); -// -//void ADC0_start(void); -//Start ADC conversion -uint16_t ADC0_read(void); -// Gets the value from sensor and internal voltage -uint16_t internal_voltage_read(void); -uint16_t diode_voltage_read(void) + void ADC0_init(void); + // + //void ADC0_start(void); + //Start ADC conversion + uint16_t ADC0_read(void); + // Gets the value from sensor and internal voltage + uint16_t internal_voltage_read(); + uint16_t diode_voltage_read(); #ifdef __cplusplus } #endif /* __cplusplus */ From b8e2305349739e4beb2ccda01d62bd43fa35e286 Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 16:09:16 +0100 Subject: [PATCH 22/32] Auto stash before merge of "voltage-reading" and "origin/voltage-reading" --- prosjekt.X/voltage.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 95b79a3..c31322d 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -39,4 +39,14 @@ uint16_t internal_voltage_read() { VREF.ADC0REF = VREF_REFSEL_VDD_gc; +uint16_t diode_voltage_read(void){ + /* Gets value for the diode */ + uint8_t adcVal = ADC0_read(); + return adcVal; +} +uint16_t internal_voltage_read(void) { + /* Gets value for the internal voltage reffreance*/ + + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + } \ No newline at end of file From be82194a6d634905e46070cb25827c2965c3c0bb Mon Sep 17 00:00:00 2001 From: Inamr Date: Wed, 20 Mar 2024 16:11:01 +0100 Subject: [PATCH 23/32] add bracket --- prosjekt.X/voltage.c | 1 + 1 file changed, 1 insertion(+) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index c31322d..6366edd 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -38,6 +38,7 @@ uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ VREF.ADC0REF = VREF_REFSEL_VDD_gc; +} uint16_t diode_voltage_read(void){ /* Gets value for the diode */ From c189291012ac440812c4b4b3ced53a0663fa1b87 Mon Sep 17 00:00:00 2001 From: Inamr Date: Tue, 26 Mar 2024 13:35:36 +0100 Subject: [PATCH 24/32] test --- prosjekt.X/voltage.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 6366edd..8c43dfd 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -40,14 +40,14 @@ uint16_t internal_voltage_read() { VREF.ADC0REF = VREF_REFSEL_VDD_gc; } -uint16_t diode_voltage_read(void){ +uint16_t diode_voltage_read(){ /* Gets value for the diode */ uint8_t adcVal = ADC0_read(); return adcVal; } -uint16_t internal_voltage_read(void) { +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 a9f5c422432c5f24050baa53bab98b60778f8025 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 13:37:38 +0200 Subject: [PATCH 25/32] Fix comments --- prosjekt.X/main.c | 3 +- prosjekt.X/nbproject/configurations.xml | 200 +----------------------- prosjekt.X/nbproject/project.xml | 4 +- prosjekt.X/voltage.c | 26 +-- prosjekt.X/voltage.h | 8 +- 5 files changed, 24 insertions(+), 217 deletions(-) diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 4325f26..c6898b6 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -17,13 +17,12 @@ #define F_CPU 4E6 int main() { - sensor_init(); ADC0_init(); init_uart((uint16_t)9600); stdout = &USART_stream; while (1) { - uint16_t adcVal = voltage_values(); + uint16_t adcVal = ADC0_read(); 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 ff5c2b4..75cd8bb 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,4 +1,3 @@ -<<<<<<< Updated upstream @@ -8,11 +7,6 @@ uart.h voltage.h - - Makefile - @@ -24,6 +18,11 @@ uart.c voltage.c + + Makefile + Makefile @@ -179,192 +178,3 @@ -======= - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->>>>>>> Stashed changes 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..6e61b12 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -3,9 +3,21 @@ 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; + + /* Voltage reading T2 */ + PORTD.PIN2CTRL &= ~PORT_ISC_gm; + PORTD.PIN2CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN2CTRL &= PORT_PULLUPEN_bm; + + /* Voltage reading T1 */ + PORTD.PIN1CTRL &= ~PORT_ISC_gm; + PORTD.PIN1CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN1CTRL &= PORT_PULLUPEN_bm; + ADC0.CTRLC = ADC_PRESC_DIV4_gc; VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */ @@ -27,7 +39,7 @@ uint16_t ADC0_read(void) { return ADC0.RES; } -uint16_t diode_voltage_read() { +uint16_t photodiode_voltage_read() { /* Gets value for the diode */ uint8_t adcVal = ADC0_read(); @@ -39,15 +51,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 diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index c006796..a001a19 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -20,9 +20,9 @@ */ /* - * File: - * Author: - * Comments: + * File: Voltage.h + * Author: Sebastian Hernø Gabrielli, Helle Augland Grasmo, Ina Min Rørnes + * Comments: * Revision history: */ @@ -54,7 +54,7 @@ extern "C" { uint16_t ADC0_read(void); // Gets the value from sensor and internal voltage uint16_t internal_voltage_read(); - uint16_t diode_voltage_read(); + uint16_t photodiode_voltage_read(); #ifdef __cplusplus } #endif /* __cplusplus */ From 953034cab828ca6153dd9a9e7fa59435ad7670ca Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 13:50:02 +0200 Subject: [PATCH 26/32] Add thermistor adc reading --- prosjekt.X/voltage.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 6e61b12..08a0251 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -8,17 +8,11 @@ void ADC0_init(void) { PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ PORTD.PIN6CTRL &= PORT_PULLUPEN_bm; - /* Voltage reading T2 */ - PORTD.PIN2CTRL &= ~PORT_ISC_gm; - PORTD.PIN2CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN2CTRL &= PORT_PULLUPEN_bm; + /* Thermistor */ + PORTD.PIN3CTRL &= ~PORT_ISC_gm; + PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN3CTRL &= PORT_PULLUPEN_bm; - /* Voltage reading T1 */ - PORTD.PIN1CTRL &= ~PORT_ISC_gm; - PORTD.PIN1CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ - PORTD.PIN1CTRL &= 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 */ From ae28be55d3ede4b0a31ef47226460e6b88941418 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 14:36:11 +0200 Subject: [PATCH 27/32] Fix muxpos --- prosjekt.X/voltage.c | 5 +++-- prosjekt.X/voltage.h | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 08a0251..4175c41 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -33,8 +33,9 @@ uint16_t ADC0_read(void) { return ADC0.RES; } -uint16_t photodiode_voltage_read() { +uint16_t thermistor_voltage_read() { /* Gets value for the diode */ + ADC0.MUXPOS = 0x03; uint8_t adcVal = ADC0_read(); return adcVal; @@ -43,5 +44,5 @@ uint16_t photodiode_voltage_read() { uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ - VREF.ADC0REF = VREF_REFSEL_VDD_gc; + return VREF.ADC0REF = VREF_REFSEL_VDD_gc; } diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index a001a19..dde41a5 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -54,7 +54,8 @@ extern "C" { uint16_t ADC0_read(void); // Gets the value from sensor and internal voltage uint16_t internal_voltage_read(); - uint16_t photodiode_voltage_read(); + uint16_t thermistor_voltage_read(); + uint16_t internal_voltage_read(); #ifdef __cplusplus } #endif /* __cplusplus */ From 91ee8b1af8b802a6d3904454ca1117ba5e1c3a46 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 14:56:14 +0200 Subject: [PATCH 28/32] Add muxpos to internal voltage --- prosjekt.X/voltage.c | 11 ++++++++--- prosjekt.X/voltage.h | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index 4175c41..c62d872 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -36,13 +36,18 @@ uint16_t ADC0_read(void) { uint16_t thermistor_voltage_read() { /* Gets value for the diode */ ADC0.MUXPOS = 0x03; - uint8_t adcVal = ADC0_read(); + uint8_t adc_val = ADC0_read(); - return adcVal; + return adc_val; } uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ - return VREF.ADC0REF = VREF_REFSEL_VDD_gc; + VREF.ADC0REF = VREF_REFSEL_VDD_gc; + + ADC0.MUXPOS = 0x44; + uint8_t adc_val = ADC0_read(); + + return adc_val*10; } diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index dde41a5..af2e76d 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -55,7 +55,6 @@ extern "C" { // Gets the value from sensor and internal voltage uint16_t internal_voltage_read(); uint16_t thermistor_voltage_read(); - uint16_t internal_voltage_read(); #ifdef __cplusplus } #endif /* __cplusplus */ From b0c0efa4fc24b9ef0c58aa588e837bc4f3f8fa03 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 15:28:57 +0200 Subject: [PATCH 29/32] Delete a line --- prosjekt.X/voltage.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index c62d872..eec2bd2 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -43,8 +43,6 @@ uint16_t thermistor_voltage_read() { uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ - - VREF.ADC0REF = VREF_REFSEL_VDD_gc; ADC0.MUXPOS = 0x44; uint8_t adc_val = ADC0_read(); From ee6e6a6d0756bbf9f6b11b5bc169f1ddded7a00f Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 15:54:42 +0200 Subject: [PATCH 30/32] Make voltage converter --- prosjekt.X/voltage.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index eec2bd2..c4bf527 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -36,11 +36,18 @@ uint16_t ADC0_read(void) { uint16_t thermistor_voltage_read() { /* Gets value for the diode */ ADC0.MUXPOS = 0x03; - uint8_t adc_val = ADC0_read(); + uint16_t adc_val = ADC0_read(); return adc_val; } +uint16_t external_voltage_read(){ + ADC0.MUXPOS = 0x02; + uint16_t adc_val = ADC0_read(); + + return adc_val; +} + uint16_t internal_voltage_read() { /* Gets value for the internal voltage reffreance*/ @@ -49,3 +56,13 @@ uint16_t internal_voltage_read() { return adc_val*10; } + +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 From 7f3c1fa7c1aa3d31ea5275dcbf16a969f93e4f17 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 15:58:17 +0200 Subject: [PATCH 31/32] Commented the code --- prosjekt.X/voltage.c | 1 + 1 file changed, 1 insertion(+) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index c4bf527..c7ed16e 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -57,6 +57,7 @@ uint16_t internal_voltage_read() { return adc_val*10; } +/* 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; From ca28e23b70411d5cd75c5e03527847b123f3b4b9 Mon Sep 17 00:00:00 2001 From: Elp03 Date: Tue, 9 Apr 2024 16:13:04 +0200 Subject: [PATCH 32/32] Fixed header, comments and Muxpos --- prosjekt.X/voltage.c | 6 +++--- prosjekt.X/voltage.h | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/prosjekt.X/voltage.c b/prosjekt.X/voltage.c index c7ed16e..b06fa06 100644 --- a/prosjekt.X/voltage.c +++ b/prosjekt.X/voltage.c @@ -35,14 +35,14 @@ uint16_t ADC0_read(void) { uint16_t thermistor_voltage_read() { /* Gets value for the diode */ - ADC0.MUXPOS = 0x03; + ADC0.MUXPOS = 0x03; // Read PD3 uint16_t adc_val = ADC0_read(); return adc_val; } - +// Gets the value over thermistor uint16_t external_voltage_read(){ - ADC0.MUXPOS = 0x02; + ADC0.MUXPOS = 0x06; // Read PD6 uint16_t adc_val = ADC0_read(); return adc_val; diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h index af2e76d..4f88758 100644 --- a/prosjekt.X/voltage.h +++ b/prosjekt.X/voltage.h @@ -54,7 +54,13 @@ extern "C" { uint16_t ADC0_read(void); // Gets the value from sensor and internal voltage uint16_t internal_voltage_read(); + // Gets the value over thermistor uint16_t thermistor_voltage_read(); + // Gets internal voltage + uint16_t external_voltage_read(); + // Converts value to voltage + uint16_t convert_to_voltage(uint16_t adc_val); + #ifdef __cplusplus } #endif /* __cplusplus */