How we can use Pointers for Reading/Writing to a Memory mapped Peripheral register

How we can use Pointers for Reading/Writing to a Memory mapped Peripheral register

What are Memory Mapped Peripheral Registers?
Memory mapped Peripheral registers are those registers which are mapped to a Fixed Memory Address[Physical Address] in the Microcontroller Memory

For example: Microcontroller has different PORTS like PORTA,PORTB,PORTC....,etc.
Every PORT is mapped to a Fixed Memory Address in the memory

So we are going to Use a MACRO in our 'C' Code
Reason for using a MACRO is Better "Code Readability"

So this is our MACRO 👇👇👇
#define PORTB_REG (*(volatile unsigned char *)(0x38))

(*(volatile unsigned char *)(0x38))

How to Read this Expression?
Simple....Break it into Parts

'*' --> Pointer Deferencing Operator
(volatile unsigned char *) --> Pointer Typecasting
0x38 --> Physical Address of the Microcontroller PORT

So we are Typecasting the Memory Address of the Microcontroller PORT into a Pointer and then By using the Pointer we can READ/WRITE to the Microcontroller PORT

Below is the Embedded 'C' Code


Credits to Abhishek Mane for contributing this article. Please, reach to him on LinkedIn

Post a Comment

0 Comments