SV: [Icc-avr] Casting

Jaspers, Ton t.jaspers at cpseurope.com
Mon Jan 14 03:11:26 PST 2008


That only works on little endian machines that do not allign on word
boundaries. In other words it is not portable code. Using casting, ">>
8" and "& 0x0f" works on any machine, regardless its endianess or
allignment. 

Using structs for type conversion should be done only in combination
with #ifdef's like:  

#if BIGENDIAN  
#else  

#if ALLIGN16  
#else  

Or:  

#pragma stuff (or someting similar)  
#if COMPILERTYPE == ATMEL (or something similar)
Etc.  

At the end of the day it isn't worth the trouble. 
The >> and & methode is portable and easy to understand by anyone.  

Ton

 

> -----Original Message-----
> From: icc-avr-bounces at imagecraft.com 
> [mailto:icc-avr-bounces at imagecraft.com] On Behalf Of Edi Im Hof
> Sent: maandag 14 januari 2008 10:39
> To: Discussion list for ICCAVR and ICCtiny Users. You do NOT 
> need to subscribe to icc-announce if you are a member of this.
> Subject: Re: SV: [Icc-avr] Casting
> 
> 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                  +                              +
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
> 



More information about the Icc-avr mailing list