-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusart.c
65 lines (43 loc) · 1.13 KB
/
usart.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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include "usart.h"
void usart_init (void)
{
// set baud rate to 19.2 kbps based on 16-MHz core clock
UBRRH = 0x00;
UBRRL = 0x33;
// enable receiver interrupt, receiver and transmitter
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);
// configure 8-bit character size
UCSRC |= (3 << UCSZ0);
}
void usart_print_char (char print_char)
{
// wait until transmit buffer is free
while (!(UCSRA & (1 << UDRE)));
// send character
UDR = print_char;
}
void usart_print_str (char *print_str)
{
uint8_t i;
i = 0;
// print each character
while (print_str[i]) usart_print_char (print_str[i++]);
}
void usart_print_str_P (char *print_str)
{
uint8_t i;
i = 0;
// print each character (from program memory)
while (pgm_read_byte (&print_str[i])) usart_print_char (pgm_read_byte (&print_str[i++]));
}
ISR (USART_RX_vect)
{
// queue character and increment write pointer
usart_rx_buffer[usart_rx_wptr++] = UDR;
// wrap write pointer
usart_rx_wptr %= USART_RX_SIZE;
}