2024-03-06 11:34:22 +00:00
|
|
|
/*
|
|
|
|
|
* File: main.c
|
|
|
|
|
* Author: Sebastian H. Gabrielli
|
|
|
|
|
*
|
|
|
|
|
* Created on March 6, 2024, 12:34 PM
|
|
|
|
|
*/
|
2024-03-12 11:57:20 +00:00
|
|
|
#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
|
|
|
|
2024-03-06 14:36:24 +00:00
|
|
|
int main() {
|
|
|
|
|
init_uart((uint16_t)9600);
|
|
|
|
|
stdout = &USART_stream;
|
2024-03-06 11:34:22 +00:00
|
|
|
|
2024-03-12 11:57:20 +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);
|
|
|
|
|
}
|
2024-03-12 11:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
|
sensor_init();
|
|
|
|
|
ADC0_init();
|
|
|
|
|
|
|
|
|
|
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-11 14:48:20 +00:00
|
|
|
}
|