2108 |
paro |
1 |
#include "usart.h" |
|
|
2 |
|
|
|
3 |
#include <avr/io.h> |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
void USART_Init( unsigned int ubrr) |
|
|
7 |
{ |
|
|
8 |
/* Set baud rate */ |
|
|
9 |
UBRRH = (unsigned char)(ubrr>>8); |
|
|
10 |
UBRRL = (unsigned char)ubrr; |
|
|
11 |
|
|
|
12 |
/* flush transmitt register Double speed */ |
|
|
13 |
UCSRA = (1<<UDRE)|(1 << U2X); |
|
|
14 |
|
|
|
15 |
/* enable receiver interrupt and transmitter, pins forced */ |
|
|
16 |
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN); |
|
|
17 |
|
|
|
18 |
/* Asynchronous, no parity, Set frame format: 8data, 1stop bit */ |
|
|
19 |
UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); |
|
|
20 |
} |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
void USART_Transmit( unsigned char data ) |
|
|
24 |
{ |
|
|
25 |
while ( !( UCSRA & (1<<UDRE)) ); /* Wait for empty transmit buffer */ |
|
|
26 |
|
|
|
27 |
UDR = data; /* Put data into buffer, sends the data */ |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
void USART_Transmit_string( char *data ) |
|
|
32 |
{ |
|
|
33 |
while ( *data != 0 ) { |
|
|
34 |
USART_Transmit(*data); |
|
|
35 |
data++; |
|
|
36 |
} |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
void USART_Transmit_longnum(signed long data ) { |
|
|
41 |
unsigned char digits[10],i; |
|
|
42 |
|
|
|
43 |
if (data==0) |
|
|
44 |
USART_Transmit('0'); |
|
|
45 |
else { // PROZKOUMAT! |
|
|
46 |
|
|
|
47 |
if (data<0) { |
|
|
48 |
USART_Transmit('-'); |
|
|
49 |
data*=-1; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
for (i=0;i<10;i++) { |
|
|
53 |
digits[i]=data%10; |
|
|
54 |
data=data/10; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
i=9; |
|
|
58 |
while (digits[i]==0) i--; |
|
|
59 |
while (i>0) { |
|
|
60 |
USART_Transmit(digits[i]+48); |
|
|
61 |
i--; |
|
|
62 |
} |
|
|
63 |
USART_Transmit(digits[0]+48); |
|
|
64 |
|
|
|
65 |
} |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
void USART_Transmit_uint16 (unsigned int data ) { |
|
|
70 |
unsigned char digits[5],i; |
|
|
71 |
|
|
|
72 |
if (data==0) //podivne, ale nulu to odeslat neumi |
|
|
73 |
USART_Transmit('0'); |
|
|
74 |
else { |
|
|
75 |
|
|
|
76 |
for (i=0;i<5;i++) { |
|
|
77 |
digits[i]=data%10; |
|
|
78 |
data=data/10; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
i=4; |
|
|
82 |
while (digits[i]==0) i--; |
|
|
83 |
while (i>0) { |
|
|
84 |
USART_Transmit(digits[i]+48); |
|
|
85 |
i--; |
|
|
86 |
} |
|
|
87 |
USART_Transmit(digits[0]+48); |
|
|
88 |
|
|
|
89 |
} |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
void USART_Transmit_num(unsigned char data ) { |
|
|
94 |
unsigned char a,b,c; |
|
|
95 |
|
|
|
96 |
c=data%10; |
|
|
97 |
data=data/10; |
|
|
98 |
b=data%10; |
|
|
99 |
data=data/10; |
|
|
100 |
a=data%10; |
|
|
101 |
|
|
|
102 |
USART_Transmit(a+48); |
|
|
103 |
USART_Transmit(b+48); |
|
|
104 |
USART_Transmit(c+48); |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
|
|
|
108 |
/*void USART_Transmit_float( float data ) { |
|
|
109 |
int a,b,c; |
|
|
110 |
|
|
|
111 |
c=((int)data)%10; |
|
|
112 |
b=((int)(data/10))%10; |
|
|
113 |
a=((int)(data/100))%10; |
|
|
114 |
|
|
|
115 |
USART_Transmit(a+48); |
|
|
116 |
USART_Transmit(b+48); |
|
|
117 |
USART_Transmit(c+48); |
|
|
118 |
}*/ |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
void USART_Transmit_byte( char data ) { |
|
|
122 |
unsigned char i; |
|
|
123 |
i=0b10000000; |
|
|
124 |
while (i) { |
|
|
125 |
USART_Transmit( ( (i&data)&&1 ) + 48 ); |
|
|
126 |
i>>=1; |
|
|
127 |
} |
|
|
128 |
} |