-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
65 lines (57 loc) · 995 Bytes
/
serial.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
/*
* FumenBot v0.71
* http://fumenbot.sourceforge.net/
*
* Andres Basile GPLv3
* http://www.gnu.org/licenses/gpl-3.0.en.html
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "serial.h"
void serial_setup()
{
UBRR0H = (BAUD_PRESCALLER>>8);
UBRR0L = BAUD_PRESCALLER;
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
SERIAL_INDEX = 0;
SERIAL_READY = 0;
}
ISR(USART_RX_vect)
{
SERIAL_CHAR = UDR0;
if(SERIAL_CHAR == '\r' || SERIAL_CHAR == '\n') {
SERIAL_READY = 1;
}
else {
SERIAL_BUFFER[SERIAL_INDEX] = SERIAL_CHAR;
SERIAL_INDEX++;
}
UDR0 = SERIAL_CHAR;
}
char serial_get(void)
{
while(!(UCSR0A & (1<<RXC0)));
return UDR0;
}
void serial_put(char c)
{
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = c;
}
void serial_puts(char* c)
{
while(*c != 0x00) {
serial_put(*c);
c++;
}
}
void serial_flush()
{
SERIAL_INDEX = 0;
SERIAL_READY = 0;
uint8_t i;
for(i=0; i<SERIAL_BUFFER_SIZE; i++) {
SERIAL_BUFFER[i] = '\0';
}
}