Society of Robots - Robot Forum
Software => Software => Topic started by: madsci1016 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.
-
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?
-
Look up Big Endian and Little Endian in wikipedia for the reasons.
-
Look up Big Endian and Little Endian in wikipedia for the reasons.
Well, learn something new everyday.
-
Well, learn something new everyday.
That's a good thing, right?
-
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 (http://www.avrfreaks.net/modules/FreaksFiles/files/271/DN_007.pdf)