Hallo nochmal,
erstmal vielen Dank für die Hilfe letztes mal, leider komme ich wieder
nicht weiter :(
ich bin ein blutiger Anfänger (habe gerade mal das Anfängertutorial auf
der Seite durchgemacht) auf diesem Gebiet und bin gerade dabei eine
Atxmega64a3u so zu programmieren dass eine kommunikation über einen
seriellen Port am PC stattfinden kann. Ich benutze AtmelStudio 6.2 und
programmiere in C++.
Ich habe die Ports definiert und leider ist es so, dass am PC über den
Seriellen Anschluss nichts auslesen kann. Ich wollte euch fragen ob ihr
mir weiterhelfen könnt und möchte mich entschuldigen, wenn es ein sehr
trivialer Fehler ist..
1 | #define F_CPU 32000000UL
|
2 |
|
3 | #include <avr/io.h>
|
4 | #include <avr/interrupt.h>
|
5 | #include <util/delay.h>
|
6 | #define BAUD 9600
|
7 | #include <util/setbaud.h>
|
8 | #undef BAUD
|
9 | #include <stdio.h>
|
10 |
|
11 |
|
12 | static int uart_putchar(char c, FILE *stream);
|
13 |
|
14 | FILE * uart_str;
|
15 |
|
16 | void io_init(void){
|
17 | PORTC_DIR |= (1<<4);
|
18 | }
|
19 | void clock_init(void){
|
20 | OSC.CTRL |= OSC_RC32MEN_bm;
|
21 | while(!(OSC.STATUS & OSC_RC32MRDY_bm));
|
22 | CCP = CCP_IOREG_gc;
|
23 | CLK.CTRL = (CLK.CTRL & ~CLK_SCLKSEL_gm) | CLK_SCLKSEL_RC32M_gc;
|
24 | }
|
25 | void setUPSerial()
|
26 | {
|
27 | PORTC_REMAP |= 0x16;
|
28 | PORTC_OUTSET = PIN3_bm;
|
29 | PORTC_DIRSET = PIN3_bm;
|
30 |
|
31 | PORTC_OUTCLR = PIN2_bm;
|
32 | PORTC_DIRCLR = PIN2_bm;
|
33 |
|
34 |
|
35 | USARTC0_BAUDCTRLB = 0;
|
36 | USARTC0_BAUDCTRLA = 0x22;
|
37 | USARTC0_CTRLA =0;
|
38 | USARTC0_CTRLC = USART_CHSIZE_8BIT_gc;
|
39 | USARTC0_CTRLB = USART_TXEN_bm | USART_RXEN_bm;
|
40 | }
|
41 |
|
42 | void sendChar(char c)
|
43 | {
|
44 |
|
45 | while( !(USARTC0_STATUS & USART_DREIF_bm) );
|
46 |
|
47 | USARTC0_DATA = c;
|
48 |
|
49 | }
|
50 | void sendString(char *text)
|
51 | {
|
52 | while(*text)
|
53 |
|
54 | {
|
55 | sendChar(*text++);
|
56 | }
|
57 | }
|
58 |
|
59 | static int uart_putchar (char c, FILE *stream)
|
60 | {
|
61 | if (c == '\n')
|
62 | uart_putchar('\r', stream);
|
63 |
|
64 | while ( !(USARTC0_STATUS & USART_DREIF_bm) );
|
65 |
|
66 | USARTC0_DATA = c;
|
67 |
|
68 | return 0;
|
69 | }
|
70 |
|
71 | char usart_receiveByte()
|
72 | {
|
73 | while( !(USARTC0_STATUS & USART_RXCIF_bm) );
|
74 | return USARTC0_DATA;
|
75 | }
|
76 |
|
77 | int uart_getchar(FILE *stream)
|
78 | {
|
79 | while( !(USARTC0_STATUS & USART_RXCIF_bm) );
|
80 | char data = USARTC0_DATA;
|
81 | if(data == '\r')
|
82 | data = '\n';
|
83 | uart_putchar(data, stream);
|
84 | return data;
|
85 | }
|
86 |
|
87 | int main()
|
88 | {
|
89 | io_init();
|
90 | clock_init();
|
91 | setUPSerial();
|
92 | uart_str = fdevopen(uart_putchar, uart_getchar);
|
93 | stdout = stdin = uart_str;
|
94 | printf("Hello World!\n\r");
|
95 | while(1)
|
96 | {
|
97 | _delay_ms(100);
|
98 | PORTC_OUT &= ~(1<<4);
|
99 | _delay_ms(100);
|
100 | PORTC_OUT |= (1<<4);
|
101 | }
|
102 | return 0;
|
103 | }
|