Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Serial format

2005-03-14 by rtstofer

When you pick up the 4 byte value you start from the first address 
of the array of bytes.  So, this gets to the concept of 'endian'.  
In a 'little endian' architecture the low order byte will be stored 
first followed at higher addresses by higher order bytes.  In 
the 'big endian' architecture the high order byte is stored first.

The thing is, since the AVR doesn't really deal with 4 byte values 
in hardware, it doesn't know or care about endian.  The compiler, in 
this case, makes the decision of how it wants to store the data.

On the PC, I believe the order will be little endian but even there, 
the compiler can change the order.

You can determine the byte order within the long value by using a 
union to overlay a 4 byte array on top of a long variable.  Put a 
long value in the data element and then look at the bytes from low 
to high.  Or check the compiler documentation - search for 'endian'.

Something like:

union test {
    long lval;  /* whatever type has 4 bytes */
    char cval(4);
} t;

t.lval = 0x76543210;

printf("%x\n",t.lval);
printf("%x %x %x %x\n",t.cval(0),t.cval(1),t.cval(2),t.cval(3));

You will have to work out the details.

--- In AVR-Chat@yahoogroups.com, "fjch100" <fjch100@c...> wrote:
> 
> sorry I´m having problems because I´m sending a Long(4 bytes) from 
> the PC serial port to the AVR and when I sent back the 4 Bytes its 
> seem to be in reverse order!.
> Is the PC serial format LSB first and the AVR MSB first?
>  
> 
> 
> 
> 
> --- In AVR-Chat@yahoogroups.com, John Samperi <samperi@a...> wrote:
> > At 01:28 PM 14/03/2005, you wrote:
> > 
> > >HI,
> > >I have a question:
> > >when I sent a Long(4 bytes) variable from the PC to the AVR 
which
> > >byte is first? LSB ? MSB ?
> > 
> > You decide. The code for the AVR (or any other processor) has
> > to match the sender's protocol.
> > 
> > Regards
> > 
> > John Samperi
> > 
> > ******************************************************
> >                          Ampertronics Pty. Ltd.
> >    11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
> >           Tel. (02) 9674-6495       Fax (02) 9674-8745
> >                 Email: samperi@a...
> >                   Website  http://www.ampertronics.com.au
> > * Electronic Design   * Custom Products   * Contract Assembly
> > ******************************************************

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.