mikrokontrollersystemer-pro.../prosjekt.X/header.h
2024-03-06 15:22:40 +01:00

125 lines
3.3 KiB
C

/* Microchip Technology Inc. and its subsidiaries. You may use this software
* and any derivatives exclusively with Microchip products.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
* TERMS.
*/
/*
* File:
* Author:
* Comments:
* Revision history:
*/
#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>
#include <stdio.h>
// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h> // include processor files - each processor file is guarded.
uint16_t adcVal;
void led_init(void)
{
/* Disable digital input buffer */
PORTC.PIN6CTRL &= ~PORT_ISC_gm;
PORTC.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTC.PIN6CTRL &= ~PORT_PULLUPEN_bm;
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 */
USART3.BAUD = USART3_BAUD_RATE (9600); /* Setting the baudrate */
}
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_conersionDone(void)
{
return (ADC0.INTFLAGS & ADC_RESRDY_bm);
}