Accessing A Memory location using Pointers in Embedded C
In microcontroller programming we have to write or read certain registers. The questions is how to access these registers ?.
The register or a memory location can easily be accessed using pointers if we know the required address. But, From where we can get theses addresses ?
The reference manual of the microcontroller contains all the details regarding address of different register or required memory locations. Let's consider we have a 32 bit microcontroller and the objective is to write data to a register having address : 0x00040002 (32 bit address)
Code:
# define addr 0x0004002
main()
{
unsigned long *p; /* As the pointer needs to store 32 bit address */
p = (unsigned long *) addr; /* Assigning Address to the pointer*/
*p = data; /* Writing data to the memory location specified by the pointer*/
return 0;
}