SV: [Icc-avr] Casting

Edi Im Hof edi.imhof.ml at ihe.ch
Mon Jan 14 01:39:22 PST 2008


Steven Lose schrieb:

<snip>
> 
> Struct {
> 	Unsigned int uiVar;
> 	Unsigned char ucVar[2];
>        }uVar;
> 
> uVar.uiVar = Channel_One;
> Data[7] = uVar.ucVar[1];
> Data[8] = uVar.ucVar[0];
> 

That is the way I deal with this case. It's basically the same, but I 
use defines instead if the numbers to access the single bytes. There 
are at least two advantages:
1. I don't have to think about witch one is the LSB/MSB
2. I can change the endianess (little endian/lig endian) if I have to 
in one place

"define.h":
===========
typedef unsigned char	uint8;
typedef signed   char	int8;
typedef unsigned int	uint16;
typedef signed   int	int16;
typedef unsigned long	uint32;
typedef signed   long	int32;

#define MSB16	1
#define LSB16	0
typedef union
{
	uint16 w;	/*word access*/
	uint8  b[2];	/*byte access*/
} uint16u;

typedef union
{
	int16 w;	/*word access*/
	int8  b[2];	/*byte access*/
} int16u;

#define MSB32H	3
#define MSB32L	2
#define LSB32H	1
#define LSB32L	0
#define MSB32	1
#define LSB32	0
typedef union
{
	uint32 l;	/*long access*/
	uint16 w[2];	/*word access*/
	uint8  b[4];	/*byte access*/
} uint32u;

typedef union
{
	int32 l;	/*long access*/
	int16 w[2];	/*word access*/
	int8  b[4];	/*byte access*/
} int32u;



Code example:
=====
int16u var;


Var.w = Channel_One;
data[7] = var.b[LSB16];
data[8] = var.b[MSB16];


regards
Edi


-- 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  IH electronic                +  Phone:   ++41 52 320 90 00  +
+  Edi Im Hof                   +  Fax:     ++41 52 320 90 04  +
+  Doernlerstrasse 1, Sulz      +  URL:     http://www.ihe.ch  +
+  CH-8545 Sulz                 +  E-Mail:  edi.imhof at ihe.ch   +
+  Switzerland                  +                              +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



More information about the Icc-avr mailing list