forked from jkleve/Quadcopter-Homemade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusart.c
137 lines (111 loc) · 3.56 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/********************************************************
Source code .c file Library for send data through USART AVR
Copyright (C) 2014 Juan Lopez Medina
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* You can contact me by mail : [email protected]
*********************************************************/
/************** Intructions ********
To use this library you must initilize in you header project by the function: usart_init();
Then you can to use five functions as follow:
put_float(variable); // Send float data
put_string(" "); // Send character space
put_int(variable); // Send int data
put_string(" ");
put_long(variable); // Send long data
put_string("\n"); // Send newline character
void get_float(void); // Rx float data
void get_int(void); // Rx int data
Also you should define the CPU frequency for your board and the baud
rate in this code and in your Terminal Window of Atmel Studio 6 project.
For further information contact with me please. [email protected]
************************************/
#include "avr/io.h"
#include "stdlib.h"
#include "usart.h"
//#include <stdio.h> // For use with printf and scanf
// Initialize USART
void usart_init(void){
UCSR0A |= (1 << U2X0); // Config BAUDRATE
UBRR0 = F_CPU / (8 * USART_BAUD) - 1;
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // Format 8N1 Asynchronous
UCSR0B = (1 << TXEN0) | (1 << RXEN0); // Enable module TX adn RX
//fdevopen((int (*)(char, FILE*))put_char, (int (*)(FILE*))get_char); // For use with printf and scanf
}
// TX data char through USART
int put_char (int dato){
while ((UCSR0A & (1 << UDRE0)) == 0); // Wait for empty buffer
UDR0 = dato;
return dato;
}
// TX data string ASCII through USART
void put_string(char *s){
while (*s){
put_char(*s);
s++;
}
}
// TX integer variable through USART
void put_int (int dato){
char s[20];
itoa(dato,s,10); // Converting data integer to ASCII
put_string(s);
}
// TX long variable through USART
void put_long (long dato){
char s[20];
ltoa(dato,s,10); // Converting data integer to ASCII
put_string(s);
}
// Tx float variable through USART
void put_float (float dato){
char s[20];
dtostrf(dato,8,3,s); // Converting data integer to ASCII. 3 Decimals
put_string(s);
}
// Rx data char through USART
int get_char(void){
int dato;
while ((UCSR0A & (1<<RXC0)) == 0 ); // Wait for data in buffer
dato = UDR0;
return dato;
}
// Rx string through USART
float get_float(void){
char k[20];
float f;
int i = 0;
while (1) {
k[i] = get_char();
if (k[i] == '\n') break;
i++;
}
f = atof(k);
put_float(f); // Echo print
put_string("\n");
return f;
}
// Rx string through USART
int get_int(void){
char k[20];
int data;
int i = 0;
while (1) {
k[i] = get_char();
if (k[i] == '\n') break;
i++;
}
data = atoi(k); // ASCII to data int
put_int(data); // Echo print
put_string("\n");
return data;
}