You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
1.8 KiB
C

/*
* UART.c
*
* Created: 18/11/2021 17:13:25
* Author: n0x
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "UART.h"
volatile int enabled = 1;
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von
Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stopbit
initialisiert. */
void
uart_init(void)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
/* enable send */
UCSR0B = (1<<TXEN0);
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. */
void
uart_send (char* string){
int len = strlen(string);
for(int i = 0; i < len; i++){
while (!( UCSR0A & (1<<UDRE0)) );
UDR0 = string[i];
}
}
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stoppbit
initialisiert. Nutzen Sie dabei ein Uart-Interrupt. */
void
uart_init_isr(void)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
/* enable send */
UCSR0B |= (1<<TXEN0);
/* enable interrupt */
UCSR0B |= (1<<UDRIE0);
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. (Interrupts) */
void
uart_send_isr(char* string)
{
int i = 0;
while( i < strlen(string)){
if(enabled){
UDR0 = string[i];
i++;
enabled = 0;
}
}
}
ISR(USART0_UDRE_vect){
enabled = 1;
}