Mikrocontroller und Digitale Elektronik
›
Thread
usart xmega probleme
hallo leute,
irgendwie funktioniert das alles nicht
hab folgenden code zur initialisierung der uartc0
1
2 // PD3 (TXD0) as output, Transmitter
3 PORTC . DIRSET = PIN3_bm ;
4 // PD2 (RXD0) as input, Receiver
5 PORTC . DIRCLR = PIN2_bm ;
6 USARTC0 . CTRLA = 0 b00001000 ;
7 USARTC0 . CTRLB = 0 b00011001 ;
8 USARTC0 . CTRLC = 0 b00000011 ;
9 /* Set Baudrate to 9600bps:
10 * Use the default I/O clock frequency that is 32 MHz.
11 * Do not use the baudrate scale factor
12 *
13 * Baudrate select = F_CPU
14 -------------------------
15 ( 16 * BAUDRATE - 1)
16 * = 208
17 */
18 USARTC0 . BAUDCTRLA = F_CPU / (( 16 * 9600 ) - 1 );
19 USARTC0 . BAUDCTRLB = 0 ;
20
21 in der main steht nicht anderes als
22
23 UARTC0 . DATA = 'A' ;
auf meinem Terminal programm kommt nur éh raus
verstehe das alles nicht
gruß jens
Hallo Gandalf,
Du hast eine falsche Baudrate. Wenn 'eh' ausgegeben wird, heisst das:
der Rest der Initialisierung ist ok.
Was für fcpu hast Du?
Du tust Dich übrigens leichter, wenn Du nicht binär, sondern in C die
Defines benutzt.
Gruß
Helmut
habe dei pll auf 32MHz stehen!!!
ist denn die formel da richtig? oder kann ich das so nicht machen?
was für defines meinst du?
X
XMEGA
Gast
06.08.2010 10:09
#1808145
Hallo,
so stelle ich meine Geschwindigkeit ein:
Init USART 115200bps ->
(F_CPU 8000000 0xAC/91)
(F_CPU 16000000 0xD7/0x93)
(F_CPU 32000000 0x17/A4)
Init USART 9600 bps ->
(F_CPU 32000000 0x7B/0xD6)
Wert für USARTE0.BAUDCTRLA/USARTE0.BAUDCTRLB
Nach Atmel Formel errechnet, 0,01 % genau
Gruß XMEGA
hat sich erledigt,
hatte in ein 8bit integer ein 16bit integer geschrieben
gruß
so leute nun kann ich auf der usart einfand frei senden aber leider noch
nicht richtig empfangen.
und zwar hab ich folgendes phenomaen
wenn ich in der main einen dauerhafte ausgabe über die usart mache
funktioniert die eingabe.
wenn ich nun denn sende befehl ausm xmega raus hole kann ich nichts mehr
eingeben.
folgenden Code habe ich
die main.c
1 volatile static usart usartC0 ;
2
3 /****************************************************************************/
4 /* definition for EEPROM memory for ATMega(16-128) and newer*/
5 /****************************************************************************/
6 uint16_t outC0 = 0 ;
7 uint16_t ui16TimerC0Config = 0 ;
8 char usartC0out [ 100 ];
9
10
11
12
13 /****************************************************************************/
14 /*MAIN Function*/
15 /****************************************************************************/
16
17
18
19
20 int main ( void )
21 {
22
23 portinit (); //Portinit for XMEGA
24 oscilatorinit (); //Oscilator set to 32MHz
25 USARTC0_Init ( 9600 , & usartC0 ); //USART Init
26 usartC0_puts ( "Uart C0 ist now online with 9600 Baud \r\n " );
27 TimerInterruptInit (); //Timer Interrupt
28 ui16TimerC0Config = TimerInitC0 ( 1024 , 500 );
29 sprintf_P ( usartC0out , PSTR ( "The Timer C0 is now working with %u \r\n " ), ui16TimerC0Config );
30 usartC0_puts ( usartC0out );
31
32 sprintf_P ( usartC0out , PSTR ( "compile date %s and time %s \r\n " ), __DATE__ , __TIME__ );
33 usartC0_puts ( usartC0out );
34
35
36
37 while ( 1 )
38 {
39
40 // sprintf_P(usartC0out,PSTR("The Comflag is \r"));
41 // usartC0_puts(usartC0out);
42
43 PORTE . DIRTGL = 0x03 ;
44
45 if ( usartC0 . comflags > 0 )
46 {
47 handlecom ( & usartC0 );
48 }
49 }
50 return 0 ;
51 }
die usartC0.c
1 /****************************************************************************/
2 /* definition for EEPROM memory for ATMega(16-128) and newer*/
3 /****************************************************************************/
4
5 /****************************************************************************/
6 /*MAIN Function*/
7 /****************************************************************************/
8
9
10 void USARTC0_Init ( unsigned long ulBaudrate , usart * usartPtr )
11 {
12 unsigned long ulBaudratCalc = 0 ;
13
14 usartC0 = usartPtr ; // Set scope pointer to the parameter
15 // PC3 (TXD0) as output, Transmitter
16 PORTC . DIRSET = PIN3_bm ;
17 // PC2 (RXD0) as input, Receiver
18 PORTC . DIRCLR = PIN2_bm ;
19 USARTC0 . CTRLA = 0 b00111100 ;
20 USARTC0 . CTRLB = 0 b00011000 ;
21 USARTC0 . CTRLC = 0 b00000011 ;
22 /* Set Baudrate to 9600bps:
23 * Use the default I/O clock frequency that is 32 MHz.
24 * Do not use the baudrate scale factor
25 *
26 * Baudrate select = F_CPU
27 -------------------------
28 ( 16 * BAUDRATE - 1)
29 * = 208*/
30 ulBaudratCalc = ( unsigned long )( F_CPU / ( 16 * ulBaudrate - 1 ));
31 USARTC0 . BAUDCTRLA = ulBaudratCalc ;
32 USARTC0 . BAUDCTRLB = 0 ;
33 }
34
35 ISR ( USARTC0_RXC_vect )
36 {
37 char c ;
38 //while(!(USARTC0.STATUS & USART_RXCIF_bm));
39 c = USARTC0 . DATA ;
40
41 if ( c == 0x0D )
42 {
43 usartC0 -> comflags |= ( 1 << RXLINERECEIVED );
44 usartC0 -> rxbuffer [ usartC0 -> rxptr ] = '\0' ;
45 usartC0 -> rxptr = 0 ;
46 // to deactivate this function you must set in the makefile CDEFS += -DUARTMODE=$(USARTMODE)
47 #ifndef UARTMODE
48 usartC0_puts_p ( PSTR ( " \r\n " ));
49 #endif
50
51 }
52 else
53 {
54 #ifndef UARTMODE
55 usartC0_putc ( c );
56 #endif
57 usartC0 -> rxbuffer [ usartC0 -> rxptr ] = c ;
58 usartC0 -> rxptr ++ ;
59
60 // delete the uart buffer and the pointer when buffer overflow
61 if ( usartC0 -> rxptr == UARTBUFFERLENGTH - 2 )
62 {
63 usartC0 -> rxptr = 0 ;
64 usartC0 -> rxbuffer [ 0 ] = '\0' ;
65 #ifndef UARTMODE
66 usartC0_puts_p ( PSTR ( "Buffer overflow usartC0 \r\n " ));
67 #endif
68 }
69 }
70 }
71
72
73
74 void usartC0_putc ( char c )
75 {
76 while (( USARTC0 . STATUS & USART_DREIF_bm ) == 0 )
77 { };
78 USARTC0 . DATA = c ;
79 }
80
81 void usartC0_puts ( const char * s )
82 {
83 while ( * s )
84 {
85 usartC0_putc ( * s );
86 s ++ ;
87 }
88 }
89
90 void usartC0_puts_p ( const char * s )
91 {
92 register char c ;
93 while ( ( c = pgm_read_byte_far (( unsigned int ) s ++ )) )
94 usartC0_putc ( c );
95 }
die define.h
1 #ifndef DEFINE_H
2 #define DEFINE_H 1
3
4
5 /***************************************************************************/
6 /* INCLUDES*/
7 /***************************************************************************/
8
9
10
11 /****************************************************************************/
12 /* definition for EEPROM memory for ATMega(16-128) and newer*/
13 /****************************************************************************/
14 #define UARTBUFFERLENGTH 30
15 #define RXLINERECEIVED 0
16
17 #define UP(Anz) sprintf_P(out,PSTR("\033[%dA"),Anz)
18 #define UP_LINE sprintf_P(out,PSTR("\033[A"))
19
20 #define RIGHT(Anz) sprintf_P(out,PSTR("\033[%dC"),Anz);uart1_puts(out)
21 #define ONE_POS_RIGHT sprintf_P(out,PSTR("\033[C"));uart1_puts(out)
22
23 #define POSITION(Ze, Sp) sprintf_P(out,PSTR("\033[%d;%dH"),Ze,Sp);uart1_puts(out)
24
25 #define CLEAR sprintf_P(out,PSTR("\033[2J"));uart1_puts(out)
26
27
28 #define FORECOLOR_BLACK sprintf_P(out,PSTR("\033[30m"));uart1_puts(out)
29 #define FORECOLOR_RED sprintf_P(out,PSTR("\033[31m"));uart1_puts(out)
30 #define FORECOLOR_GREEN sprintf_P(out,PSTR("\033[32m"));uart1_puts(out)
31 #define FORECOLOR_YELLOW sprintf_P(out,PSTR("\033[33m"));uart1_puts(out)
32 #define FORECOLOR_BLUE sprintf_P(out,PSTR("\033[34m"));uart1_puts(out)
33 #define FORECOLOR_VIOLETT sprintf_P(out,PSTR("\033[35m"));uart1_puts(out)
34 #define FORECOLOR_KOBALTBLUE sprintf_P(out,PSTR("\033[36m"));uart1_puts(out)
35 #define FORECOLOR_WHITE sprintf_P(out,PSTR("\033[37m"));uart1_puts(out)
36 #define BACKCOLOR_BLACK sprintf_P(out,PSTR("\033[40m"));uart1_puts(out)
37 #define BACKCOLOR_RED sprintf_P(out,PSTR("\033[41m"));uart1_puts(out)
38 #define BACKCOLOR_GREEN sprintf_P(out,PSTR("\033[42m"));uart1_puts(out)
39 #define BACKCOLOR_YELLOW sprintf_P(out,PSTR("\033[43m"));uart1_puts(out)
40 #define BACKCOLOR_BLUE sprintf_P(out,PSTR("\033[44m"));uart1_puts(out)
41 #define BACKCOLOR_VIOLETT sprintf_P(out,PSTR("\033[45m"));uart1_puts(out)
42 #define BACKCOLOR_KOBALTBLUE sprintf_P(out,PSTR("\033[46m"));uart1_puts(out)
43 #define BACKCOLOR_WHITE sprintf_P(out,PSTR("\033[47m"));uart1_puts(out)
44
45
46
47 typedef struct
48 {
49 unsigned char rxptr ;
50 unsigned char comflags ;
51 char rxbuffer [ UARTBUFFERLENGTH ];
52 } usart ;
53
54 #endif
ich danke euch schon mal im vorraus
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren