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

85 lines
1.8 KiB
C
Raw Normal View History

2024-03-06 11:34:22 +00:00
/*
* File: main.c
* Author: Sebastian H. Gabrielli
*
* Created on March 6, 2024, 12:34 PM
*/
#include "header.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>
2024-03-06 11:34:22 +00:00
2024-03-06 14:36:24 +00:00
#define F_CPU 4E6
2024-03-06 11:34:22 +00:00
#include <stdio.h>
#include <stdlib.h>
2024-03-06 14:36:24 +00:00
#include "uart.h"
#include <util/delay.h>
2024-03-06 11:34:22 +00:00
void sensor_init(void) {
/* Disable digital input buffer */
2024-03-06 14:36:24 +00:00
while (1) {
printf("Hello, world!\n");
_delay_ms(500);
}
}
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;
}
bool ADC0_conversationDone(void) {
if (ADC0.INTFLAGS && ADC_RESRDY_bm) {
return true;
} else {
return false;
}
}
2024-03-12 12:13:06 +00:00
int main() {
sensor_init();
ADC0_init();
2024-03-12 12:13:06 +00:00
init_uart((uint16_t)9600);
stdout = &USART_stream;
while (1) {
//printf("loop\n");
if (ADC0_conversationDone()) {
adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc;
printf("The values: \n");
printf("%u , %u",VREF_REFSEL_VDD_gc , adcVal);
2024-03-12 12:13:06 +00:00
} else {
printf("Not done\n");
}
}
2024-03-11 14:48:20 +00:00
}