From: Thomas Magma on
Hello,

I'm programming in C on a dsPIC33F and I'm trying to find the most efficient
way to save a 8-bit signed value found on the LSByte of PORTD to a CHAR.
That is, the lower byte of PORTD contains a signed integer that is 8-bits
wide and I need to save it to a CHAR data type. I don't care what is on the
upper byte of PORTD.

What would be the most efficient way to do this?

Thanks.

Thomas


From: Roberto Waltman on
"Thomas Magma" wrote:
>... I'm trying to find the most efficient
>way to save a 8-bit signed value found on the LSByte of PORTD to a CHAR.
>That is, the lower byte of PORTD contains a signed integer that is 8-bits
>wide and I need to save it to a CHAR data type. I don't care what is on the
>upper byte of PORTD.

Just read the value and assign it to a char variable. Assuming you do
not need to do anything special to access the port:

#define INT16 [put right type here]

INT16 * port_d = [put address of PORTD here];
char c;

....
c = *port_d;
....
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
From: Frank Buss on
Roberto Waltman wrote:

> #define INT16 [put right type here]

You should use typedef instead of #define

> INT16 * port_d = [put address of PORTD here];

You should add a cast to avoid compiler warnings for C compilers:

INT16* port_d = (INT16*) [put address of PORTD here];

In C++ not adding a cast would be an error.

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Thomas Magma on
>>... I'm trying to find the most efficient
>>way to save a 8-bit signed value found on the LSByte of PORTD to a CHAR.
>>That is, the lower byte of PORTD contains a signed integer that is 8-bits
>>wide and I need to save it to a CHAR data type. I don't care what is on
>>the
>>upper byte of PORTD.
>
> Just read the value and assign it to a char variable. Assuming you do
> not need to do anything special to access the port:
>
> #define INT16 [put right type here]
>
> INT16 * port_d = [put address of PORTD here];
> char c;
>
> ...
> c = *port_d;
> ...

Thanks Roberto, I guess this would make sense if the data representation of
the dsPIC33 using the C30 compiler was stored in little endian format. Do
you know if it is for sure?

Thomas


From: Frank Buss on
Thomas Magma wrote:

> Thanks Roberto, I guess this would make sense if the data representation of
> the dsPIC33 using the C30 compiler was stored in little endian format. Do
> you know if it is for sure?

This would be a problem, if you access a union or cast the int* to char*,
but if you first read the int* and then assign it to an char, endian
doesn't matter, the lower 8 bits are used all the time, which is what you
asked for. Of course, if you read the 16 bit value from PORTD, endian could
be a problem.

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de