1089 |
mija |
1 |
/* mija 2008 |
|
|
2 |
|
|
|
3 |
source file for hw USART ATtiny2313 |
|
|
4 |
fixed boud rate 9600 8N1 |
|
|
5 |
|
|
|
6 |
PD1 - TX |
|
|
7 |
PD0 - RX |
|
|
8 |
|
|
|
9 |
ver.: 0.0 TESTED |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
#include <avr/io.h> |
|
|
14 |
#include "rs232.h" |
|
|
15 |
|
|
|
16 |
//************************************************************** |
|
|
17 |
|
|
|
18 |
void rs232_init(void) |
|
|
19 |
{ |
|
|
20 |
//set baud rate fixed 9600 8N1 |
|
|
21 |
UCSRA = UCSRA | ( 1 << U2X) ; |
|
|
22 |
UBRRH = 0; |
|
|
23 |
UBRRL = 12; |
|
|
24 |
//enable RX TX |
|
|
25 |
UCSRB = (1<<RXEN)|(1<<TXEN); |
|
|
26 |
//8N1 |
|
|
27 |
//UCSRC = (1<<UMSEL); |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
int rs232_put(char data) |
|
|
31 |
{ |
|
|
32 |
// Wait for empty transmit buffer |
|
|
33 |
while ( !( UCSRA & (1<<UDRE)) ); |
|
|
34 |
// Put data into buffer, sends the data |
|
|
35 |
UDR = data; |
|
|
36 |
return 0; |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
int rs232_get(void) |
|
|
40 |
{ |
|
|
41 |
// Wait for data to be received |
|
|
42 |
while ( !(UCSRA & (1<<RXC)) ); |
|
|
43 |
// Get and return received data from buffer |
|
|
44 |
return UDR; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
|