Revision 437
Cleaned up analog.c code. Recompiled library. Mostly commenting and
documentation fixes. No changes made to actual code.
serial.c | ||
---|---|---|
49 | 49 |
//Set baud rate |
50 | 50 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
51 | 51 |
// - 9600 is U2X =1, UBRR = 107. |
52 |
#if (USB_BAUD == 115200)
|
|
53 |
UBRR0H = 0x00;
|
|
54 |
UBRR0L = 8;
|
|
55 |
UCSR0A |= _BV(U2X0);
|
|
56 |
#elif (USB_BAUD == 9600)
|
|
57 |
UBRR0H = 0x00;
|
|
58 |
UBRR0L = 103;
|
|
59 |
UCSR0A |= _BV(U2X0);
|
|
60 |
#else //Baud rate is defined in the header file, we should not get here
|
|
61 |
return;
|
|
62 |
#endif
|
|
52 |
#if (USB_BAUD == 115200) |
|
53 |
UBRR0H = 0x00; |
|
54 |
UBRR0L = 8; |
|
55 |
UCSR0A |= _BV(U2X0); |
|
56 |
#elif (USB_BAUD == 9600) |
|
57 |
UBRR0H = 0x00; |
|
58 |
UBRR0L = 103; |
|
59 |
UCSR0A |= _BV(U2X0); |
|
60 |
#else //Baud rate is defined in the header file, we should not get here |
|
61 |
return; |
|
62 |
#endif |
|
63 | 63 |
|
64 | 64 |
/*Enable receiver and transmitter */ |
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
65 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
|
|
66 | 66 |
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
67 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
|
68 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
|
|
69 | 69 |
|
70 | 70 |
// if we have enabled the stdio stuff, then we init it here |
71 |
#ifdef USE_STDIO
|
|
72 |
/* Open the stdio stream corresponding to this port */
|
|
73 |
usb_fd = fdevopen(usb_putc, usb_getc);
|
|
74 |
#endif
|
|
71 |
#ifdef USE_STDIO |
|
72 |
/* Open the stdio stream corresponding to this port */ |
|
73 |
usb_fd = fdevopen(usb_putc, usb_getc); |
|
74 |
#endif |
|
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
/** |
... | ... | |
83 | 83 |
//Set baud rate |
84 | 84 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
85 | 85 |
// - 9600 is U2X =1, UBRR = 107. |
86 |
#if (XBEE_BAUD == 115200)
|
|
87 |
UBRR1H = 0x00;
|
|
88 |
UBRR1L = 8;
|
|
89 |
UCSR1A |= _BV(U2X1);
|
|
90 |
#elif (XBEE_BAUD == 9600)
|
|
91 |
UBRR1H = 0x00;
|
|
92 |
UBRR1L = 103;
|
|
93 |
UCSR1A |= _BV(U2X1);
|
|
94 |
#else //Baud rate is defined in the header file, we should not get here
|
|
95 |
return;
|
|
96 |
#endif
|
|
86 |
#if (XBEE_BAUD == 115200) |
|
87 |
UBRR1H = 0x00; |
|
88 |
UBRR1L = 8; |
|
89 |
UCSR1A |= _BV(U2X1); |
|
90 |
#elif (XBEE_BAUD == 9600) |
|
91 |
UBRR1H = 0x00; |
|
92 |
UBRR1L = 103; |
|
93 |
UCSR1A |= _BV(U2X1); |
|
94 |
#else //Baud rate is defined in the header file, we should not get here |
|
95 |
return; |
|
96 |
#endif |
|
97 | 97 |
|
98 |
//Enable receiver and transmitter
|
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
98 |
//Enable receiver and transmitter
|
|
99 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
|
|
100 | 100 |
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
101 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
102 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
103 | 103 |
|
104 | 104 |
// if we have enabled the stdio stuff, then we init it here |
105 |
#ifdef USE_STDIO
|
|
106 |
/* Open the stdio stream corresponding to this port */
|
|
107 |
xbee_fd = fdevopen(xbee_putc, xbee_getc);
|
|
108 |
#endif
|
|
105 |
#ifdef USE_STDIO |
|
106 |
/* Open the stdio stream corresponding to this port */ |
|
107 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
|
108 |
#endif |
|
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/** |
... | ... | |
114 | 114 |
* @param c the character to send |
115 | 115 |
* @return 0 for success, nonzero for failure |
116 | 116 |
**/ |
117 |
int usb_putc(char c) |
|
118 |
{ |
|
117 |
int usb_putc(char c) { |
|
119 | 118 |
// Wait until buffer is clear for sending |
120 | 119 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
121 | 120 |
|
... | ... | |
130 | 129 |
* @param c the character to send |
131 | 130 |
* @return 0 for success, nonzero for failure |
132 | 131 |
**/ |
133 |
int xbee_putc(char c) |
|
134 |
{ |
|
132 |
int xbee_putc(char c) { |
|
135 | 133 |
// Wait until buffer is clear for sending |
136 | 134 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
137 | 135 |
|
... | ... | |
146 | 144 |
* @param s the string to send |
147 | 145 |
* @return 0 for success, nonzero for failure |
148 | 146 |
**/ |
149 |
int usb_puts(char *s) |
|
150 |
{ |
|
151 |
char *t = s; |
|
152 |
while (*t != 0) |
|
153 |
{ |
|
154 |
usb_putc(*t); |
|
155 |
t++; |
|
156 |
} |
|
147 |
int usb_puts(char *s) { |
|
148 |
char *t = s; |
|
149 |
while (*t != 0) { |
|
150 |
usb_putc(*t); |
|
151 |
t++; |
|
152 |
} |
|
157 | 153 |
return 0; |
158 | 154 |
} |
159 | 155 |
|
... | ... | |
166 | 162 |
* |
167 | 163 |
* @see usb_init, usb_getc_nb |
168 | 164 |
**/ |
169 |
int usb_getc(void) |
|
170 |
{ |
|
171 |
// Wait for the receive buffer to be filled |
|
172 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
165 |
int usb_getc(void) { |
|
166 |
// Wait for the receive buffer to be filled |
|
167 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
173 | 168 |
|
174 |
// Read the receive buffer
|
|
175 |
return UDR0;
|
|
169 |
// Read the receive buffer
|
|
170 |
return UDR0;
|
|
176 | 171 |
} |
177 | 172 |
|
178 | 173 |
/** |
... | ... | |
185 | 180 |
* |
186 | 181 |
* @see xbee_init, xbee_getc_nb |
187 | 182 |
**/ |
188 |
int xbee_getc(void) |
|
189 |
{ |
|
190 |
// Wait for the receive buffer to be filled |
|
191 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
183 |
int xbee_getc(void) { |
|
184 |
// Wait for the receive buffer to be filled |
|
185 |
loop_until_bit_is_set(UCSR1A, RXC1); |
|
192 | 186 |
|
193 |
// Read the receive buffer
|
|
194 |
return UDR1;
|
|
187 |
// Read the receive buffer
|
|
188 |
return UDR1;
|
|
195 | 189 |
} |
196 | 190 |
|
197 | 191 |
/** |
... | ... | |
206 | 200 |
* |
207 | 201 |
* @see usb_init, usb_getc |
208 | 202 |
**/ |
209 |
int usb_getc_nb(char *c) |
|
210 |
{ |
|
211 |
// check if the receive buffer is filled |
|
203 |
int usb_getc_nb(char *c) { |
|
204 |
// check if the receive buffer is filled |
|
212 | 205 |
if (UCSR0A & _BV(RXC0)) { |
213 | 206 |
// Read the receive buffer |
214 | 207 |
(*c) = UDR0; |
215 | 208 |
return 0; |
216 |
} |
|
217 |
else { |
|
209 |
} else { |
|
218 | 210 |
// Return empty |
219 | 211 |
return -1; |
220 |
}
|
|
212 |
}
|
|
221 | 213 |
} |
222 | 214 |
|
223 | 215 |
/** |
... | ... | |
232 | 224 |
* |
233 | 225 |
* @see xbee_init, xbee_getc |
234 | 226 |
**/ |
235 |
int xbee_getc_nb(char *c) |
|
236 |
{ |
|
237 |
// check if the receive buffer is filled |
|
227 |
int xbee_getc_nb(char *c) { |
|
228 |
// check if the receive buffer is filled |
|
238 | 229 |
if (UCSR1A & _BV(RXC1)) { |
239 | 230 |
// Read the receive buffer |
240 | 231 |
(*c) = UDR1; |
241 | 232 |
return 0; |
242 |
} |
|
243 |
else { |
|
233 |
} else { |
|
244 | 234 |
// Return empty |
245 | 235 |
return -1; |
246 |
}
|
|
236 |
}
|
|
247 | 237 |
} |
248 | 238 |
|
249 | 239 |
|
250 | 240 |
/* |
251 |
prints an int to serial |
|
241 |
prints an int to serial
|
|
252 | 242 |
|
253 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr) |
|
254 |
uses usb_putc |
|
243 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
|
|
244 |
uses usb_putc
|
|
255 | 245 |
*/ |
256 | 246 |
/** |
257 | 247 |
* Prints an integer, converted to ASCII, to usb. usb_init must be called |
... | ... | |
264 | 254 |
* @see usb_init, usb_putc |
265 | 255 |
**/ |
266 | 256 |
int usb_puti(int value ) { |
267 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
257 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10;
|
|
268 | 258 |
|
269 |
/* convert int to ascii */ |
|
270 |
if(value<0) { usb_putc('-'); value=-value; } |
|
271 |
do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
|
259 |
/* convert int to ascii */ |
|
260 |
if(value<0) { |
|
261 |
usb_putc('-'); |
|
262 |
value=-value; |
|
263 |
} |
|
264 |
do { |
|
265 |
position--; |
|
266 |
*(usb_data+position)=(value%radix)+'0'; |
|
267 |
value/=radix; |
|
268 |
} while(value); |
|
272 | 269 |
|
273 | 270 |
|
274 |
/* start displaying the number */ |
|
275 |
for(;position<=(sizeof(usb_data)-1);position++) |
|
276 |
{ |
|
271 |
/* start displaying the number */ |
|
272 |
for(;position<=(sizeof(usb_data)-1);position++) { |
|
277 | 273 |
|
278 |
usb_putc(usb_data[position]);
|
|
279 |
}
|
|
274 |
usb_putc(usb_data[position]); |
|
275 |
} |
|
280 | 276 |
|
281 |
return 0;
|
|
277 |
return 0;
|
|
282 | 278 |
} |
283 |
|
|
284 |
|
Also available in: Unified diff