go_away

Author Topic: int type binary storage, LSB first?  (Read 1298 times)

0 Members and 1 Guest are viewing this topic.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
int type binary storage, LSB first?
« on: December 24, 2009, 03:47:13 PM »

Hey guys, i have this piece of code written in the arduino IDE

struct
{
 char msgHeader;
 signed int data[3];
 char star;
 byte CS;
} msg;

msg.msgHeader = 'S';
 msg.star = '*';
 msg_size = sizeof(msg);


msg.data[0] = 1;
msg.data[1] = 2;
msg.data[2] = 400;
msg.CS = 0;


byte *p = (byte*)&msg;

for (int i = 0; i < msg_size - 2; i++)
{
     msg.CS ^= (byte)p;
}
Serial.write(p, msg_size);

The output i get over serial ( in hex mode)

53 01 00 02 00 90 01 2A C1

Notice that the output is low byte then high byte of each variable. Why is this?

I am running out of cycles on this uC, and fast ways to fix this?
Thanks.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: int type binary storage, LSB first?
« Reply #1 on: December 24, 2009, 04:16:49 PM »
well, i changed it to this

 msg.data[0] = ((distance_moved << 8) | ((unsigned int)distance_moved >>8)) ;
 msg.data[1] = ((voltage << 8) | ((unsigned int)voltage >>8)) ;
 msg.data[2] = ((current << 8) | ((unsigned int)current >>8)) ;

to get it to send the high byte first. Is there a better way to fix it, and why does it store int's like that?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: int type binary storage, LSB first?
« Reply #2 on: December 24, 2009, 05:19:50 PM »
Look up Big Endian and Little Endian in wikipedia for the reasons.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: int type binary storage, LSB first?
« Reply #3 on: December 24, 2009, 10:03:43 PM »
Look up Big Endian and Little Endian in wikipedia for the reasons.

Well, learn something new everyday.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,943
  • Helpful? 98
Re: int type binary storage, LSB first?
« Reply #4 on: December 25, 2009, 10:52:42 AM »
Well, learn something new everyday.

That's a good thing, right?

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: int type binary storage, LSB first?
« Reply #5 on: December 25, 2009, 05:57:25 PM »
You original program is behaving correctly.

You are accessing memory (maybe Flash, because your variable is initialized) directly.
Try putting your variables in RAM then try the test again. You will see that the order will again change. I don't have an AVR board in front of me right now so I can't officially verify this.

Try reading this when you have time.
http://www.avrfreaks.net/modules/FreaksFiles/files/271/DN_007.pdf
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

 


Get Your Ad Here

data_list