Merge remote-tracking branch 'origin/voltage-reading' into calculate-fanspeed

This commit is contained in:
Inamr 2024-03-20 14:52:28 +01:00
commit 8b5b4e186a
4 changed files with 296 additions and 181 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>
#define F_CPU 4E6 #define F_CPU 4E6
@ -15,11 +24,13 @@
#include "themistor-temp.h" #include "themistor-temp.h"
int main() { int main() {
sensor_init();
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 = voltage_values();
_delay_ms(500); printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal);
} }
} }

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

@ -0,0 +1,40 @@
#include "voltage.h"
#include "uart.h"
void sensor_init(void) {
/* Disable digital input buffer */
}
void ADC0_init(void) {
/* Initializing ADC0 pin*/
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
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;
}
uint16_t voltage_values(void) {
/* Gets values */
uint16_t adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc;
return adcVal;
}

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

@ -0,0 +1,64 @@
/* 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:
* Author:
* 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 sensor pins
void sensor_init(void);
// 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 voltage_values(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */