ParityCheck

March 27, 2009

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

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 0×22, 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 0×22. The C compiler will refer to address 0×22 when the variable DDRA is used. The assembly code will end up using 0×22 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 0×22. * 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.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.