[Icc-avr] Casting

Johan H. Bodin jh.bodin at telia.com
Tue Jan 15 00:49:38 PST 2008


This works for me:

-----------
#ifndef BYTEPICK_INCLUDED
#define BYTEPICK_INCLUDED

/***********************************/
/*  Johan Bodin SM6LKM 2007-06-01  */
/***********************************/

// This should ideally be predefined by the compiler...:
// ICCAVR7 uses little endian (LSByte first)

#define LITTLE_ENDIAN
//#define BIG_ENDIAN


// Sanity check:

#ifdef LITTLE_ENDIAN
#ifdef BIG_ENDIAN
#error "You can't have both endian types in bytepick.h!"
#endif
#endif

#ifndef LITTLE_ENDIAN
#ifndef BIG_ENDIAN
#error "You must select big/little endian in bytepick.h!"
#endif
#endif


// Here we go:

#ifdef BIG_ENDIAN

#define HIGH_BYTE_32(longvar)   (*((unsigned char *)(&longvar)))
#define MID_H_BYTE_32(longvar)  (*((unsigned char *)(&longvar) + 1))
#define MID_L_BYTE_32(longvar)  (*((unsigned char *)(&longvar) + 2))
#define LOW_BYTE_32(longvar)    (*((unsigned char *)(&longvar) + 3))

#define HIGH_BYTE_16(shortvar)  (*((unsigned char *)(&shortvar)))
#define LOW_BYTE_16(shortvar)   (*((unsigned char *)(&shortvar) + 1))

#else

#define HIGH_BYTE_32(longvar)   (*((unsigned char *)(&longvar) + 3))
#define MID_H_BYTE_32(longvar)  (*((unsigned char *)(&longvar) + 2))
#define MID_L_BYTE_32(longvar)  (*((unsigned char *)(&longvar) + 1))
#define LOW_BYTE_32(longvar)    (*((unsigned char *)(&longvar)))

#define HIGH_BYTE_16(shortvar)  (*((unsigned char *)(&shortvar) + 1))
#define LOW_BYTE_16(shortvar)   (*((unsigned char *)(&shortvar)))

#endif

#endif // BYTEPICK_INCLUDED

-----------

Regards
Johan Bodin



More information about the Icc-avr mailing list