2116 |
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_num(unsigned char data ) { |
|
|
70 |
unsigned char a,b,c; |
|
|
71 |
|
|
|
72 |
c=data%10; |
|
|
73 |
data=data/10; |
|
|
74 |
b=data%10; |
|
|
75 |
data=data/10; |
|
|
76 |
a=data%10; |
|
|
77 |
|
|
|
78 |
USART_Transmit(a+48); |
|
|
79 |
USART_Transmit(b+48); |
|
|
80 |
USART_Transmit(c+48); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/*void USART_Transmit_float( float data ) { |
|
|
85 |
int a,b,c; |
|
|
86 |
|
|
|
87 |
c=((int)data)%10; |
|
|
88 |
b=((int)(data/10))%10; |
|
|
89 |
a=((int)(data/100))%10; |
|
|
90 |
|
|
|
91 |
USART_Transmit(a+48); |
|
|
92 |
USART_Transmit(b+48); |
|
|
93 |
USART_Transmit(c+48); |
|
|
94 |
}*/ |
|
|
95 |
|
|
|
96 |
|
|
|
97 |
void USART_Transmit_byte( char data ) { |
|
|
98 |
unsigned char i; |
|
|
99 |
i=0b10000000; |
|
|
100 |
while (i) { |
|
|
101 |
USART_Transmit( ( (i&data)&&1 ) + 48 ); |
|
|
102 |
i>>=1; |
|
|
103 |
} |
|
|
104 |
} |