root / trunk / code / projects / colonet / testing / robot_routine_reg_test / serial.c @ 13
History | View | Annotate | Download (2.98 KB)
1 | 13 | emarinel | /*
|
---|---|---|---|
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 | |||
94 | //put to uart1 (serial1)
|
||
95 | int serial1_putchar(char c) |
||
96 | { |
||
97 | |||
98 | if (c == '\n') |
||
99 | serial1_putchar('\r'); //changed from putchar aj |
||
100 | // Wait until buffer is clear for sending
|
||
101 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
102 | // Load buffer with your character
|
||
103 | UDR1 = c; |
||
104 | return 0; |
||
105 | } |
||
106 | |||
107 | //get from uart1 (serial1)
|
||
108 | int serial1_getchar(void) |
||
109 | { |
||
110 | char c;
|
||
111 | // Wait for the receive buffer to be filled
|
||
112 | loop_until_bit_is_set(UCSR1A, RXC1); |
||
113 | |||
114 | // Read the receive buffer
|
||
115 | c = UDR1; |
||
116 | return c;
|
||
117 | } |
||
118 | |||
119 | //get from uart1 non-block
|
||
120 | int serial1_getchar_nb(void) |
||
121 | { |
||
122 | char c;
|
||
123 | // Wait for the receive buffer to be filled
|
||
124 | //loop_until_bit_is_set(UCSR1A, RXC1);
|
||
125 | if (UCSR1A & _BV(RXC1)){
|
||
126 | // Read the receive buffer
|
||
127 | c = UDR1; |
||
128 | return c;
|
||
129 | } |
||
130 | return 0; |
||
131 | |||
132 | } |