Difference between accessing Memory Mapped Registers using char and int

c, embedded

Solution

`*ptrMyReg = 0x7FFFFFFF;`

In the second case, `*ptrMyReg` is of type `unsigned char` so `0x7FFFFFFF` will be converted to `unsigned char` (i.e., value after conversion will be `0xFF`) before assignment and only one byte will be written. I don't think this what you want if you originally intended to write 4 bytes.

Problem

I have been reading about accessing Memory Mapped Registers of peripheral devices and it seems you can do multiple ways. For example: Method 1: ``` #define MyReg 0x30610000 volatile int *ptrMyReg; ptrMyReg = (volatile int *) MyReg; *ptrMyReg = 0x7FFFFFFF; /* Turn ON all bits */ ``` Method 2: ``` #define MyReg 0x30610000 volatile unsigned char *ptrMyReg; ptrMyReg = (volatile unsigned char *) MyReg; *ptrMyReg = 0x7FFFFFFF; /* Turn ON all bits */ ``` Question: Is there any specific reason as to why one would choose one over another? Assume: Size of int on architecture is 4 bytes.

Original source