root / branches / charging_station / code / projects / recharging / charging_station / serial.c @ 85
History | View | Annotate | Download (4.15 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 xbee_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.
|
35 |
int xbee_putc(char c) |
36 |
{ |
37 |
// Wait until buffer is clear for sending
|
38 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
39 |
// Load buffer with your character
|
40 |
UDR0 = c; |
41 |
return 0; |
42 |
} |
43 |
|
44 |
int xbee_puts(char *s) |
45 |
{ |
46 |
char *t = s;
|
47 |
while (*t != 0) |
48 |
{ |
49 |
xbee_putc(*t); |
50 |
t++; |
51 |
} |
52 |
return 0; |
53 |
} |
54 |
|
55 |
int xbee_puti(int value ) { |
56 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
57 |
|
58 |
/* convert int to ascii */
|
59 |
if(value<0) { xbee_putc('-'); value=-value; } |
60 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
61 |
|
62 |
|
63 |
/* start displaying the number */
|
64 |
for(;position<=(sizeof(usb_data)-1);position++) |
65 |
{ |
66 |
|
67 |
xbee_putc(usb_data[position]); |
68 |
} |
69 |
|
70 |
return 0; |
71 |
} |
72 |
|
73 |
// uart_getchar - Low-level function which waits for the Rx buffer
|
74 |
// to be filled, and then reads one character out of it.
|
75 |
// Note that this function blocks on read - it will wait until
|
76 |
// something fills the Rx buffer.
|
77 |
int xbee_getc(void) |
78 |
{ |
79 |
char c;
|
80 |
// Wait for the receive buffer to be filled
|
81 |
loop_until_bit_is_set(UCSR0A, RXC0); |
82 |
|
83 |
// Read the receive buffer
|
84 |
c = UDR0; |
85 |
return c;
|
86 |
} |
87 |
|
88 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer
|
89 |
// is filled, and then reads one character out of it.
|
90 |
// This is a non blocking version uart_getchar
|
91 |
int xbee_getc_nb(void) |
92 |
{ |
93 |
char c;
|
94 |
// Wait for the receive buffer to be filled
|
95 |
//loop_until_bit_is_set(UCSR0A, RXC0);
|
96 |
if (UCSR0A & _BV(RXC0)){
|
97 |
// Read the receive buffer
|
98 |
c = UDR0; |
99 |
return c;
|
100 |
} |
101 |
return 0; |
102 |
|
103 |
} |
104 |
|
105 |
//setup uart1 (serial1)
|
106 |
void usb_init( unsigned int ubrr) |
107 |
{ |
108 |
/*Set baud rate - baud rates under 4800bps unsupported */
|
109 |
UBRR1H = 0x00;
|
110 |
UBRR1L = (unsigned char)ubrr; |
111 |
|
112 |
/*Enable receiver and transmitter */
|
113 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
114 |
|
115 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
116 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
117 |
} |
118 |
|
119 |
//put to uart1 (serial1)
|
120 |
int usb_putc(char c) |
121 |
{ |
122 |
// Wait until buffer is clear for sending
|
123 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
124 |
// Load buffer with your character
|
125 |
UDR1 = c; |
126 |
return 0; |
127 |
} |
128 |
|
129 |
int usb_puts(char *s) |
130 |
{ |
131 |
char *t = s;
|
132 |
while (*t != 0) |
133 |
{ |
134 |
usb_putc(*t); |
135 |
t++; |
136 |
} |
137 |
return 0; |
138 |
} |
139 |
|
140 |
int usb_puti(int value ) { |
141 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
142 |
|
143 |
/* convert int to ascii */
|
144 |
if(value<0) { usb_putc('-'); value=-value; } |
145 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
146 |
|
147 |
|
148 |
/* start displaying the number */
|
149 |
for(;position<=(sizeof(usb_data)-1);position++) |
150 |
{ |
151 |
|
152 |
usb_putc(usb_data[position]); |
153 |
} |
154 |
|
155 |
return 0; |
156 |
} |
157 |
|
158 |
//get from uart1 (serial1)
|
159 |
int usb_getc(void) |
160 |
{ |
161 |
char c;
|
162 |
// Wait for the receive buffer to be filled
|
163 |
loop_until_bit_is_set(UCSR1A, RXC1); |
164 |
|
165 |
// Read the receive buffer
|
166 |
c = UDR1; |
167 |
return c;
|
168 |
} |
169 |
|
170 |
//get from uart1 non-block
|
171 |
int usb_getc_nb(void) |
172 |
{ |
173 |
char c;
|
174 |
// Wait for the receive buffer to be filled
|
175 |
//loop_until_bit_is_set(UCSR1A, RXC1);
|
176 |
if (UCSR1A & _BV(RXC1)){
|
177 |
// Read the receive buffer
|
178 |
c = UDR1; |
179 |
return c;
|
180 |
} |
181 |
return 0; |
182 |
|
183 |
} |
184 |
|