diff --git a/.gitignore b/.gitignore index d14efc5..f31b37b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ # Large Files *.exe *.zip -*.pdf \ No newline at end of file +*.pdf +/prosjekt.X/build/ +/prosjekt.X/dist/ \ No newline at end of file diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 177c5e4..3157197 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -1,72 +1,84 @@ -/* - * File: main.c - * Author: Sebastian H. Gabrielli - * - * Created on March 6, 2024, 12:34 PM - */ -#include "header.h" -#define F_CPU 4000000UL -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#include -#include -#include -#include -#include -#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(void) { - sensor_init(); - ADC0_init(); - - 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); - } - } +/* + * File: main.c + * Author: Sebastian H. Gabrielli + * + * Created on March 6, 2024, 12:34 PM + */ +#include "header.h" +#define RTC_PERIOD (511) +#define DELAY_TIME 1000 +#include +#include +#include +#include +#include + +#define F_CPU 4E6 + +#include +#include +#include "uart.h" +#include + +int main() { + init_uart((uint16_t)9600); + stdout = &USART_stream; + +void sensor_init(void) { + /* Disable digital input buffer */ + + while (1) { + printf("Hello, world!\n"); + _delay_ms(500); + } +} + +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(void) { + sensor_init(); + ADC0_init(); + + 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); + } + } } \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 8857de3..9fa5b00 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -1,171 +1,173 @@ - - - - - header.h - - - Makefile - - - - - main.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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/prosjekt.X/uart.c b/prosjekt.X/uart.c new file mode 100644 index 0000000..67648d1 --- /dev/null +++ b/prosjekt.X/uart.c @@ -0,0 +1,25 @@ + +#include "uart.h" + +void init_uart(uint16_t baud) { + // Configure UART pin directions + PORTB.DIR &= ~PIN1_bm; + PORTB.DIR |= PIN0_bm; + // Set the baudrate + USART3.BAUD = (uint16_t)USART3_BAUD_RATE(baud); + // Enable UART TX & RX + USART3.CTRLB |= USART_TXEN_bm; +} + +void USART3_sendChar(char c) { + // Hold the code while the UART is not ready to send + while (!(USART3.STATUS & USART_DREIF_bm)) { ; } + // UART is ready, send the character. + USART3.TXDATAL = c; + +} + +int USART3_printChar(char c, FILE *stream) { + USART3_sendChar(c); + return 0; +} \ No newline at end of file diff --git a/prosjekt.X/uart.h b/prosjekt.X/uart.h new file mode 100644 index 0000000..7284552 --- /dev/null +++ b/prosjekt.X/uart.h @@ -0,0 +1,40 @@ +/* + * File: uart.h + * Author: Sebastian H. Gabrielli + * + * Created on March 6, 2024, 3:19 PM + */ + +#ifndef UART_H +#define UART_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifndef F_CPU + #define F_CPU 4E6 +#endif + +#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5) + +// Initialize the USART3 controller +void init_uart(uint16_t baud); + +// Send a single character over UART +void USART3_sendChar(char c); + +// Send a string of characters over UART +int USART3_printChar(char c, FILE *stream); + +static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); + +#ifdef __cplusplus +} +#endif + +#endif /* UART_H */ +