NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » how does avr memory work?
October 19, 2010 by Hexorg |
Hello everyone. My code has been behaving weirdly lately and I have 2 suspects - either my avr-gcc is crocked, or I'm running out of RAM on the chip. My main question is what RAM memory is used for in AVR chips? I know, on computers, program code is stored in RAM. However, I remember original NerdKits' projects were about 8000bytes size. I still have hex file for tempsensor project that's quite big. But chip's memory is only 1024bytes, so I doubt it keeps the whole code in there. So I'm guessing that RAM is used for only variables, and the code is read from flash all the time? My code size is varying from 2092 to 2318 bytes, depending on -On flag of GCC. How would I go about calculating how much memory I'm using? Most functions that I use have around 10 bytes worth of variables, then I have two 96byte-arrays, and about 20bytes worth of global variables. So technically, I have 230 bytes give or take being used. So if i'm correct and i'm NOT using too much RAM, can I upload my source code + makefile here, and someone with a different GCC version try to compile it and send back? |
---|---|
October 19, 2010 by hevans (NerdKits Staff) |
Hi Hexorg, The program code runs straight out of the Flash memory and is never in RAM. This is why we have to be careful when defining static strings and such in Flash Memory. There is no very good way of finding out exactly how much memory you are using (barring expensive debuggers), but you can do a back of the envelope analysis like the one you did, however there are a few things to remember. Both global variables, and the call stack are held in RAM. So assuming your arrays and global variables actually add up too 230 bytes you could still end up running out of memory due to the stack. Every time you jump to a function all registers that were in use get saved to the stack and any new local variables in the function being called get allocated on stack as well. If you have recursive calls in your program this could very easily lead you running out of RAM, or if you declare those arrays in a way they get allocated more than once. If your code is behaving strangely also check that you didn't accidentally write outside of the bounds of your array, that is an easy way to get strange results. If it doesn't completely break what you are trying to do, try reducing the size of the arrays by half or so, so you are sure that is not the problem. Humberto |
Please log in to post a reply.
Did you know that you can input numbers in binary via a DIP switch, and output them to the LCD? Learn more...
|