Storing constant data in program memory

From Mech
Revision as of 23:05, 5 February 2008 by BrianKephart (Talk | contribs)
Jump to: navigation, search


Original Assignment

Your PIC has a relatively large amount of program memory (32 Kbytes) but relatively little data memory (1536 bytes). For some applications, we want to have lookup tables or calibration data stored on our PIC, but we don't want it hogging data memory. This project is to demonstrate how to write a program that stores constant data in program memory. Test your ability to do this by creating a look-up table for the sin function. This is a 1-d array with integer index corresponding to an angle (e.g., numbers 0 to 89 if the index is in degrees; angles in other quadrants can be determined by simple transformations). Depending on your choice of int8, int16, or float to represent the number, what angle resolution can you use for the index before you reach the limits of program memory?

See p. 43 of the PIC MCU C Compiler book.

Finally, give sample code for storing data in the EEPROM, too.


The PIC has a relatively large amount of program memory (32 Kbytes) but relatively little data memory (1536 bytes). For some applications, we want to have lookup tables or calibration data stored on our PIC, but we don't want it hogging data memory. The CCS C Compiler provides a few different ways to use program memory for data, discussed below. We have also provided an example of how to create a look-up table for the sin function to illustrate one of these methods.

Constant Data

The CONST qualifier will place variables into program memory. If the keyword CONST is used before the identifier, the identifier is treated as a constant. These constants need to be initialized and cannot be changed at run-time.

The ROM qualifier puts data in program memory with 3 bytes per instruction space. The address used for ROM data is a true byte address not a physical address. The & operator can be used on ROM variables even though the address is logical not physical.

The syntax is: const type id[cexpr] = {value}

You must initialize the constant variable when you declare it or the compiler will spit out an error at you. Which means, unfortunately, you can't do something like:

for (i = 0; i < 100; i++)
    const float listOfNumbers[i] = i;

#ROM Directive

Another method that can be used to assign data to program memory is the #ROM directive.

The syntax is: #rom address = {data, data, ... , data}

The following example places the numbers 1, 3, 5, 7, 9 into ROM addresses starting at 0x2000:

#rom 0x2000 = {1, 3, 5, 7, 9}

You can also put strings into program memory using the #ROM directive:

#rom 0x3000 = {"mechatronicsrocks"}

Built-in Functions

The compiler provides built in functions to place data in program memory.


  • Writes data to program memory

write_program_memory(address, dataptr, count);

  • Writes count bytes of data from dataptr to address in program memory.
  • Every fourth byte of data will not be written, this needs to be filled with 0x00.


Creating a look-up table for the sin function


Constant Data Examples

Placing data into ROM

const int table[16]={0,1,2...15}

Placing a string into ROM

const char cstring[6]={"hello"}

Creating pointers to constants

const char *cptr;
cptr = string;

#ROM Directive Examples

Places 1,2,3,4 to ROM addresses starting at 0x1000

#rom 0x1000 = {1,2,3,4}

Places null terminated string in ROM

#rom 0x1000 = {"hello"}
  • This method can only be used to initialize the program memory.

Creating a look-up table for the sin function

int8 = 8 bit number; range 0 to 255 (2-3 digits) int16 = 16 bit number; range 0 to 65535 (4-5 digits) float = 32 bit number; range -1.5x10^45 to 3.4x10^38 (7-8 digits)

With values of sine from 0-89°, we can calculate sine values for all 360°. Below is code storing a table of sine values from 0-89° with a angle resolution of 1°.

const float sineTable[90] =

This table stores values as floats and is ____ in size. Since the PIC has __KB of program memory, we could theoretically hold a sine lookup table precise to ___°.

insert table of data types and angle resolution possible int8, int16, float, double?

Personal tools