root / trunk / cardbox / serial.c @ 139
History | View | Annotate | Download (2.66 KB)
1 |
/********
|
---|---|
2 |
* This file is part of Tooltron.
|
3 |
*
|
4 |
* Tooltron is free software: you can redistribute it and/or modify
|
5 |
* it under the terms of the Lesser GNU General Public License as published by
|
6 |
* the Free Software Foundation, either version 3 of the License, or
|
7 |
* (at your option) any later version.
|
8 |
*
|
9 |
* Tooltron is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* Lesser GNU General Public License for more details.
|
13 |
* You should have received a copy of the Lesser GNU General Public License
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>.
|
15 |
*
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com>
|
17 |
*
|
18 |
********/
|
19 |
/*
|
20 |
serial.c - Functions for using the RS232 serial port
|
21 |
|
22 |
author: Robotics Club, Colony Project
|
23 |
|
24 |
much code taken from FWR's library, author: Tom Lauwers
|
25 |
|
26 |
|
27 |
general note - serial -> uart
|
28 |
serial1 -> uart1
|
29 |
this is done for clarity purposes
|
30 |
*/
|
31 |
|
32 |
|
33 |
#include <avr/io.h> |
34 |
#include <stdio.h> |
35 |
#include "serial.h" |
36 |
|
37 |
//setup uart0 (serial)
|
38 |
void serial_init(unsigned int ubrr) |
39 |
{ |
40 |
DDRD |= (1<<PD1);
|
41 |
DDRD &= ~(1<<PD0);
|
42 |
|
43 |
UDR0 = 0;
|
44 |
|
45 |
/*Set baud rate - baud rates under 4800bps unsupported */
|
46 |
UBRR0H = 0x00;
|
47 |
UBRR0L = (unsigned char)ubrr; |
48 |
|
49 |
//UCSR0A |= (1<<U2X0);
|
50 |
|
51 |
/*Enable receiver and transmitter */
|
52 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
53 |
|
54 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
|
55 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
56 |
} |
57 |
|
58 |
// uart_putchar - Low-level function which puts a value into the
|
59 |
// Tx buffer for transmission. Automatically injects
|
60 |
// a \r before all \n's
|
61 |
int serial_putchar(char c) |
62 |
{ |
63 |
|
64 |
if (c == '\n') |
65 |
putchar('\r');
|
66 |
// Wait until buffer is clear for sending
|
67 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
68 |
//while ( !(UCSR0A & (1<<UDRE0)));
|
69 |
|
70 |
// Load buffer with your character
|
71 |
UDR0 = c; |
72 |
return 0; |
73 |
} |
74 |
|
75 |
// uart_getchar - Low-level function which waits for the Rx buffer
|
76 |
// to be filled, and then reads one character out of it.
|
77 |
// Note that this function blocks on read - it will wait until
|
78 |
// something fills the Rx buffer.
|
79 |
int serial_getchar(void) |
80 |
{ |
81 |
char c;
|
82 |
// Wait for the receive buffer to be filled
|
83 |
loop_until_bit_is_set(UCSR0A, RXC0); |
84 |
|
85 |
// Read the receive buffer
|
86 |
c = UDR0; |
87 |
return c;
|
88 |
} |
89 |
|
90 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer
|
91 |
// is filled, and then reads one character out of it.
|
92 |
// This is a non blocking version uart_getchar
|
93 |
int serial_getchar_nb(void) |
94 |
{ |
95 |
char c;
|
96 |
// Wait for the receive buffer to be filled
|
97 |
//loop_until_bit_is_set(UCSR0A, RXC0);
|
98 |
if (UCSR0A & _BV(RXC0)){
|
99 |
// Read the receive buffer
|
100 |
c = UDR0; |
101 |
return c;
|
102 |
} |
103 |
return 0; |
104 |
|
105 |
} |
106 |
|
107 |
|