NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Array creation and access

November 06, 2011
by rob_k
rob_k's Avatar

I am attempting to store a series of "off", "on" states in an array. However it seems that using the array is not working. I have tested my code (modified a bit to print to stdout) on a normal gcc compiler, and everything works as expected. Can anyone see what would be wrong with the following code? The led never turns on or off. But I can turn the led on and off when i do not use an array.

//i have my leds so that the a low signal turns the leds on.
#define OFF 1
#define ON 0
uint8_t stage[10] = {ON, ON, OFF, ON, ON, ON, ON, ON, OFF, ON };
uint8_t stage_index;
//this is in a while(1) sort of loop
for(stage_index = 0; stage_index < 10; stage_index++)
{
  if(stage[stage_index] == OFF)
  {
    PORTC |= 1;
  }
  else
  {
    PORTC &= ~(1);
  }
  delay_ms(100);
}
November 06, 2011
by bl00513
bl00513's Avatar

Please let me know if you figure out what is actually going on with this. I too have been having some array problems. I am just using some simple UART / printf commands, but when I use them to display variable (from an array) I get nothing. I know this isn't any help to you, but at least you know you aren't the only person running into some array problems.

November 06, 2011
by rob_k
rob_k's Avatar

if you look at this post it seems that the data section was not being flashed to the chip. I was able to add "-j .data" (leave -j .text there too) and it seems to work just fine.

Can someone give me a quick and dirty definition of what the PROGMEM macro does? And perhaps why -j .data is not enabled by default?

November 07, 2011
by JimFrederickson
JimFrederickson's Avatar

I am pretty sure there are 2 reasons why this occurring for you, and why the Nerdkit Project Makefiles are setup the way they are. (Just to know, I am NOT talking for Nerdkits, only I believe this is accurate...)

The AVR CPU's are based a the 'Harvard Architecture'.

This means that the Program space and the Data Space are separate areas. (Most desktop computers are based on the 'Von Newmann Arhitecture' where these spaces are combined.) In fact in the AVR CPU's they are actually in a different 'format' as well.

Additionally, the AVR CPU's are mostly for 'Control Applications' where memory is often at a 'premium' so creating code that has a minimal space footprint is often important as well.

So any 'constants/literals' that are declared in the c program are normally stored in the program space so that 'writeable data space' use is mitigated.

There are functions, and initialization routines, that the Nerdkits implementation of c for the AVR CPU's has that will copy these 'constants/literals' to the 'data space'.

For me...

I just always initialize arrays or structures that I want in code.

See these links, which I think will further explain this...

NOTE: For the first one it may not seem 'immediately clear' that it is referencing what you are looking for, but keep reading.

http://www.nerdkits.com/forum/thread/100/
http://www.nerdkits.com/library/memory/

Post a Reply

Please log in to post a reply.

Did you know that multiple MOSFETs can be used in parallel to switch bigger currents on and off? Learn more...