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

Indirect operator problem

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



Joined: 09 Feb 2007
Posts: 55

View user's profile Send private message

Indirect operator problem
PostPosted: Wed Sep 01, 2010 9:17 am     Reply with quote

I have a routine that sends out data to a communications port:
Code:

int1 Write_comms(int8 *ptr, n)

So, I use it by putting an address pointing to the data to go and the number of bytes to be sent out. It works fine.

Now, I need to send out the 3 least significant bytes of a 32bit integer (amp1), so I tried the following:
Code:

Write_comms(++&amp1,3);

When I tried to compile I received the following message:

Internal Error - Contact CCS INCDEC SCR=3296

Any suggestions how overcome this error?

I can get the code to work using the following code
Code:

k[0]=make8(amp1, 2);
k[1]=make8(amp1,1);
k[2]=make8(amp1,0);
Write_comms(&k[0],3);

but I wondered if there is a more elegant and better way.

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 10:02 am     Reply with quote

Problem here is trying to increment something that doesn't exist!.....
++, increments a variable/value, inside itself.
Now, &amp1, is the address of amp1, but is not in itself a manipulable object (technically not an 'lvalue').
You should be able to use:

Write_comms(((int8 *)&amp1)+1,3);

Now the 'cast' is vital. '&amp1', is a pointer to a 32bit value. If you add one to this, by the rules of pointer arithmetic in C, the value will be incremented by the size of the value. So you tell the compiler that this is now a pointer to an 8bit value, using the cast, then add one to this (giving the required address), and pass this to the function.

Best Wishes
nazoa



Joined: 09 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 10:23 am     Reply with quote

Thanks for the suggestion. I tried it, it compiles and runs but the data sent out is not correct. I think the wrong address is being passed to the routine.

Any other suggestions? Thanks.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 11:50 am     Reply with quote

I don't understand several points
The below code, said to work correctly, is generating a reversed byte order, why?
Code:
k[0]=make8(amp1,2);
k[1]=make8(amp1,1);
k[2]=make8(amp1,0);

If the above byte order would be correct, it can't with the other variant, despite of other issues
Code:
Write_comms(++&amp1,3);

Also the purpose of an increment operation isn't obvious.

The code suggested by Ttelmah is actually writing out the 3 most significant bytes.
Code:
Write_comms(((int8 *)&amp1)+1,3);
Possibly it's the intention of your increment operation, too. But in contrast to the posted text.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 8:05 pm     Reply with quote

Yes, I'd have expected
Code:

    Write_comms((int8 *)&amp1, 3);


Wouldn't it then run the function with the lowest, second lowest, and second highest bytes of amp1? And wouldn't they be correctly ordered in processor memory without any need for the make8() operator or any ++ stuff?

Well, you would need to accept the three bytes in low-to-high order. If you want them the other way around, it's more difficult.
nazoa



Joined: 09 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Thu Sep 02, 2010 6:43 am     Reply with quote

The thing is that the data sent out needs to go most significant byte first (peripheral expects it that way). So I think the flaw with the idea is the order in which the data is stored in the memory. Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 3:54 am     Reply with quote

Yes.
What I posted, was showing how to implement the original code 'to work'. In the original code, the poster tried to perform an increment on the address of the 4byte int32, before sending this to his function.
If he wants the low bytes, then no increment is needed, but the byte order in memory, would be LSB first.
However have to make the obvious comment, if the transmission always expects MSB first, then the answer is to rewrite the write_comms code to send the bytes in this order. Add the number of bytes to the passed address, and decrement this, and the counter when sending. The data will then be in the correct order, without having to either implement multiple calls, or re-arrange the bytes.....

Best Wishes
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