Hallo zusammen, ich habe Probleme, mit meinem Atmega128 den UART richtig zu initialisieren. Das einzigste was geht ist, wenn ich vom PC aus eine Taste drücke kann ich diesen Tastendruck über eine LED anzeigen lassen ( pollen ). Wenn ich im Terminalprogramm am PC den Buchstaben a empfangen lassen möchte, kommen nur komische Zeichen. Messe ich mit dem Oszi, wird aber vom Atmega ein Datenpaket geschickt. Habe schon viele Tutorials durchgearbeitet, aber kein richtigies Ergebnis gefunden. Würde mir jemand helfen ????
Hier meine Initialisierung eines 256er. Der Code ist nicht sehr optimal, funktioniert aber. lg, Mario void uart1_init (unsigned int baudrate) { UART1_TxBuf_WIdx = 0 ; UART1_TxBuf_RIdx = 0 ; UART1_TxBuf_Cnt = 0 ; UART1_RxBuf_WIdx = 0 ; UART1_RxBuf_RIdx = 0 ; UART1_RxBuf_Cnt = 0 ; UART1_Tx_Disable = 0 ; // if != 0 dont transmit UART1_Tx_Priority = 0 ; // 0=normal, 1=sendXON, 2=sendXOFF /* Set baud rate */ if ( baudrate & 0x8000 ) UART1_STATUS = (1<<U2X1); //Enable 2x speed UART1_UBRR0H = ((unsigned char)(baudrate>>8))&0x80; UART1_UBRR0L = (unsigned char) baudrate; UART1_STATUS=0x00; // U2X auf LO //--------------------------- UART1_CONTROL=0x00; UART1_CONTROL|=0x80; // Rx Interrupt enable UART1_CONTROL|=0x10; // Rx enable UART1_CONTROL|=0x08; // Tx enable //--------------------------- UART1_MODE = 0x00; // asyncron, no parity, 1stopbit UART1_MODE |= 0x04; // 8datenbits UART1_MODE |= 0x02; // 8datenbits UART1_MODE |= 0x08; // 2 Stop Bits DDRD |= 0x08 ; // PORTD3 ist TxD DDRD &= ~0x04 ; // PORTD2 ist RxD PORTD |= 0x08 ; // TxD ist LowAktiv also Ruhe geben Uart_DbgInit () ; }/* uart1_init */
Danke für die vielen Antworten. Hier mal meine main.c:
1 | #include <avr/io.h> |
2 | #include <avr/interrupt.h> |
3 | #include <avr/UARTMethods.h> |
4 | #include <util/delay.h> |
5 | |
6 | |
7 | volatile unsigned char receivedByte; |
8 | |
9 | |
10 | |
11 | int main(void) |
12 | {
|
13 | DDRC |= (1<<PC0); |
14 | |
15 | InitializeUART0(9600, 0, 8, 0, 1,1); |
16 | |
17 | sei(); |
18 | |
19 | while (1) |
20 | {
|
21 | _delay_ms(1000); |
22 | TransmitUART0(0x61); |
23 | |
24 | }
|
25 | }
|
26 | ISR(USART0_RX_vect) |
27 | {
|
28 | receivedByte = UDR0; |
29 | PORTC ^= (1<<PC0); |
30 | }
|
Und hier die UARTMethods.h die in die main includiert wird:
1 | #ifndef UARTInit
|
2 | #define UARTInit
|
3 | |
4 | #define EVEN 0
|
5 | #define ODD 1
|
6 | |
7 | |
8 | #include <math.h> |
9 | #include <avr/io.h> |
10 | |
11 | unsigned char ReceiveUART0(void) |
12 | {
|
13 | while (! (UCSR0A & (1 << RXC0)) ); |
14 | |
15 | return (UDR0); |
16 | |
17 | }
|
18 | |
19 | void TransmitUART0(unsigned char data) |
20 | {
|
21 | //Wait until the Transmitter is ready
|
22 | while (! (UCSR0A & (1 << UDRE0)) ); |
23 | |
24 | //Get that data outa here!
|
25 | UDR0 = data; |
26 | }
|
27 | |
28 | void InitializeUART0(int baud, char AsyncDoubleSpeed, char DataSizeInBits, char ParityEVENorODD, char StopBits, char USARTInterruptEnable) |
29 | {
|
30 | uint16_t UBBRValue = lrint ((F_CPU / (16UL * baud) ) -1); |
31 | |
32 | if (AsyncDoubleSpeed == 1) UCSR0A = (1 << U2X0); //setting the U2X bit to 1 for double speed asynchronous |
33 | //UCSRA &= ~(1 << U2X); //setting the U2X bit to 0 for normal asynchronous
|
34 | |
35 | //Put the upper part of the baud number here (bits 8 to 11)
|
36 | UBRR0H = (unsigned char) (UBBRValue >> 8); |
37 | |
38 | //Put the remaining part of the baud number here
|
39 | UBRR0L = (unsigned char) UBBRValue; |
40 | |
41 | //Enable the receiver and transmitter
|
42 | UCSR0B = (1 << RXEN0) | (1 << TXEN0) |(1<<RXCIE0); |
43 | |
44 | //Enable interrupt
|
45 | //if USARTinteruptEnable
|
46 | |
47 | if (USARTInterruptEnable) UCSR0B |= (1<<RXCIE0); |
48 | |
49 | //Set 2 stop bits
|
50 | if (StopBits == 2) UCSR0C = (1 << USBS0); |
51 | |
52 | |
53 | if (ParityEVENorODD == EVEN) UCSR0C |= (1 << UPM01); //Sets parity to EVEN |
54 | if (ParityEVENorODD == ODD) UCSR0C |= (3 << UPM00); //Alternative way to set parity to ODD |
55 | |
56 | if (DataSizeInBits == 6) UCSR0C |= (1 << UCSZ00); //6-bit data length |
57 | if (DataSizeInBits == 7) UCSR0C |= (2 << UCSZ00); //7-bit data length, or |
58 | if (DataSizeInBits == 8) UCSR0C |= (3 << UCSZ00); //8-bit data length, or |
59 | if (DataSizeInBits == 9) UCSR0C |= (7 << UCSZ00); //9-bit data length |
60 | }
|
61 | |
62 | void InitializeUART1(int baud, char AsyncDoubleSpeed, char DataSizeInBits, char ParityEVENorODD, char StopBits, char USARTInterruptEnable ) |
63 | {
|
64 | uint16_t UBBRValue = lrint ((F_CPU / (16L * baud) ) -1); |
65 | |
66 | if (AsyncDoubleSpeed == 1) UCSR0A = (1 << U2X0); //setting the U2X bit to 1 for double speed asynchronous |
67 | //UCSRA &= ~(1 << U2X); //setting the U2X bit to 0 for normal asynchronous
|
68 | |
69 | //Put the upper part of the baud number here (bits 8 to 11)
|
70 | UBRR1H = (unsigned char) (UBBRValue >> 8); |
71 | |
72 | //Put the remaining part of the baud number here
|
73 | UBRR1L = (unsigned char) UBBRValue; |
74 | |
75 | //Enable the receiver and transmitter
|
76 | UCSR1B = (1 << RXEN1) | (1 << TXEN1); |
77 | |
78 | //Enable interrupt
|
79 | //if USARTinteruptEnable
|
80 | |
81 | if (USARTInterruptEnable) UCSR1B |= (1<<RXCIE1); |
82 | |
83 | //Set 2 stop bits
|
84 | if (StopBits == 2) UCSR1C = (1 << USBS1); |
85 | |
86 | |
87 | if (ParityEVENorODD == EVEN) UCSR1C |= (1 << UPM11); //Sets parity to EVEN |
88 | if (ParityEVENorODD == ODD) UCSR1C |= (3 << UPM10); //Alternative way to set parity to ODD |
89 | |
90 | if (DataSizeInBits == 6) UCSR1C |= (1 << UCSZ10); //6-bit data length |
91 | if (DataSizeInBits == 7) UCSR1C |= (2 << UCSZ10); //7-bit data length, or |
92 | if (DataSizeInBits == 8) UCSR1C |= (3 << UCSZ10); //8-bit data length, or |
93 | if (DataSizeInBits == 9) UCSR1C |= (7 << UCSZ10); //9-bit data length |
94 | }
|
95 | |
96 | #endif
|
:
Bearbeitet durch User
Hallo nochmal, habe den Fehler gefunden. Bei meinem Atmega 128 war der interne Quarz mit 8MHz eingestellt unter Fuses. Wenn ich nun im Programm die 8Mhz einstelle geht es wunderbar. Aber ich würde gerne den externen Quarz benutzen. Also habe ich versucht, in Eclipse die Fuses Einstellungen zu ändern. Wenn ich diese Änderung nun speichern möchte bekomm ich folgende Fehlermeldung: Internal Error: java.lang.NullPointerException
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.