root / trunk / code / projects / colonet / utilities / manual_control / manualControlRobot / serial.c @ 13
History | View | Annotate | Download (3.33 KB)
1 |
/*
|
---|---|
2 |
serial.c - Functions for using the RS232 serial port
|
3 |
|
4 |
author: Robotics Club, Colony Project
|
5 |
|
6 |
much code taken from FWR's library, author: Tom Lauwers
|
7 |
|
8 |
|
9 |
general note - serial -> uart
|
10 |
serial1 -> uart1
|
11 |
this is done for clarity purposes
|
12 |
*/
|
13 |
|
14 |
|
15 |
#include <avr/io.h> |
16 |
#include <stdio.h> |
17 |
#include "serial.h" |
18 |
|
19 |
//setup uart0 (serial)
|
20 |
void serial_init(unsigned int ubrr) |
21 |
{ |
22 |
/*Set baud rate - baud rates under 4800bps unsupported */
|
23 |
UBRR0H = 0x00;
|
24 |
UBRR0L = (unsigned char)ubrr; |
25 |
|
26 |
/*Enable receiver and transmitter */
|
27 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
28 |
|
29 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
30 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
31 |
} |
32 |
|
33 |
// uart_putchar - Low-level function which puts a value into the
|
34 |
// Tx buffer for transmission. Automatically injects
|
35 |
// a \r before all \n's
|
36 |
int serial_putchar(char c) |
37 |
{ |
38 |
|
39 |
if (c == '\n') |
40 |
putchar('\r');
|
41 |
// Wait until buffer is clear for sending
|
42 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
43 |
// Load buffer with your character
|
44 |
UDR0 = c; |
45 |
return 0; |
46 |
} |
47 |
|
48 |
// uart_getchar - Low-level function which waits for the Rx buffer
|
49 |
// to be filled, and then reads one character out of it.
|
50 |
// Note that this function blocks on read - it will wait until
|
51 |
// something fills the Rx buffer.
|
52 |
int serial_getchar(void) |
53 |
{ |
54 |
char c;
|
55 |
// Wait for the receive buffer to be filled
|
56 |
loop_until_bit_is_set(UCSR0A, RXC0); |
57 |
|
58 |
// Read the receive buffer
|
59 |
c = UDR0; |
60 |
return c;
|
61 |
} |
62 |
|
63 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer
|
64 |
// is filled, and then reads one character out of it.
|
65 |
// This is a non blocking version uart_getchar
|
66 |
int serial_getchar_nb(void) |
67 |
{ |
68 |
char c;
|
69 |
// Wait for the receive buffer to be filled
|
70 |
//loop_until_bit_is_set(UCSR0A, RXC0);
|
71 |
if (UCSR0A & _BV(RXC0)){
|
72 |
// Read the receive buffer
|
73 |
c = UDR0; |
74 |
return c;
|
75 |
} |
76 |
return 0; |
77 |
|
78 |
} |
79 |
|
80 |
//setup uart1 (serial1)
|
81 |
void serial1_init( unsigned int ubrr) |
82 |
{ |
83 |
/*Set baud rate - baud rates under 4800bps unsupported */
|
84 |
UBRR1H = 0x00;
|
85 |
UBRR1L = (unsigned char)ubrr; |
86 |
|
87 |
/*Enable receiver and transmitter */
|
88 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
89 |
|
90 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
91 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
92 |
|
93 |
/* For use with fprintf() and related functions */
|
94 |
serial1_stream = fdevopen(serial1_putchar, serial1_putchar); |
95 |
|
96 |
/* To use fprintf():
|
97 |
After calling serial1_init(BAUD115200);
|
98 |
use the command fprintf(serial1_stream, "Hello World");
|
99 |
This call to fdevopen() does give a compile warning
|
100 |
but works fine.
|
101 |
-Greg Tress
|
102 |
*/
|
103 |
} |
104 |
|
105 |
//put to uart1 (serial1)
|
106 |
int serial1_putchar(char c) |
107 |
{ |
108 |
|
109 |
if (c == '\n') |
110 |
serial1_putchar('\r'); //changed from putchar aj |
111 |
// Wait until buffer is clear for sending
|
112 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
113 |
// Load buffer with your character
|
114 |
UDR1 = c; |
115 |
return 0; |
116 |
} |
117 |
|
118 |
//get from uart1 (serial1)
|
119 |
int serial1_getchar(void) |
120 |
{ |
121 |
char c;
|
122 |
// Wait for the receive buffer to be filled
|
123 |
loop_until_bit_is_set(UCSR1A, RXC1); |
124 |
|
125 |
// Read the receive buffer
|
126 |
c = UDR1; |
127 |
return c;
|
128 |
} |
129 |
|
130 |
//get from uart1 non-block
|
131 |
int serial1_getchar_nb(void) |
132 |
{ |
133 |
char c;
|
134 |
// Wait for the receive buffer to be filled
|
135 |
//loop_until_bit_is_set(UCSR1A, RXC1);
|
136 |
if (UCSR1A & _BV(RXC1)){
|
137 |
// Read the receive buffer
|
138 |
c = UDR1; |
139 |
return c;
|
140 |
} |
141 |
return 0; |
142 |
|
143 |
} |