1
0
mirror of synced 2024-11-26 19:24:24 +01:00
avr_demo/hello_uart/uart.c

35 lines
806 B
C
Raw Normal View History

2011-10-05 21:03:53 +02:00
#include <avr/io.h>
#include <stdio.h>
#ifndef BAUD
#define BAUD 9600
#endif
#include <util/setbaud.h>
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
2015-02-19 09:22:51 +01:00
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
2011-10-05 21:03:53 +02:00
void uart_init(void) {
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
2011-10-09 00:43:30 +02:00
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
2011-10-05 21:03:53 +02:00
}
int uart_putchar(char c, FILE *stream) {
if (c == '\n') {
uart_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
int uart_getchar(FILE *stream) {
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}