Merge branch 'voltage-reading' into main
This commit is contained in:
commit
1b8ca0c7f6
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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"
|
||||||
|
|||||||
69
prosjekt.X/voltage.c
Normal file
69
prosjekt.X/voltage.c
Normal 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
71
prosjekt.X/voltage.h
Normal 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 */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user