Merge branch 'voltage-reading' into main

This commit is contained in:
Elp03 2024-04-09 16:20:46 +02:00
commit 1b8ca0c7f6
5 changed files with 177 additions and 25 deletions

View File

@ -4,6 +4,15 @@
* *
* Created on March 6, 2024, 12:34 PM * Created on March 6, 2024, 12:34 PM
*/ */
#include "voltage.h"
#include "uart.h"
#define RTC_PERIOD (511)
#define DELAY_TIME 1000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdbool.h>
#include "eeprom.h" #include "eeprom.h"
@ -14,11 +23,12 @@
#include <util/delay.h> #include <util/delay.h>
int main() { int main() {
ADC0_init();
init_uart((uint16_t)9600); init_uart((uint16_t)9600);
stdout = &USART_stream; stdout = &USART_stream;
while (1) { while (1) {
printf("Hello, world!\n"); uint16_t adcVal = ADC0_read();
_delay_ms(500); printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal);
} }
} }

View File

@ -6,6 +6,7 @@
projectFiles="true"> projectFiles="true">
<itemPath>uart.h</itemPath> <itemPath>uart.h</itemPath>
<itemPath>eeprom.h</itemPath> <itemPath>eeprom.h</itemPath>
<itemPath>voltage.h</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="LinkerScript" <logicalFolder name="LinkerScript"
displayName="Linker Files" displayName="Linker Files"
@ -17,6 +18,7 @@
<itemPath>main.c</itemPath> <itemPath>main.c</itemPath>
<itemPath>uart.c</itemPath> <itemPath>uart.c</itemPath>
<itemPath>eeprom.c</itemPath> <itemPath>eeprom.c</itemPath>
<itemPath>voltage.c</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="ExternalFiles" <logicalFolder name="ExternalFiles"
displayName="Important Files" displayName="Important Files"

View File

@ -1,23 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1"> <project xmlns="http://www.netbeans.org/ns/project/1">
<type>com.microchip.mplab.nbide.embedded.makeproject</type> <type>com.microchip.mplab.nbide.embedded.makeproject</type>
<configuration> <configuration>
<data xmlns="http://www.netbeans.org/ns/make-project/1"> <data xmlns="http://www.netbeans.org/ns/make-project/1">
<name>prosjekt</name> <name>prosjekt</name>
<creation-uuid>928a6534-8606-496e-8a74-8a6544316339</creation-uuid> <creation-uuid>928a6534-8606-496e-8a74-8a6544316339</creation-uuid>
<make-project-type>0</make-project-type> <make-project-type>0</make-project-type>
<sourceEncoding>ISO-8859-1</sourceEncoding> <sourceEncoding>ISO-8859-1</sourceEncoding>
<make-dep-projects/> <make-dep-projects/>
<sourceRootList/> <sourceRootList/>
<confList> <confList>
<confElem> <confElem>
<name>default</name> <name>default</name>
<type>2</type> <type>2</type>
</confElem> </confElem>
</confList> </confList>
<formatting> <formatting>
<project-formatting-style>false</project-formatting-style> <project-formatting-style>false</project-formatting-style>
</formatting> </formatting>
</data> </data>
</configuration> </configuration>
</project> </project>

69
prosjekt.X/voltage.c Normal file
View File

@ -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;
}

71
prosjekt.X/voltage.h Normal file
View File

@ -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 <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
// 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 <xc.h> // 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 */