05.08.2014 Views

1 C Programming for Embedded Systems. Data RAM Pointer Types ...

1 C Programming for Embedded Systems. Data RAM Pointer Types ...

1 C Programming for Embedded Systems. Data RAM Pointer Types ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

P. Klimo Sep 2010<br />

Assessing Individual bits:<br />

struct foo {<br />

// Declare foo as a structure type<br />

unsigned lo:1; // bit 0<br />

unsigned dummy: 6; // bits 1 to 6<br />

unsigned hi:1; // bit 7<br />

} *ptrfoo; // Declare ptrfoo as a pointer to structure type foo<br />

struct foo PortA;<br />

// Define PortA as a structure foo<br />

PortA.hi =0; // Initiate bit 7 of PortA to 0<br />

ptrfoo=&PortA;<br />

// Set pointer ptrfoo to point at PortA<br />

ptrfoo->lo = 1; // Initiate bit 0 of PortA to 1<br />

Macros:<br />

#define TWO 2<br />

// Define a constant macro<br />

int y = TWO*3;<br />

#define multiplyByTwo(x) 2*(x) // Define a “ function” macro<br />

y = multiplyByTwo(3);<br />

Typedef:<br />

int myFunc tion(int r);<br />

// Declare a function (defined elsewhere)<br />

int k = 6;<br />

typedef int (*Func)(int a) ptrFunc;<br />

ptrFunc = myFunction;<br />

// Define ptrFunc as a pointer to a function type<br />

// Point to myFunction<br />

ptrFunc(k); // Call myFunc tion with argument 6<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!