NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Array limitations
October 15, 2012 by jmuthe |
Hello, I was trying to write some test programs with the arrays and I notice that there is a limit to the amount of variables that could be stored and retrieved from an array. I wrote a program that stores variables into an array and then displays them one by one on an LCD. I wanted to see how many numbers I could put in the array and it was 440 variables. When I tried to store 441 variables the program didn't work. Is the reason for this limitation a programming error or is it simply because of the limited memory space on the ATmega168 microcontroller that came with the Nerdkit. If so then how many variables could I store in the array of the Atmel ATmega328P (Microcontroller). |
---|---|
October 16, 2012 by JimFrederickson |
I don't think anyone will be able to give you an answer if there is no code? Without seeing what is defined, and how it was defined the question cannot really be answered. (As well as surrounding code, as to it's influences as well...) In Regards to the "proportionality" of the two Microcontrollers... The 328 has twice the available RAM. (Which is likely where "your variables" are being stored.) Microcontrollers are by definition "Systems on a Chip" with "Limited Resources". It is likely the "program didn't work" result you are seeing is not simply a "limitation", but is the result of the data section and the stack section colliding. (The "data section" grows low to high, and the "stack section" grows from high to low within the same RAM Memory.) Remember that "memory sections colliding, nearly doomed the pair of Mars Rovers a few years ago. The C Compiler relies you, the programmer, to really make sure that these two sections NEVER REALLY MEET! |
October 16, 2012 by jmuthe |
|
October 16, 2012 by jmuthe |
That last post was the code I used> it works fine as it is but if I add one more number in the array, it won't work. |
October 16, 2012 by pcbolt |
jmuthe - Since all your array values are less than 255, you should be able to increase your array size by changing line 17 to:
The initial array had 2-byte (16-bit) elements so a 1-btye (8-bit) array should double your array size. This would mean out of the 1024 bytes of RAM, 880 is used for the array and the rest is being used by the code execution stack (144 bytes) like Jim said. If you switch to the 328p with 2048 bytes of RAM and the same stack size is used, you should get more than double the array size (1904 bytes) instead of 1760. For 16-bit variables, this would mean an array of 952 elements. |
October 16, 2012 by JimFrederickson |
(you could also add RAM is necessary too.) Understand though that "by definition" a Microcontroller is not a general purpose computer. So the int_16/word array that you have created with 441 entries is using 882 bytes of the 1KB of RAM. The C compiler has some RAM overhead, for the Compiler Libraries that are being used there is some additional RAM overhead for temporary variables, buffers, and other uses. RAM in the Microntroller is a precious commodity and has to be used very judiciously. You can, as mentioned, reduce the size of the memory footprint of the array by changing it to an int_8/byte array which should essentially double your maximum array size. Another option, used quite often and effectively, is to store CONSTANT ARRAYS/DATA into the Program Memory/Flash on the Microcontroller. Using this wisely you can get significant increases in capacity. You can even use Flash or EEPROM to store values that don't change that often. A log of temperature measurements for instance.) Although this is NOT a process that can be done directly, it is a process that can work well.
There have been many posts here regarding how to do this. (I had posted a pretty indepth post some time ago on this.)
This was taken from "http://www.nongnu.org" as an example. You can use the following: avr-size -A myuart.o To get and idea of how space is being used in your code.
I just used an "example" from my code. The code is myuart.o. "*.o" are the object files that the Compiler Creates. You can do that command with ANY of the Object File in your Project to get a better understanding as well. Hopefully this helps, at little. |
Please log in to post a reply.
Did you know that you can build a giant scrolling LED array with our microcontroller kit? Learn more...
|