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

72 lines
1.6 KiB
C
Raw Normal View History

2024-03-06 13:58:06 +00:00
/*
* File: main.c
* Author: Sebastian H. Gabrielli
*
* Created on March 6, 2024, 12:34 PM
*/
2024-03-06 14:46:56 +00:00
#include "header.h"
2024-03-11 14:48:20 +00:00
#define F_CPU 4000000UL
#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 <stdio.h>
2024-03-06 14:46:56 +00:00
2024-03-11 14:54:52 +00:00
void sensor_init(void) {
/* Disable digital input buffer */
2024-03-06 14:46:56 +00:00
}
2024-03-11 14:54:52 +00:00
void ADC0_init(void) {
2024-03-12 11:49:42 +00:00
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
2024-03-11 14:54:52 +00:00
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;
2024-03-06 14:46:56 +00:00
}
2024-03-11 14:48:20 +00:00
2024-03-11 14:54:52 +00:00
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-06 14:46:56 +00:00
}
2024-03-11 14:54:52 +00:00
bool ADC0_conversationDone(void) {
if (ADC0.INTFLAGS && ADC_RESRDY_bm) {
2024-03-11 14:48:20 +00:00
return true;
2024-03-11 14:54:52 +00:00
} else {
2024-03-11 14:48:20 +00:00
return false;
}
2024-03-11 14:54:52 +00:00
2024-03-06 14:46:56 +00:00
}
2024-03-11 14:54:52 +00:00
int main(void) {
sensor_init();
ADC0_init();
2024-03-12 11:55:57 +00:00
2024-03-11 14:54:52 +00:00
while (1) {
2024-03-11 14:48:20 +00:00
//printf("loop\n");
2024-03-11 14:54:52 +00:00
if (ADC0_conversationDone()) {
adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc;
printf("The values: \n");
2024-03-12 11:49:42 +00:00
printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal);
2024-03-11 14:54:52 +00:00
}
2024-03-11 14:48:20 +00:00
}
}