sysprog-basic/include/uart.c

50 lines
1.1 KiB
C

#include <stdlib.h>
#include "uart.h"
void uart_init() {
/* This make stuff not work, idk
// set baud rate
UBRR0H = (MYUBRR>>8);
UBRR0L = MYUBRR;
*/
/* Enable receiver and transmitter */
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C |= (1<<UCSZ01)|(3<<UCSZ00);
//Lokales interrupt enable
UCSR0B |= (1 << RXCIE0);
}
void uart_printc(char c) {
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
{
// wartet nur
}
/* Put data into buffer, sends the data */
UDR0 = c;
}
void uart_printsnl(char* s) {
while(*s)
uart_printc(*s++);
uart_printc('\n');
}
void uart_prints(char* s) {
while(*s)
uart_printc(*s++);
}
void uart_printi(char* pre, int i) {
// we want to print this,
// so we store the ascii repr in our buffer
char buf[10];
itoa(i, buf, 10); // base 10
uart_prints(pre);
uart_printsnl(buf);
}
char uart_getc() {
while (UCSR0A & (1 << RXC0));
return UDR0;
}