Use of pointers in C
Pointers in C for the 68HC12
To access a memory location:
*address
Need to tell compiler whether you want to access 8-bit or 16 bit number,
signed or unsigned:
*(type *)address
To read from an eight-bit unsigned number at memory location 0x0900:
x = *(unsigned char *)0x0900;
To write an 0xaa55 to a sixteen-bit signed number at memory locations
0x0910 and 0x0911:
*(signed int *)0x0910 = 0xaa55;
If there is an address which is used alot:
#define PORTA (* (unsigned char *) 0x0000)
x = PORTA; /* Read from address 0x0000 */
PORTA = 0x55; /* Write to address 0x0000 */
To access consecutive locations in memory, use a variable as a pointer:
unsigned char *ptr;
ptr = (unsigned char *)0x0900;
*ptr = 0xaa; /* Put 0xaa into address 0x0900 */
ptr = ptr+2; /* Point two further into table */
x = *ptr; /* Read from address 0x0902 */
To set aside ten locations for a table:
unsigned char table[10];
Can access the third element in the table as:
table[2]
or as
*(table+2)
To set up a table of constant data:
const unsigned char table[] = {0x00,0x01,0x03,0x07,0x0f};