This is a very common error to them who works with microcontrollers. In this article, I’ll show you ways to solve this Not Enough ROM/RAM error. The high-level MCUs are capable of handling the memory mapping properly. But the lower level MCUs are not especially PIC10F,12F,16F series. But we need to use these low-level MCUs in various products to keep the price minimum. That is why we frequently face this error. Here is the solution you can try.
Disclaimer:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
Table of Contents
What is the RAM & ROM of microcontrollers?
No, I’ll not describe it here, you can read it from this link. But you should know that there are RAM and ROM in each microcontroller. ROM is used to store the program you wrote (the machine file although) and RAM is used to store the variables and data. Now, that is another section to learn but let’s go to the point.
Reason for Not Enough ROM error!:
There are several reasons for this. Also, there are solutions to them. Sometimes, little precaution will help reduce your time consumption for a project. Otherwise, it may take hours, days, weeks even months to find the problem and solve it. I’m working with microcontrollers since 2008, but still, I face this problem. But it takes less time than it took before. Anyway, let’s find the reason and ways to solve that.
1. Using too much ASCII values:
What are the ASCII values? It’s what you are reading right now. I mean the texts. While using LCDs or similar displays or writing codes for UARTs, we need to use ASCIIs. But as you know that the variables are stored in the RAM, so using so many ASCII values will consume lots of RAM as well as ROM.
Here in this code, we used the TEXT or ASCII values. This simple one-line code is consuming 15% of RAM and 14% of ROM. Now, let’s check what happens if we use more of this.
Only 9 of this consumed 93% RAM. If so, then what else you can do with this MCU? No more calculation can be used with this!!!
Done! There is no way to add more code here. But we never use a MCU only for this simple task. We need to do a lot of work with MCU like this one. So? How can we solve that?
Solution:
Convert the ASCII into a constant value. And print each digit one by one. Let’s see!
Wow!!!, only 9% of RAM is consumed. And if we add more of this?
Same 9%, but the ROM consumption is increasing. That actually gives us enough scope to add more code and do more tasks with this microcontroller. But keep that in mind, we converted the ASCIIs into constants, so it will consume ROM because constants are stored in ROM. So you must utilize this feature carefully.
The code: This code is for LCD only and for the UART, there is another one in the end of this article.
void Lcd_COut(char row, char col, const char *cptr) { char chr = 0; //first, it is used as empty string Lcd_Out(row, col, &chr); //nothing to write but set position. for ( ; chr = *cptr ; ++cptr ) Lcd_Chr_CP(chr); //out in loop }
Don’t miss the following steps, you’ll find more resources that will surprise you.
2. Using wrong variable types:
We know there are several types of variables such as int, char, float, long, double, short, etc. When a microcontroller calculates among different variable types, it needs extra memories. Also, you need a value that can not exceed 255 but you declared that as long which is 32bit wide, then it must take extra RAM to calculate such a variable. Let’s see an example:
Now, if we change the type, then the result:
It took 2% less RAM. Now we go a little further. We know that the value of a is constant. Now, if we declare a as constant, then:
Wow!!!, it took only 3% RAM and 1% ROM. So? Did you find the tricks?
3. Using calculations in extended form:
We need to use lots of calculations in the code. But there are some limitations to this as well. Let’s check the examples:
Maybe we need to use a float to calculate with. But if you check carefully, it takes 18% while we are using this way. And if we write this way?
It takes only 4% keeping the result the same. Now, the choice is yours. Besides, the a can be declared as a constant. That will give you a dramatic result.
4. Using Extra code lines:
Sometimes, we write extra lines whether that could be written directly. Let’s check this example:
As you see, here we used another variable txt just to separate the digits of the first variable ‘value’. But we could write this way which results same but works efficiently.
So? Did you find the tricks? If not, practice both.
RAM to ROM copy for UART function:
Like the LCD_COut function, this can be used for UART write which will reduce RAM consumption dramatically.
void UART_Write_CText(const char *cptr) { char chr; for ( ; chr = *cptr ; ++cptr ) UART1_Write(chr); }
This article can be helpful for you too: How to remove noise/garbage from the HD44780 LCD display
Conclusion:
There are lot more ways you can get the error we discussed in this article. But the most common ones we have already learned today. I’ll add more points to this article in the future while I face that individual problem. So don’t forget to come back and check this article again after a while.
If this article helps you a little, kindly share this with others so that they can come back from a coma situation looking for the solution to the error we learned to solve.
For Professional Designs or Help:
2 Comments
RK Hamy · 04/12/2021 at 5:44 pm
Excellent Article!
Many Thanks!
M H N · 20/05/2023 at 12:23 am
This article helps me a lot. Thanks for sharing those trics.