diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 8e516c4..364a28b 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,6 +4,15 @@ * * 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 #include "eeprom.h" @@ -14,11 +23,12 @@ #include int main() { + ADC0_init(); init_uart((uint16_t)9600); stdout = &USART_stream; while (1) { - printf("Hello, world!\n"); - _delay_ms(500); + 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 8e42839..3b13f57 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -6,6 +6,7 @@ projectFiles="true"> uart.h eeprom.h + voltage.h main.c uart.c eeprom.c + voltage.c - - 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 new file mode 100644 index 0000000..b06fa06 --- /dev/null +++ b/prosjekt.X/voltage.c @@ -0,0 +1,69 @@ +#include "voltage.h" +#include "uart.h" + +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; + + /* Thermistor */ + PORTD.PIN3CTRL &= ~PORT_ISC_gm; + PORTD.PIN3CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */ + PORTD.PIN3CTRL &= 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; +} + +uint16_t thermistor_voltage_read() { + /* Gets value for the diode */ + 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 = 0x06; // Read PD6 + uint16_t adc_val = ADC0_read(); + + return adc_val; +} + +uint16_t internal_voltage_read() { + /* Gets value for the internal voltage reffreance*/ + + ADC0.MUXPOS = 0x44; + uint8_t adc_val = ADC0_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; + 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 diff --git a/prosjekt.X/voltage.h b/prosjekt.X/voltage.h new file mode 100644 index 0000000..4f88758 --- /dev/null +++ b/prosjekt.X/voltage.h @@ -0,0 +1,71 @@ +/* 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: Voltage.h + * Author: Sebastian Hernø Gabrielli, Helle Augland Grasmo, Ina Min Rørnes + * Comments: + * Revision history: + */ + +#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 +#define XC_HEADER_TEMPLATE_H + +#include // include processor files - each processor file is guarded. + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + // 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(); + // 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 */ + +#endif /* XC_HEADER_TEMPLATE_H */ + + +