2024-03-12 12:37:29 +00:00
|
|
|
#include "voltage.h"
|
2024-03-13 11:13:38 +00:00
|
|
|
#include "uart.h"
|
2024-03-13 10:46:02 +00:00
|
|
|
|
2024-03-12 12:37:29 +00:00
|
|
|
void ADC0_init(void) {
|
2024-04-30 08:59:47 +00:00
|
|
|
/* Initializing ADC0 pin*/
|
2024-04-30 14:17:52 +00:00
|
|
|
/*Voltage reading on pin pd2*/
|
|
|
|
|
PORTD.PIN2CTRL &= ~PORT_ISC_gm;
|
|
|
|
|
PORTD.PIN2CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable */
|
|
|
|
|
PORTD.PIN2CTRL &= PORT_PULLUPEN_bm;
|
2024-04-30 08:59:47 +00:00
|
|
|
|
|
|
|
|
/* 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 */
|
2024-05-03 15:16:29 +00:00
|
|
|
|
2024-04-30 08:59:47 +00:00
|
|
|
/* Select ADC channel */
|
|
|
|
|
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
|
2024-03-12 12:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t ADC0_read(void) {
|
2024-04-30 08:59:47 +00:00
|
|
|
/* 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;
|
2024-03-12 12:37:29 +00:00
|
|
|
}
|
2024-03-13 10:46:02 +00:00
|
|
|
|
2024-04-09 12:36:11 +00:00
|
|
|
uint16_t thermistor_voltage_read() {
|
2024-04-30 08:59:47 +00:00
|
|
|
/* Gets value for the diode */
|
|
|
|
|
ADC0.MUXPOS = 0x03; // Read PD3
|
|
|
|
|
uint16_t adc_val = ADC0_read();
|
2024-03-20 14:45:24 +00:00
|
|
|
|
2024-04-30 08:59:47 +00:00
|
|
|
return adc_val;
|
2024-03-20 14:45:24 +00:00
|
|
|
}
|
2024-04-09 14:13:04 +00:00
|
|
|
// Gets the value over thermistor
|
2024-04-30 08:59:47 +00:00
|
|
|
uint16_t external_voltage_read() {
|
2024-04-30 14:17:52 +00:00
|
|
|
ADC0.MUXPOS = 0x02; // Read PD6
|
2024-04-30 08:59:47 +00:00
|
|
|
uint16_t adc_val = ADC0_read();
|
|
|
|
|
|
|
|
|
|
return adc_val;
|
2024-04-09 13:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-20 14:53:26 +00:00
|
|
|
uint16_t internal_voltage_read() {
|
2024-04-30 08:59:47 +00:00
|
|
|
/* Gets value for the internal voltage reffreance*/
|
|
|
|
|
|
|
|
|
|
ADC0.MUXPOS = 0x44;
|
|
|
|
|
uint8_t adc_val = ADC0_read();
|
2024-04-09 13:54:42 +00:00
|
|
|
|
2024-04-30 08:59:47 +00:00
|
|
|
return adc_val * 10;
|
2024-03-20 15:11:01 +00:00
|
|
|
}
|