Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Defining address in microcontroller using preprocessor (#define)

Status
Not open for further replies.

nikhilsigma

Full Member level 2
Joined
Jun 19, 2010
Messages
142
Helped
17
Reputation
34
Reaction score
16
Trophy points
1,298
Location
Delhi, India
Activity points
2,584
Hello Everyone,

I came across following statement, but I am unable to understand it.
Kindly explain in detail about what exactly is happening.

Code:
[COLOR=#000000][FONT=monospace]#define GPIO_PORTA_DATA_R       (*((volatile unsigned long *)0x400043FC))


My guess is that some pointer of unsigned long type is being created with address 0x400043FC.
My understanding of pointer is as follows:[/FONT][/COLOR]
Code:
[COLOR=#000000][FONT=monospace]unsigned long my_variable; // Creating a variable[/FONT][/COLOR][COLOR=#000000][FONT=monospace]
unsigned long *my_pointer; // Creating a pointer
my_pointer=&my_variable;   // Assigning the address of my variable to my_pointer
                           //Now my_pointer contains the address of my_variable and [B]*my_pointer[/B] can be used to directly access the value of my_variable. [/FONT][/COLOR]
 

Your long-winded translation describes the meaning of the expression correctly, but other than in your code, neither a variable or a pointer are created. The purpose the #define is to give direct access to special function registers, e.g.

Code C - [expand]
1
port_input = GPIO_PORTA_DATA_R;


You are reading or writing it like a variable, but it's actually a hardware register. Similar #defines are often found in a processor specific include file.

Review your C text book about the purpose of volatile attribute in the define.
 

Thank you very much for the explanation.
But I want to know how "port_input = GPIO_PORTA_DATA_R" lets us access the register directly?

I understand that the address of a register named GPIO_PORTA_DATA_R is 0x400043FC.

I try to decifer "port_input = GPIO_PORTA_DATA_R" statement as follows:

port_input = GPIO_PORTA_DATA_R
above can be written as below since pre processor replaces GPIO_PORTA_DATA_R with its defined value.
port_input = (*((volatile unsigned long *)0x400043FC))
now i want to understand the above statement.

I think port_input will get a value written at address = ((volatile unsigned long *)0x400043FC)

I dont understand how the C compiler processes following:
((volatile unsigned long *)0x400043FC)
 

Code:
port_input = GPIO_PORTA_DATA_R;

port_input is your variable for reading the DATA port and GPIO_PORTA_DATA_R is the DATA register associated with PORTA here.

In the above code, you are reading PORTA data register into the variable port_input.

Yes, it can be written like

Code:
port_input = (*((volatile unsigned long *)0x400043FC))

It is easier to remember PORTA data register with name like GPIO_PORTA_DATA_R once it is defined in device header or in your code.
 

It's just common C syntax, required to specify the size and data type of the memory object.

Volatile instructs the compiler to skip possible optimizations and perform the accesses unconditionally and in the given order.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top