ParityCheck

March 27, 2009

What does this mean? (*(volatile unsigned char *)(0x22))

Filed under: Embedded Design — paritycheck @ 11:08 pm

Using C, I was trying to assign a variable name to a register address so that my code would be readable. An example of how to do this is as follows:

#define DDRA (*(volatile unsigned char *)(0x22))

This means that if a register or memory location exists at address 0x22, I can use DDRA to read or write to it like so..

DDRA = 0x05

in my C code.

The #define looks really cryptic at first. The way to understand this is by breaking it down into pieces.

First of all,

unsigned char

means we are using a byte-sized memory location. Byte being 8-bits wide.

unsigned char *

means we are declaring a pointer that points to a byte-sized location.

(unsigned char *) (0x22)

means the byte-sized pointer points to address 0x22. The C compiler will refer to address 0x22 when the variable DDRA is used. The assembly code will end up using 0x22 in Load(LD) and Store (STR) insturctions.

(*(unsigned char *)(0x22))

The first asterisk from the left signifies that we want to manipulate the value in address 0x22. * means “the value pointed to by the pointer”.

volatile

volatile forces the compiler to issue a Load or Store anytime DDRA is accessed as the value may change without the compiler knowing it.

Advertisements

9 Comments »

  1. Thank you so much for sharing! This was exactly the explanation I needed and this makes perfect sense. For embedded systems these types of definitions are critical, and understanding the syntax fully really helps with the understanding part.

    Comment by shaun — December 30, 2009 @ 1:33 pm

  2. Thanks for sharing. That really helps.

    Comment by itnovice — August 5, 2010 @ 7:35 pm

  3. Thank you + it really cheer me up!

    Comment by Zoran Saponia — December 2, 2010 @ 11:53 am

  4. really thankyou

    Comment by Raj — September 8, 2011 @ 9:50 pm

  5. Thank you.. explained clearly……

    Comment by SURESH — February 21, 2012 @ 11:35 pm

  6. thnx it is very helpful info…
    😀

    Comment by sandeep — December 9, 2012 @ 4:31 am

  7. good work nice explanation…

    Comment by LavaKumar S — February 12, 2013 @ 10:16 pm

  8. really important…thankyou

    Comment by Ronak — February 17, 2013 @ 6:53 am

  9. thank uu it really helped

    Comment by chinnu — June 17, 2013 @ 2:37 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: