c - Why use 2 pointers to point to register address in atmega microcontroller? -
this line defines address ddrd register in avr microcontroller
#define myddrd *((volatile unsigned char* const) 0x31) can please clarify how pointers used in above line? why need first asterisk? shouldn't second 1 enough point address 0x31?
you can separate *((volatile unsigned char* const) 0x31) 2 parts:
1 inner part: (volatile unsigned char* const) 0x31
, 1 outer part: *( inner part ).
the inner part casts integer 0x31 volatile unsigned char pointer constant.
pointer type consist of type name , asterisk after type name: type*. cast expression parenthesis uses (type)expression.
the outer part dereferences address pointer contains asterisk in front of it: *pointer in order access it's value.
if take inner part why isn't enough read , write address?
imagine pointer int* intptr pointing valid integer already. if want change integer have *intptr = 42;. if instead intptr = 42; write 42 pointers value , not address points to, 42 new address pointer contains.
in short:
macro reads 1 byte (unsigned char) address 0x31 if it's on right hand side of assigment , writes 1 byte if it's on left hand side.
usage:
typical usage bit manipulation clearing or setting single bits on register lying on specific address:
myddrd &= ~(1 << pd0); /* clear bit 0 pd0 defined 0 */ myddrd |= (1 << pd1); /* set bit 1 pd1 defined 1 */ for more information bit manipulation, see here: how set, clear, , toggle single bit?
Comments
Post a Comment