1 | #ifndef F_CPU
|
2 | #define F_CPU 16000000
|
3 | #endif
|
4 | |
5 | #define BAUD 19200UL //Baudrate
|
6 | #define UBRR_BAUD ((F_CPU/(16UL*BAUD))-1)
|
7 | |
8 | |
9 | #define length 10 //Textlänge ab welcher Empfangen beendet wird
|
10 | |
11 | |
12 | void uart_init(void) |
13 | {
|
14 | UCSRB = (1<<RXEN)|(1<<TXEN); // UART TX und RX einschalten |
15 | UCSRC |= ( 1 << URSEL )|( 3<<UCSZ0 ); // Asynchron 8N1 |
16 | //nur für 19200 Baud bei 16MHz für Mega32
|
17 | UBRRH = 0; // Highbyte ist 0 |
18 | UBRRL = 51; // Lowbyte ist 51 |
19 | |
20 | //falls anderer Mega bzw andere Frequenz
|
21 | //UBRRH = (uint8_t) (UBRR_BAUD>>8);
|
22 | //UBRRL = (uint8_t) (UBRR_BAUD & 0x0ff);
|
23 | }
|
24 | |
25 | void uart_putc(unsigned char c) |
26 | {
|
27 | while (!(UCSRA & (1<<UDRE))) {} // warten bis Senden möglich |
28 | UDR = c; // Zeichen senden |
29 | }
|
30 | |
31 | uint8_t uart_getc(void) |
32 | {
|
33 | while (!(UCSRA & (1<<RXC))) {} // warten bis Zeichen verfügbar |
34 | return UDR; // Zeichen aus UDR zurueckgeben |
35 | }
|
36 | |
37 | |
38 | int main (void) |
39 | {
|
40 | uint8_t i; |
41 | char c; |
42 | char* s; |
43 | char string[length+1]; |
44 | |
45 | uart_init(); |
46 | |
47 | while(1) |
48 | {
|
49 | |
50 | s=string; |
51 | i=0; |
52 | |
53 | do
|
54 | {
|
55 | c=uart_getc(); |
56 | if (c!='\r') |
57 | {
|
58 | *s=c; |
59 | uart_putc( c ); |
60 | s++; |
61 | i++; |
62 | }
|
63 | }
|
64 | while( i!=length && c!='\r'); |
65 | *s=0; |
66 | |
67 | uart_puts( "\r\n" ); |
68 | uart_puts( "eingegebender Text: " ); |
69 | uart_puts( string ); |
70 | while (!(UCSRA & (1<<UDRE))) {} |
71 | uart_puts( "\r\n" ); |
72 | |
73 | }
|
74 | return (0); |
75 | }
|
WARUM EMPFANGE ICH NICHTS WENN ICH WAS ÜBER DEN HYPERTERM AN DEN AMEGA16 SENDE DANKE FÜR EURE HILFE