forked from arduino-c/uart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
53 lines (38 loc) · 776 Bytes
/
uart.c
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
42
43
44
45
46
47
48
49
50
51
52
#include <avr/io.h>
#define BAUDRATE 9600UL
#define UBRR_VAL (F_CPU / 16 / BAUDRATE - 1)
static void uart_putchar(char c) {
while(!(UCSR0A & _BV(UDRE0)));
UDR0 = c;
}
static char uart_getchar() {
while(!(UCSR0A & _BV(RXC0)));
return UDR0;
}
void uart_init() {
UBRR0L = (unsigned char)(UBRR_VAL & 0xff);
UBRR0H = (unsigned char)(UBRR_VAL >> 0x8);
/* Enable receiver and transmitter */
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
/* Set frame format: 8data, 1stop bit */
/* needed for AVR USART */
UCSR0C = (3 << UCSZ00);
}
int uart_write(char *buf, int len) {
int i;
i = 0;
while(i < len) {
uart_putchar(buf[i]);
++i;
}
return i;
}
int uart_read(char *buf, int len) {
int i;
i = 0;
while(i < len) {
buf[i] = uart_getchar();
++i;
}
return i;
}