usart strange problem

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everybody,

I'm using ATmega128 UASART0 to send data to my PC.
I've written a small test code, to send data, and it works well ( I receive 0x10 0x01 0x11 on the PC ):

while ( ! (UCSR0A & 0x20) );
UDR0 = 0x10;

while ( ! (UCSR0A & 0x20) );
UDR0 = 0x01;

while ( ! (UCSR0A & 0x20) );
UDR0 = 0x11;

But I tryed to do the same but in an other way :

unsigned char ack[] = {0x10, 0x01, 0x11};
for ( i = 0; i < sizeof(ack) ; i++ )
{
   while ( ! (UCSR0A & 0x20) );
   UDR0 = data[i];
}

But its doesn't work properly. On my PC, I receive 0x10 0x81 0xC4

Can you help me ?

Regards,

Yann.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is data? You mean ack?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

unsigned char ack[] = {0x10, 0x01, 0x11};
for ( i = 0; i < sizeof(ack) ; i++ )
{
   while ( ! (UCSR0A & 0x20) );
   UDR0 = data[i];
} 


should that not be

unsigned char ack[] = {0x10, 0x01, 0x11};
for ( i = 0; i < sizeof(ack) ; i++ )
{
   while ( ! (UCSR0A & 0x20) );
   UDR0 = ack[i];
} 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you're right, this is (bad copy past :-)

unsigned char ack[] = {0x10, 0x01, 0x11};
for ( i = 0; i < sizeof(ack) ; i++ )
{
   while ( ! (UCSR0A & 0x20) );
   UDR0 = ack[i];
} 

But it's not working.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I mean ack ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've just found the problem. It was an optimization option.

gcc was configured with -O0 and I changed it to -Os and now its work fine.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wouldn't think -O0 would cause any problems. If anything, I would think the -Os would...

Does the compiler have an issue with:

unsigned char ack[] = {0x10, 0x01, 0x11};

instead of:

unsigned char ack[3] = {0x10, 0x01, 0x11};

?

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Both doesn't work if I leave the -O0 option.

There are no warning at all. The problem wasn't at compiling time, but when the uC send data to me.