CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

BUilding a signed 32 bit number

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ringo42
Guest







BUilding a signed 32 bit number
PostPosted: Sun Apr 13, 2003 8:54 am     Reply with quote

I'm sending a signed 32 bit number froma pc Via serial port as 4 bytes.
I can send a number from -32767 to 32767 and everything is fine, however if I send a 32768 the pic interprets it as a 0, a 32769 it thinks is a -1, etc

The PC side appears to be working,
if I send a 1 it sends 1 0 0 0.
If I send a 256 it sends a 0 1 0 0
If I send a 32768 it send a 0 80 0 0
if I send a -1 it send a FF FF FF FF
if I send a -2 it send FE FF FF FF

On the pic side I have tried:
R_goal_position=Rposition+make32(command_byte_5,command_byte_4,command_byte_3,command_byte_2);

and this
temp_pos_long=0;
temp_pos_long=temp_pos_long|command_byte_5;
temp_pos_long=(temp_pos_long<<8)|command_byte_4;
temp_pos_long=(temp_pos_long<<8)|command_byte_3;
temp_pos_long=(temp_pos_long<<8)|command_byte_2;
R_goal_position=Rposition+temp_pos_long;

Any ideas here why apparantly a 15bit number is ok, but beyond that it gets wacko??
Thanks
RIngo
___________________________
This message was ported from CCS's old forum
Original Post ID: 13636
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: BUilding a signed 32 bit number
PostPosted: Sun Apr 13, 2003 1:44 pm     Reply with quote

:=I'm sending a signed 32 bit number froma pc Via serial port as 4 bytes.
:=I can send a number from -32767 to 32767 and everything is fine, however if I send a 32768 the pic interprets it as a 0, a 32769 it thinks is a -1, etc
:=
-----------------------------------------------------------

You may not know that a "long" in CCS is an unsigned 16-bit
number. Also, all CCS data types default to "unsigned".
This is different from MSVC, etc.

To do a signed 32-bit number, you have to declare it like this:

signed int32 temp;


The following test program works fine. It displays "-2".

<PRE>
#include "c:\Program Files\Picc\Devices\16F877.h"
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
<BR>
//====================================================
void main()
{
char byte_2;
char byte_3;
char byte_4;
char byte_5;
signed int32 goal;
<BR>
byte_2 = 0xFe; // LSB
byte_3 = 0xFF;
byte_4 = 0xFF;
byte_5 = 0xFF; // MSB
<BR>
goal = make32(byte_5, byte_4, byte_3, byte_2);
<BR>
printf("Hex result = \%lx\n\r", goal);
printf("Decimal result = \%ld\n\r", goal);
<BR>
while(1);
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13639
R.J.Hamlett
Guest







Re: BUilding a signed 32 bit number
PostPosted: Mon Apr 14, 2003 3:14 am     Reply with quote

:=:=I'm sending a signed 32 bit number froma pc Via serial port as 4 bytes.
:=:=I can send a number from -32767 to 32767 and everything is fine, however if I send a 32768 the pic interprets it as a 0, a 32769 it thinks is a -1, etc
:=:=
:=-----------------------------------------------------------
:=
:=You may not know that a "long" in CCS is an unsigned 16-bit
:=number. Also, all CCS data types default to "unsigned".
:=This is different from MSVC, etc.
:=
:=To do a signed 32-bit number, you have to declare it like this:
:=
:=signed int32 temp;
:=
:=
:=The following test program works fine. It displays "-2".
:=
:=<PRE>
:=#include "c:\Program Files\Picc\Devices\16F877.h"
:=#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
:=#use Delay(clock=8000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
:=<BR>
:=//====================================================
:=void main()
:={
:=char byte_2;
:=char byte_3;
:=char byte_4;
:=char byte_5;
:=signed int32 goal;
:=<BR>
:=byte_2 = 0xFe; // LSB
:=byte_3 = 0xFF;
:=byte_4 = 0xFF;
:=byte_5 = 0xFF; // MSB
:=<BR>
:=goal = make32(byte_5, byte_4, byte_3, byte_2);
:=<BR>
:=printf("Hex result = \%lx\n\r", goal);
:=printf("Decimal result = \%ld\n\r", goal);
:=<BR>
:=while(1);
:=}
:=</PRE>
And of course, you can tidy up the access a lot, by using a union.
So:
union {
int8 b[4];
signed int32 word;
} goal;

Then you can use:
goal.b[0] to access the first byte of the value,
goal.b[n] to access any byte you want, and
goal.word to access the entire 32bit variable.

Makes things a lot 'tidier'. :-)

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13650
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group