View previous topic :: View next topic |
Author |
Message |
aodj
Joined: 20 Dec 2006 Posts: 40 Location: Reading, UK
|
Pointers |
Posted: Thu May 31, 2007 3:53 am |
|
|
I'm currently dealing with some pointers and I've ended up with the following scenario:
Variable v1 is a signed int16, and contains -1 or 11111111 11111110.
Variable v2 is a signed int16 also.
Code: |
signed int16 v1, v2;
v1 = -1;
v2 = &v1;
|
*v2 returns 255, or 00000000 11111110.
I can't work out why the pointer is being converted to an int8 and then cast back to an int16, losing the leading 8 bits.
Any ideas? |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Thu May 31, 2007 4:00 am |
|
|
Hello,
I do not think that a pointer can be signed ?
dro _________________ in médio virtus |
|
|
aodj
Joined: 20 Dec 2006 Posts: 40 Location: Reading, UK
|
|
Posted: Thu May 31, 2007 4:02 am |
|
|
Even if i use it as an int16, it still loses the leading 8 bits of the data.
What I think is happening, is the contents is being cast to an int8, and then recast to an int16. |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Thu May 31, 2007 4:25 am |
|
|
Hello,
Witch microcontroler are you using ?
If you use a 14 bit parts, have you put the #device *=16 directive.
Code: |
signed int16 v1;
int16 *v2 = &v1 ;
v1 = -1;
|
So *v2 is really a pointer.
v2 contain the addresses of v1.
*v2 give the value of v1;
I cannot verify that just now because i have no development board ready.
dro _________________ in médio virtus |
|
|
aodj
Joined: 20 Dec 2006 Posts: 40 Location: Reading, UK
|
|
Posted: Thu May 31, 2007 4:33 am |
|
|
inservi wrote: | Code: |
signed int16 v1;
int16 *v2 = &v1 ;
v1 = -1;
|
So *v2 is really a pointer.
v2 contain the addresses of v1.
*v2 give the value of v1;
|
What you've written above should be right, but I'm finding that the result ends up losing the leading 8 bits, as detailed in my first post.
I'm using a 16F914, and version 4.016 of the compiler. |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Thu May 31, 2007 7:53 am |
|
|
Hello,
I make some test with my version 4.039 and a 18F252.
with :
signed int16 v1;
int16 *v2 = &v1 ;
v1= -1 in binary: 11111111 11111111
*v2 = 65535 in binary 11111111 11111111 OK
v1 = -2 in binary : 11111111 11111110
*v2 = 65534 in binary : 11111111 11111110 OK
v1 = 2 in binary : 00000000 0000010
*v2 = 2 in binary : 00000000 0000010 OK
There is probably an error in the version 4.016 ?
dro _________________ in médio virtus |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 31, 2007 11:04 am |
|
|
I compiled the test program shown below with PCM vs. 4.016 and
it works OK. I tested it with MPLAB simulator, with output directed
to 'UART1'. Here is the result, displayed in the Output window:
Code: | #include <16F914.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
signed int16 v1;
signed int16 *v2;
v1 = -1;
v2 = &v1;
printf("%ld \n\r", *v2);
printf("%LX", *v2);
while(1);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Thu May 31, 2007 2:41 pm |
|
|
I'd wonder if the fault is in the output. Remember, if you use:
printf("%d \n\r", *v2);
It'll print '255', since you are telling it the contents of'v' are an integer, not a long integer, and only the low byte will then be accessed..
Best Wishes |
|
|
aodj
Joined: 20 Dec 2006 Posts: 40 Location: Reading, UK
|
|
Posted: Tue Jun 05, 2007 2:32 am |
|
|
After some more digging, and excessive use of the simulator, I've narrowed my problem down to one command, in so far as I can tell.
Code: | void fIncrement(signed int16 vPrimary, signed int16 vCPrimary)
{
*vPrimary = *vCPrimary;
}
|
appears to pass only the lower 8 bits of the numbers. I'm using the following declaration:
Code: | fIncrement(&vOffset, &vCOffset); |
The above image shows the bitpatterns I'm getting from the MPLAB simulator; note only the lower 8 bits are changing. |
|
|
|