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); +}