1

I have a doubt that , in all micro controllers the flash memory much more that ram( Example: atmega16 it is 16k, However the RAM is just 1 Kb). .

So , how exactly that code is executed , does the CPU execute directly from the Flash itself , if yes then whats the use of that small RAM given.

Virendra Kumar
  • 947
  • 2
  • 13
  • 31
  • 1
    I'm no expert, but I think that a microcontroller's memory is only used to store globals, your stack and your heap. The program itself is run directly from flash. No sources though, so I'm not posting this as an answer – Kippie Mar 19 '14 at 14:55
  • Not quite sure but, 1kb is still 1.000.000 bits available. You don't get to parse HQ pictures on it, nor a real big OS. How much is the bytecode of compiled programs? – Daniel W. Mar 19 '14 at 14:55
  • Thanks for the answers, as per you the RAM is used for stacks and heap etc, (local variables). Think of program which has just the main function and a very big array(10 kb) declared inside it (so it became a local variable that should be stored in stack i,e; RAM). But the RAM is just 1 kb , in that case what will happen? #Kippie – Virendra Kumar Mar 19 '14 at 15:04
  • A pedant writes: 1kB is what is in the Mega16 which is 1,024 bytes. – DiBosco Mar 19 '14 at 15:06

3 Answers3

5

The flash memory is for storing the programs that you want to execute. They change seldom, so flash memory is appropriate.

The RAM is for the memory required during execution of the program: stack (local variables), heap (malloc), etc.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thanks for the answer, as per you the RAM is used for stacks and heap etc, (local variables). Think of program which has just the main function and a very big array(10 kb) declared inside it (so it became a local variable that should be stored in stack i,e; RAM). But the RAM is just 1 kb , in that case what will happen? – Virendra Kumar Mar 19 '14 at 15:03
  • 1
    @VirendraKumar: You will run out of memory. :-) If the array is constant, however, [you can make it part of the program](http://www.nongnu.org/avr-libc/user-manual/pgmspace.html) and it will be in flash memory. – Heinzi Mar 19 '14 at 15:07
  • 2
    You quite simply cannot have a 10k array, best case scenario, you'll overwrite your stack pointer and the program will disappear up its own ****. The compiler will warn you you're using too much RAM though. 1k is perfectly adequate for Mega16 type apps. If you really do need much more RAM, use something like an ARM. – DiBosco Mar 19 '14 at 15:09
  • But it is very probable scenario that the size of the local variables may be more than 1 kb in a big function with lot of variables(arrays). But the whole size of the program is just 2 kb. In that case also it will run out of memory? – Virendra Kumar Mar 19 '14 at 15:11
  • @VirendraKumar: Yes. In that case, the developer has to check if having all those array entries in memory *at the same time* is really required. – Heinzi Mar 19 '14 at 15:11
1

AVRs using a Harvard Architecture that strictly separates Program and Data Memory. In difference to PC that laods the Programm to RAM first to execute it from RAM, the code is directly executed from Programm Memory and only runtime data is stored in the RAM.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
1

Be aware that setting a variable as const does not necessarily create the variable and put it in flash. Although it may or may not be best off in flash, the compiler does not automatically do this.

For an example check out the following link for avr-gcc.

http://www.nongnu.org/avr-libc/user-manual/pgmspace.html

bassplayer142
  • 380
  • 2
  • 3
  • 15