17.02.2015 Views

CCS C Compiler Manual PCB / PCM / PCH

Create successful ePaper yourself

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

Using Program Memory for Data<br />

<strong>CCS</strong> C <strong>Compiler</strong> provides a few different ways to use program memory for data. The different<br />

ways are discussed below:<br />

Constant Data:<br />

The const qualifier will place the variables into program memory. If the keyword const is used<br />

before the identifier, the identifier is treated as a constant. Constants should be initialized and<br />

may not be changed at run-time. This is an easy way to create lookup tables.<br />

The rom Qualifier puts data in program memory with 3 bytes per instruction space. The address<br />

used for ROM data is not a physical address but rather a true byte address. The & operator can<br />

be used on ROM variables however the address is logical not physical.<br />

The syntax is:<br />

const type id[cexpr] = {value}<br />

For example:<br />

Placing data into ROM<br />

const int table[16]={0,1,2...15}<br />

Placing a string into ROM<br />

const char cstring[6]={"hello"}<br />

Creating pointers to constants<br />

const char *cptr;<br />

cptr = string;<br />

The #org preprocessor can be used to place the constant to specified address blocks.<br />

For example:<br />

The constant ID will be at 1C00.<br />

#ORG 0x1C00, 0x1C0F<br />

CONST CHAR ID[10]= {"123456789"};<br />

Note: Some extra code will precede the 123456789.<br />

The function label_address can be used to get the address of the constant. The constant<br />

variable can be accessed in the code. This is a great way of storing constant data in large<br />

programs. Variable length constant strings can be stored into program memory.<br />

A special method allows the use of pointers to ROM. This method does not contain extra code<br />

at the start of the structure as does constant.<br />

For example:<br />

char rom commands[] = {“put|get|status|shutdown”};<br />

The compiler allows a non-standard C feature to implement a constant array of variable length<br />

strings.<br />

The syntax is:<br />

const char id[n] [*] = { "string", "string" ...};<br />

Where n is optional and id is the table identifier.<br />

For example:<br />

const char colors[] [*] = {"Red", "Green", "Blue"};<br />

#ROM directive:<br />

Another method is to use #rom to assign data to program memory.<br />

The syntax is:<br />

#rom address = {data, data, … , data}<br />

34

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

Saved successfully!

Ooh no, something went wrong!