Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to point to a #defined list of numbers ?

Status
Not open for further replies.

volkswagenb

Programmer
Jan 22, 2007
6
US
Hello, everyone :), one question,

I am defining a list of numbers like this:
#define stuff (0, 0,255, 0, 0,255,\
5, 5,255, 5, 5,255)

And am declaring this pointer:
UINT16 *pattern_pointer;

And would like to use these numbers at run time from
a function, this is what I'm trying:

pattern_pointer=&stuff;

But it won't compile (claims syntax error).

I wonder, how can you point to these types of lists and read them?

-Thanks in advance to anyone out there ;) sorry if the question is too basic.

Context: I'm programming an embedded device (Atmel 8-bit uP). Actual number list is much bigger.


 
#define is compile time only.
When you compile, that code is being changed to:
Code:
pattern_pointer=&(0,  0,255,  0,  0,255,\
               5,  5,255,  5,  5,255)
That makes absolutely no sense, so the compiler complains about it.
Use something like this instead:
Code:
const int stuff[] = {0, 0, 255, 0, 0, 255,\
                     5, 5, 255, 5, 5, 255};
 

Thanks for your answer cpjust, now that makes sense.

Although Im' wondering if this data ends up in RAM or FLASH (im'programing a uP with 2k RAM and 32kFLASH).

I guess i'll try to make it 3k of data, then ill'know...

-Thanks again :)


 
hmm, well, it doesn't work. it attempts to allocate this space in RAM...

The program flunks once the data is more than 2k.


Thanks anywayz.. ;)
 
Which compiler?

There should be some kind of control over where it puts the data... flash or ram...
 
If your values aren't over 255, maybe you could use unsigned char instead of integer.
 
Thanks guys,

I'm actually using the Atmel AVR studio compiler.

I read that I must use a special library to retain the table in flash only and to be able to read the values.

That's the thing with embedded processors, that you're pretty limited by the hardware. There's only so much "C" can do about that.

But I'll try some other way to make the table reside within my actual code lines, so I can load it on condition or something like that.
:)
 
That's the thing with embedded processors, that you're pretty limited by the hardware. There's only so much "C" can do about that.

Unless the compiler writer gives you an option...

The Keil 8051 compiler has specific commands that locate data in flash/eprom rather than copying it to ram...
 
*nods to zeitghost*
Many embedded compilers give you a great deal of freedom in where things are placed.

This is a snip from the gcc manual page showing how to use attributes
Code:
struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
int init_data __attribute__ ((section ("INITDATA"))) = 0;

main()
{
  /* Initialize stack pointer */
  init_sp (stack + sizeof (stack));

  /* Initialize initialized data */
  memcpy (&init_data, &data, &edata - &data);

  /* Turn on the serial ports */
  init_duart (&a);
  init_duart (&b);
}
Other compilers have different syntax, so check the manual.

Even if the compiler has no such feature, the linker probably does. Through skillful use of the linker, you should be able to put whatever you want wherever you want it.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top