-
Notifications
You must be signed in to change notification settings - Fork 0
/
usart.c
95 lines (68 loc) · 1.52 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "usart.h"
#include "tools.h"
#include <stdlib.h>
#include <stdio.h>
void usart_init_roomba() {
// Baudrate
UBRR3L = 0x10;
UBRR3H = 0x00;
// Double speed
UCSR3A |= (1 << U2X3);
// Enable receiver and transmitter
UCSR3B |= (1 << RXEN3) | (1 << TXEN3);
// Set character size
UCSR3B &= ~(0 << UCSZ32);
UCSR3C |= (1 << UCSZ31) | (1 << UCSZ30);
// Set ansychronous
UCSR3C &= ~(0 << UMSEL31) & ~(0 << UMSEL30);
// No parity
UCSR3C &= ~(0 << UPM31) & ~(0 << UPM30);
// 1 Stop bit
UCSR3C &= ~(0 << USBS3);
}
void send_byte_roomba(uint8_t byte) {
while (!(UCSR3A & (1 << UDRE3)));
UDR3 = byte;
}
uint8_t receive_byte_roomba() {
while(!(UCSR3A & (1 << RXC3)));
return UDR3;
}
void usart_init() {
// Baudrate
UBRR0L = 0x10;
UBRR0H = 0x00;
// Double speed
UCSR0A |= (1 << U2X0);
// Enable receiver and transmitter
UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
// Set character size
UCSR0B |= (0 << UCSZ02);
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00);
// Set ansychronous
UCSR0C |= (0 << UMSEL01) | (0 << UMSEL00);
// No parity
UCSR0C |= (0 << UPM01) | (0 << UPM00);
// 1 Stop bit
UCSR0C |= (0 << USBS0);
}
void send_byte(uint8_t byte) {
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = byte;
}
uint8_t receive_byte() {
while(!(UCSR0A & (1 << RXC0)));
return UDR0;
}
void sendString(char * string) {
uint8_t a = 0;
while(string[a] != '\0') {
send_byte(string[a]);
a++;
}
}
void send_value(uint8_t value){
char buffer[10];
sprintf(buffer,"%i",value);
sendString(buffer);
}