EmbDev.net

Forum: µC & Digital Electronics Writing strings to the UART


Author: Andreas Hoeschler (ahoesch)
Posted on:

Rate this post
0 useful
not useful
Hi all,

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

but sending a complete string

void serial_writeString0(const char *s)
  {
  while (*s)
    {
      serial_writeChar0(*s);
      s++;
    }
  }

while (1)
  {
   serial_writeChar0('A');
   serial_writeChar0('B');
   serial_writeChar0('C');
   serial_writeChar0('\n');

   serial_writeString0("Some String");
   delay_ms(500);
  }
produces dirt on the terminal.
SÿÿÿÿÿÿÿÿÿÿÿABC
SÿÿÿÿÿÿÿÿÿÿÿABC
SÿÿÿÿÿÿÿÿÿÿÿABC
SÿÿÿÿÿÿÿÿÿÿÿABC
SÿÿÿÿÿÿÿÿÿÿÿABC
SÿÿÿÿÿÿÿÿÿÿÿABC
What the heck could be the problem?

Thanks a lot in advance!!

Andreas

: Edited by Moderator
Author: stefanus (Guest)
Posted on:

Rate this post
0 useful
not useful
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.

Author: Mark R. (stevestrong)
Posted on:

Rate this post
0 useful
not useful
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

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.