Writing strings to the UART

OP #3908487
Hi all,

I am programming an ATMEGA2560 and am trying to send strings via UART0. 
Sending single characters works,
1
void serial_writeChar0(unsigned char c)
2
  {
3
   while ( !(UCSR0A & (1 << UDRE0)) );
4
   UDR0 = c;
5
  }
6

7
but sending a complete string
8

9
void serial_writeString0(const char *s)
10
  {
11
  while (*s)
12
    {
13
      serial_writeChar0(*s);
14
      s++;
15
    }
16
  }
17

18
while (1)
19
  {
20
   serial_writeChar0('A');
21
   serial_writeChar0('B');
22
   serial_writeChar0('C');
23
   serial_writeChar0('\n');
24

25
   serial_writeString0("Some String");
26
   delay_ms(500);
27
  }
produces dirt on the terminal.
1
SÿÿÿÿÿÿÿÿÿÿÿABC
2
SÿÿÿÿÿÿÿÿÿÿÿABC
3
SÿÿÿÿÿÿÿÿÿÿÿABC
4
SÿÿÿÿÿÿÿÿÿÿÿABC
5
SÿÿÿÿÿÿÿÿÿÿÿABC
6
SÿÿÿÿÿÿÿÿÿÿÿABC
What the heck could be the problem?

Thanks a lot in advance!!

Andreas
Guest #3908513
Maybe the bitrate is not accurate enough. The R/C oscillator is not 
recommended for serial ports and some combinations of bitrates and 
chrystal also dont work stable. The datasheet contains tables with 
recommended combinations.

Another problem cause might be not matching number of stop bits on the 
two ends of the cable.

Instead of the while loop, I usually write:

loop_until_bit_is_set(UCSR0A, UDRE0);

I'm not usre if that makes any difference. Possibly not, but it's worth 
to try.
#3909602
It would be nice to see your timer settings clocking the UART0.
Quartz frequency? UART0 Baudrate?
What other tasks run in the background? Will the UART0 timer somehow 
re-configured or used for something else?

Reply

A question of your own? This thread is over 11 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.