EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 25
Pointers and Memory-Mapped Registers
How C reaches hardware.
On most microcontrollers, peripherals appear as ordinary memory addresses. Writing to a particular address changes a pin or starts a timer.
In C you create a pointer to that address and mark it volatile so every access really happens. The address below is invented for illustration.
Always check the chip's reference manual for real addresses and bit meanings.
EXAMPLE
#define PORT_OUT (*(volatile unsigned char *)0x4000) /* invented address */ PORT_OUT = 0x01; /* write to hardware */ unsigned char v = PORT_OUT; /* read it back */