Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Curious, why the second cast?

Pommie

Well-Known Member
Most Helpful Member
I was looking at some Arduino software and came across two rather interesting defines,
Code:
#define LOBYTE(w)           ((unsigned char)(w))
//! High nibble of 16bit variable
#define HIBYTE(w)           ((unsigned char)(((unsigned short)(w) >> 8) & 0xFF))
Both look very useful but I wonder about the second cast in the HIBYTE definition. Why the cast to unsigned short? In my experience, short equals 24 bits. Can someone explain this?

Thanks,

Mike.
 
An unsigned short is normally 16 bits - presumably used here because you're wanting the high byte of the word in w.

Try removing the second cast, and see if you get a warning message.
 
I'd assumed that w is already 16 bit or it's kinda pointless. I suppose if you feed the macro (define) an 8 bit value it'll stop it falling over. I now always use the uintn_t type bit safe defines so I know how big something is. Never thought unsigned short was 16 bit, learn't something today. Thanks.

Mike.
 
I guess coming from an 8 bit background (games in the 80s) and now being mainly XC8 based makes for a very limited knowledge of systems. Am I right in thinking that a short in XC8 is 24 bits?

Mike.
 
LOL I looked up XC's version of Generic types
C:
/* INT is processor specific in length may vary in size */
typedef signed int          INT;
typedef signed char         INT8;
typedef signed short int    INT16;
typedef signed long int     INT32;

/* MPLAB C Compiler for PIC18 does not support 64-bit integers */
#if !defined(__18CXX)
__EXTENSION typedef signed long long    INT64;
#endif

/* UINT is processor specific in length may vary in size */
typedef unsigned int        UINT;
typedef unsigned char       UINT8;
typedef unsigned short int  UINT16;
/* 24-bit type only available on C18 */
#if defined(__18CXX)
typedef unsigned short long UINT24;
#endif
typedef unsigned long int   UINT32;     /* other name for 32-bit integer */
/* MPLAB C Compiler for PIC18 does not support 64-bit integers */

unsigned short long UINT24... I was thinking Short round in Indiana Jones...
 
So, we now have two definitions and neither is short. We have short int and short long. Bed time for me.

Thanks all,

Mike.
 
Built in __uint24 __int24 can be used in XC8 , I use it as SPI , FRAM addresses

C:
/*                FRAM SPI DRIVERS 2Mb FRAM
  * uint8_t Fram_READ(__uint24 FramAddr);
  * READ_address 0000 to FFFF, page 00 to 03     
  *
  * void Fram_WRITE(__uint24 FramAddr,uint8_t FramData); 
  *  WRITE  address 0000 to FFFF, page 00 to 03 ,WRITE DATA .   
  *
  * void Fram_WREN();
  *  SET WRITE ENABLE LATCH
  *
  * uint8_t Fram_RDSR();
  *
  * void Fram_WRSR();
  *
  *  * Name   description            Op-code
 * WREN Set Write Enable Latch   0000 0110 B
 * WRDI Reset Write Enable Latch 0000 0100 B
 * RDSR Read Status Register     0000 0101 B
 * WRSR Write Status Register    0000 0001 B
 * READ Read Memory Code         0000 0011 B
 * WRITE Write Memory Code       0000 0010 B
 * RFID Read Device ID           1001 1111 B
 * FSTRD Fast Read Memory Code   0000 1011 B
 * SLEEP Sleep Mode              1011 1001 B
 *
 */
 

Latest threads

New Articles From Microcontroller Tips

Back
Top