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

119 lines
2.2 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-06 14:16:01 +00:00
#define F_CPU 4000000UL
#define RTC_PERIOD (511)
#define DELAY_TIME 1000
#define USART3_BAUD_RATE(BAUD_RATE) ((float) (64*F_CPU /(16*(float)BAUD_RATE) )+0.5)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdbool.h>
2024-03-06 13:58:06 +00:00
#include <stdio.h>
2024-03-06 14:46:56 +00:00
2024-03-06 14:16:01 +00:00
uint16_t adcVal;
2024-03-06 14:46:56 +00:00
void sensor_init(void)
{
PORTE.DIRSET = PIN0_bm;
PORTE.DIRSET = PIN1_bm;
PORTE.DIRSET = PIN2_bm;
}
void USART3_init(void)
{
PORTB.DIRCLR &= ~PIN1_bm;
PORTB.DIRSET |= PIN0_bm;
PORTB.DIRCLR = PIN2_bm;
PORTB.PIN2CTRL |= PORT_PULLUPEN_bm;
USART3.CTRLB |= USART_RXEN_bm | USART_TXEN_bm; /* Enable both TX and
RX */
}
void ADC0_init(void)
{
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;
}
void USART3_sendChar(char c)
{
while (!(USART3.STATUS & USART_DREIF_bm))
{
;
}
USART3.TXDATAL = c;
}
void USART3_sendString(char *str)
{
for(size_t i = 0; i < strlen(str); i++)
{
USART3_sendChar(str[i]);
}
}
static int USART3_printChar(char c, FILE *stream)
{
USART3_sendChar(c);
return 0;
}
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_conversionDone(void)
{
return (ADC0.INTFLAGS & ADC_RESRDY_bm);
}
2024-03-06 13:58:06 +00:00
2024-03-06 14:16:01 +00:00
static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);
2024-03-06 13:58:06 +00:00
2024-03-06 14:16:01 +00:00
int teller = 100;
int main(void)
2024-03-06 14:46:56 +00:00
{ sensor_init();
2024-03-06 14:16:01 +00:00
ADC0_init();
USART3_init();
stdout = &USART_stream;
while (1)
2024-03-06 14:46:56 +00:00
{
if (ADC0_conversionDone())
2024-03-06 14:16:01 +00:00
{
adcVal = ADC0_read();
printf("%d \n",adcVal);
}
}
}
/*char out_str[30] = {0};
float flt_num = adcVal;
sprintf(out_str, "flt_num = %f\r\n", flt_num);
UartTxStr(out_str); */
2024-03-06 13:58:06 +00:00
}