-
Notifications
You must be signed in to change notification settings - Fork 19
/
usart.c
74 lines (59 loc) · 1.32 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
66
67
68
69
70
71
72
73
74
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdarg.h>
#include "usart.h"
#define UART_RXBUFSIZE 64
volatile static uint8_t rxbuf[UART_RXBUFSIZE];
volatile static uint8_t *volatile rxhead, *volatile rxtail;
uint8_t uart_getc_nb(uint8_t *c)
{
if (rxhead==rxtail) return 0;
*c = *rxtail;
if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf;
return 1;
}
ISR (USART_RX_vect)
{
int diff;
uint8_t c;
c=UDR;
diff = rxhead - rxtail;
if (diff < 0) diff += UART_RXBUFSIZE;
if (diff < UART_RXBUFSIZE -1)
{
*rxhead = c;
++rxhead;
if (rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf;
};
}
//*****************************************************************************
//
void USART_Init (void)
{
// set clock divider
#undef BAUD
#define BAUD USART_BAUD
#include <util/setbaud.h>
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
#if USE_2X
UCSRA |= (1 << U2X); // enable double speed operation
#else
UCSRA &= ~(1 << U2X); // disable double speed operation
#endif
// set 8E2
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
UCSRB &= ~(1 << UCSZ2);
UCSRC |= (1 << UPM1);
UCSRC |= (1 << USBS);
// flush receive buffer
while ( UCSRA & (1 << RXC) ) UDR;
UCSRB |= (1 << RXEN) | (1 << TXEN);
UCSRB |= (1 << RXCIE);
rxhead = rxtail = rxbuf;
}
void USART_putc (char c)
{
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
}