-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.c
More file actions
42 lines (34 loc) · 727 Bytes
/
serial.c
File metadata and controls
42 lines (34 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <avr/io.h>
#include <stdio.h>
#include "serial.h"
static FILE uart_stdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void uart_init(void) {
UBRR0H = (unsigned char)(UBRR >> 8);
UBRR0L = (unsigned char)(UBRR);
UCSR0A = 0;
UCSR0B = (1 << TXEN0 | 1 << RXEN0);
UCSR0C = (1 << UCSZ01 | 1 << UCSZ00);
stdout = &uart_stdout;
}
int uart_putchar(char chr, FILE *stream) {
if (chr == '\n') {
uart_putchar('\r', NULL);
}
while (!(UCSR0A & (1 << UDRE0)))
;
UDR0 = chr;
return 0;
}
void uart_putchars(char chr) {
if (chr == '\n') {
uart_putchars('\r');
}
while (!(UCSR0A & (1 << UDRE0)))
;
UDR0 = chr;
}
char uart_getchar(void) {
while (!(UCSR0A & (1 << RXC0)))
;
return UDR0;
}