I have UART and printf setup "working" on an XMega128A3U with the code below. I can successfully print text to my terminal, but there are two unexpected results: 1. The "UART Ready!" text is printed twice, which isn't terrible, but I don't understand why as it is only called once. 2. Instead of printing the integer 13 (or any other number), it prints hundreds of apostrophes (0x60 in hex) for each single call of
printf(" %d ", 13);
Any attempt to use printf for other that text via %u, %i, %f gives similar unexpected results.
The test text line in main loop seems to work fine.
printf("test23456789\n\n");
Relevant code:
void uart_init() { PORTE.OUTSET = PIN7_bm; //set transmit pin as output PORTE.DIRSET = PIN7_bm; //57600 USARTE1.BAUDCTRLA = (2158 & 0xff) ; USARTE1.BAUDCTRLB = ((-6) << USART_BSCALE_gp) | ((2158 >> 8)); //Disable interrupts USARTE1_CTRLA = 0; //8 data bits, no parity and 1 stop bit USARTE1_CTRLC = USART_CHSIZE_8BIT_gc; //Enable transmit USARTE1_CTRLB = USART_TXEN_bm; } static int uart_putchar (char c, FILE *stream){ if (c == '\n') uart_putchar('\r', stream); // Wait for the transmit buffer to be empty while ( !( USARTE1.STATUS & USART_DREIF_bm) ); // Put our character into the transmit buffer USARTE1.DATA = c; return 0; } //inside main: //init data out uart on PORTE1 uart_init(); //stdio setup static FILE mystdout = FDEV_SETUP_STREAM (uart_putchar, NULL, _FDEV_SETUP_WRITE); stdout = &mystdout; printf("UART Ready!\n"); //Repeated inside Main loop printf(" %d ", 13); printf("test23456789\n\n");