|
|
View previous topic :: View next topic |
Author |
Message |
nazoa
Joined: 09 Feb 2007 Posts: 58
|
Indirect operator problem |
Posted: Wed Sep 01, 2010 9:17 am |
|
|
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(++&1,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: 19539
|
|
Posted: Wed Sep 01, 2010 10:02 am |
|
|
Problem here is trying to increment something that doesn't exist!.....
++, increments a variable/value, inside itself.
Now, &1, 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 *)&1)+1,3);
Now the 'cast' is vital. '&1', 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: 58
|
|
Posted: Wed Sep 01, 2010 10:23 am |
|
|
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
|
|
Posted: Wed Sep 01, 2010 11:50 am |
|
|
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(++&1,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 *)&1)+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
|
|
Posted: Wed Sep 01, 2010 8:05 pm |
|
|
Yes, I'd have expected
Code: |
Write_comms((int8 *)&1, 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: 58
|
|
Posted: Thu Sep 02, 2010 6:43 am |
|
|
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: 19539
|
|
Posted: Fri Sep 03, 2010 3:54 am |
|
|
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 |
|
|
|
|
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
|