mikrokontrollersystemer-pro.../prosjekt.X/voltage.c

36 lines
981 B
C
Raw Normal View History

2024-03-12 12:37:29 +00:00
#include "voltage.h"
2024-03-13 10:46:02 +00:00
2024-03-12 12:37:29 +00:00
void sensor_init(void) {
/* Disable digital input buffer */
}
void ADC0_init(void) {
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
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;
}
2024-03-13 10:46:02 +00:00
uint8_t voltage_values(void) {
uint8_t adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc;
return adcVal;
}